Skip to content

Commit c33e7fd

Browse files
committed
docs: add about initController()
1 parent 6907ce6 commit c33e7fd

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

user_guide_src/source/incoming/controllers.rst

+18-2
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

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)