Skip to content

Commit 6c83470

Browse files
committed
docs: update user guide
1 parent ae4f7c8 commit 6c83470

10 files changed

Lines changed: 96 additions & 37 deletions

File tree

user_guide_src/source/changelogs/v4.4.0.rst

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,46 @@ CodeIgniter and exit()
7272
The ``CodeIgniter::run()`` method no longer calls ``exit(EXIT_SUCCESS)``. The
7373
exit call is moved to **public/index.php**.
7474

75+
.. _v440-site-uri-changes:
76+
77+
Site URI Changes
78+
================
79+
80+
A new ``SiteURI`` class that extends the ``URI`` class and represents the site
81+
URI has been added, and now it is used in many places that need the current URI.
82+
83+
``$this->request->getUri()`` in controllers returns the ``SiteURI`` instance.
84+
Also, :php:func:`site_url()`, :php:func:`base_url()`, and :php:func:`current_url()`
85+
use the SiteURI internally.
86+
87+
getPath()
88+
---------
89+
90+
The ``getPath()`` method now always returns the full URI path with leading ``/``.
91+
Therefore, when your baseURL has sub-directories and you want to get the relative
92+
path to baseURL, you must use the new ``getRoutePath()`` method instead.
93+
94+
For example::
95+
96+
baseURL: http://localhost:8888/CodeIgniter4/
97+
The current URI: http://localhost:8888/CodeIgniter4/foo/bar
98+
getPath(): /CodeIgniter4/foo/bar
99+
getRoutePath(): foo/bar
100+
101+
Site URI Values
102+
---------------
103+
104+
The SiteURI class normalizes site URIs more strictly than before, and some bugs
105+
have been fixed.
106+
107+
As a result, the framework may return site URIs or the URI paths slightly differently
108+
than in previous versions.
109+
For example, ``/`` will be added after ``index.php``::
110+
111+
http://example.com/test/index.php?page=1
112+
113+
http://example.com/test/index.php/?page=1
114+
75115
.. _v440-interface-changes:
76116

77117
Interface Changes
@@ -192,6 +232,8 @@ Libraries
192232
the value of the `full_path` index of the file if it was uploaded via directory upload.
193233
- **CURLRequest:** Added a request option ``proxy``. See
194234
:ref:`CURLRequest Class <curlrequest-request-options-proxy>`.
235+
- **URI:** A new ``SiteURI`` class that extends ``URI`` and represents the site
236+
URI has been added.
195237

196238
Helpers and Functions
197239
=====================

user_guide_src/source/incoming/incomingrequest.rst

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,10 @@ The object gives you full abilities to grab any part of the request on it's own:
228228

229229
.. literalinclude:: incomingrequest/021.php
230230

231-
You can work with the current URI string (the path relative to your baseURL) using the ``getPath()`` and ``setPath()`` methods.
232-
Note that this relative path on the shared instance of ``IncomingRequest`` is what the :doc:`URL Helper </helpers/url_helper>`
233-
functions use, so this is a helpful way to "spoof" an incoming request for testing:
231+
You can work with the current URI string (the path relative to your baseURL) using the ``getRoutePath()``.
234232

235-
.. literalinclude:: incomingrequest/022.php
233+
.. note:: The ``getRoutePath()`` method can be used since v4.4.0. Prior to v4.4.0,
234+
the ``getPath()`` method returned the path relative to your baseURL.
236235

237236
Uploaded Files
238237
**************
@@ -476,15 +475,22 @@ The methods provided by the parent classes that are available are:
476475
:returns: The current URI path relative to baseURL
477476
:rtype: string
478477

479-
This is the safest method to determine the "current URI", since ``IncomingRequest::$uri``
480-
may not be aware of the complete App configuration for base URLs.
478+
This method returns the current URI path relative to baseURL.
479+
480+
.. note:: Prior to v4.4.0, this was the safest method to determine the
481+
"current URI", since ``IncomingRequest::$uri`` might not be aware of
482+
the complete App configuration for base URLs.
481483

482484
.. php:method:: setPath($path)
483485
486+
.. deprecated:: 4.4.0
487+
484488
:param string $path: The relative path to use as the current URI
485489
:returns: This Incoming Request
486490
:rtype: IncomingRequest
487491

488-
Used mostly just for testing purposes, this allows you to set the relative path
489-
value for the current request instead of relying on URI detection. This will also
490-
update the underlying ``URI`` instance with the new path.
492+
.. note:: Prior to v4.4.0, used mostly just for testing purposes, this
493+
allowed you to set the relative path value for the current request
494+
instead of relying on URI detection. This also updated the
495+
underlying ``URI`` instance with the new path.
496+

