Skip to content

Commit 00c6596

Browse files
authored
Merge pull request #6368 from kenjis/fix-docs-controllers.rst
docs: improve controllers.rst
2 parents 6d3a24b + f9d93ce commit 00c6596

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

user_guide_src/source/incoming/controllers.rst

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,27 @@ What is a Controller?
1313

1414
A Controller is simply a class file that handles a HTTP request. :doc:`URI Routing <routing>` associates a URI with a controller.
1515

16+
Every controller you create should extend ``BaseController`` class.
17+
This class provides several features that are available to all of your controllers.
18+
19+
Constructor
20+
***********
21+
22+
The CodeIgniter's Controller has a special constructor ``initController()``.
23+
It will be called by the framework after PHP's constructor ``__construct()`` execution.
24+
25+
If you want to override the ``initController()``, don't forget to add ``parent::initController($request, $response, $logger);`` in the method:
26+
27+
.. literalinclude:: controllers/023.php
28+
29+
.. important:: You cannot use ``return`` in the constructor. So ``return redirect()->to('route');`` does not work.
30+
31+
The ``initController()`` method sets the following three properties.
32+
1633
Included Properties
1734
*******************
1835

19-
Every controller you create should extend ``CodeIgniter\Controller`` class.
20-
This class provides several features that are available to all of your controllers.
36+
The CodeIgniter's Controller provides these properties.
2137

2238
**Request Object**
2339

@@ -95,19 +111,22 @@ The method accepts an array of data to validate in the first parameter:
95111

96112
.. literalinclude:: controllers/006.php
97113

98-
Private methods
99-
***************
114+
Protecting Methods
115+
******************
100116

101117
In some cases, you may want certain methods hidden from public access.
102118
To achieve this, simply declare the method as ``private`` or ``protected``.
103-
That will prevent it from being served by a URL request. For example,
104-
if you were to define a method like this for the ``Helloworld`` controller:
119+
That will prevent it from being served by a URL request.
120+
121+
For example, if you were to define a method like this for the ``Helloworld`` controller:
105122

106123
.. literalinclude:: controllers/007.php
107124

108-
then trying to access it using the following URL will not work::
125+
and to define a route (``helloworld/utitilty``) for the method. Then trying to access it using the following URL will not work::
126+
127+
example.com/index.php/helloworld/utility
109128

110-
example.com/index.php/helloworld/utility/
129+
Auto-routing also will not work.
111130

112131
.. _controller-auto-routing-improved:
113132

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace App\Controllers;
4+
5+
use CodeIgniter\HTTP\RequestInterface;
6+
use CodeIgniter\HTTP\ResponseInterface;
7+
use Psr\Log\LoggerInterface;
8+
9+
class Product extends BaseController
10+
{
11+
public function initController(
12+
RequestInterface $request,
13+
ResponseInterface $response,
14+
LoggerInterface $logger
15+
) {
16+
parent::initController($request, $response, $logger);
17+
18+
// Add your code here.
19+
}
20+
21+
// ...
22+
}

0 commit comments

Comments
 (0)