user_guide_src/source/incoming/incomingrequest/021.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
echo $uri->getUserInfo(); // snoopy:password
88
echo $uri->getHost(); // example.com
99
echo $uri->getPort(); // 88
10-
echo $uri->getPath(); // path/to/page
10+
echo $uri->getPath(); // /path/to/page
11+
echo $uri->getRoutePath(); // path/to/page
1112
echo $uri->getQuery(); // foo=bar&bar=baz
1213
print_r($uri->getSegments()); // Array ( [0] => path [1] => to [2] => page )
1314
echo $uri->getSegment(1); // path

user_guide_src/source/incoming/incomingrequest/022.php

Lines changed: 0 additions & 15 deletions
This file was deleted.

user_guide_src/source/installation/upgrade_440.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ If your code depends on this bug, fix the segment number.
4747
.. literalinclude:: upgrade_440/002.php
4848
:lines: 2-
4949

50+
Site URI Changes
51+
================
52+
53+
When your baseURL has sub-directories and you get the relative path to baseURL of
54+
the current URI by the ``URI::getPath()`` method, you must use the new
55+
``SiteURI::getRoutePath()`` method instead.
56+
57+
See :ref:`v440-site-uri-changes` for details.
58+
5059
When You Extend Exceptions
5160
==========================
5261

user_guide_src/source/libraries/uri.rst

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,47 @@ relative URI to an existing one and have it resolved safely and correctly.
1414
Creating URI instances
1515
======================
1616

17-
Creating a URI instance is as simple as creating a new class instance:
17+
Creating a URI instance is as simple as creating a new class instance.
18+
19+
When you create the new instance, you can pass a full or partial URL in the constructor and it will be parsed
20+
into its appropriate sections:
1821

1922
.. literalinclude:: uri/001.php
23+
:lines: 2-
2024

21-
Alternatively, you can use the ``service()`` function to return an instance for you:
25+
Alternatively, you can use the :php:func:`service()` function to return an instance for you:
2226

23-
.. literalinclude:: uri/002.php
27+
.. literalinclude:: uri/003.php
28+
:lines: 2-
2429

25-
When you create the new instance, you can pass a full or partial URL in the constructor and it will be parsed
26-
into its appropriate sections:
30+
Since v4.4.0, if you don't pass a URL, it returns the current URI:
2731

28-
.. literalinclude:: uri/003.php
32+
.. literalinclude:: uri/002.php
33+
:lines: 2-
34+
35+
.. note:: The above code returns the ``SiteURI`` instance, that extends the ``URI``
36+
class. The ``URI`` class is for general URIs, but the ``SiteURI`` class is
37+
for your site URIs.
2938

3039
The Current URI
3140
---------------
3241

3342
Many times, all you really want is an object representing the current URL of this request.
34-
You can use one of the functions available in the :doc:`../helpers/url_helper`:
43+
You can use the :php:func:`current_url()` function available in the :doc:`../helpers/url_helper`:
3544

3645
.. literalinclude:: uri/004.php
46+
:lines: 2-
3747

3848
You must pass ``true`` as the first parameter, otherwise, it will return the string representation of the current URL.
3949

4050
This URI is based on the path (relative to your ``baseURL``) as determined by the current request object and
4151
your settings in ``Config\App`` (``baseURL``, ``indexPage``, and ``forceGlobalSecureRequests``).
42-
Assuming that you're in a controller that extends ``CodeIgniter\Controller`` you can get this relative path:
52+
53+
Assuming that you're in a controller that extends ``CodeIgniter\Controller``, you
54+
can also get the current SiteURI instance:
4355

4456
.. literalinclude:: uri/005.php
57+
:lines: 2-
4558

4659
===========
4760
URI Strings
@@ -136,6 +149,10 @@ can be used to manipulate it:
136149
.. note:: When setting the path this way, or any other way the class allows, it is sanitized to encode any dangerous
137150
characters, and remove dot segments for safety.
138151

152+
.. note:: Since v4.4.0, the ``SiteURI::getRoutePath()`` method,
153+
returns the URI path relative to baseURL, and the ``SiteURI::getPath()``
154+
method always returns the full URI path with leading ``/``.
155+
139156
Query
140157
-----
141158

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<?php
22

3-
$uri = new \CodeIgniter\HTTP\URI();
3+
$uri = new \CodeIgniter\HTTP\URI('http://www.example.com/some/path');
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<?php
22

3-
$uri = service('uri');
3+
$uri = service('uri'); // returns the current SiteURI instance.
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
<?php
22

3-
$uri = new \CodeIgniter\HTTP\URI('http://www.example.com/some/path');
43
$uri = service('uri', 'http://www.example.com/some/path');
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<?php
22

3-
$path = $this->request->getPath();
3+
$uri = $this->request->getUri();

0 commit comments

Comments
 (0)