@@ -13,11 +13,27 @@ What is a Controller?
13
13
14
14
A Controller is simply a class file that handles a HTTP request. :doc: `URI Routing <routing >` associates a URI with a controller.
15
15
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
+
16
33
Included Properties
17
34
*******************
18
35
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.
21
37
22
38
**Request Object **
23
39
@@ -95,19 +111,22 @@ The method accepts an array of data to validate in the first parameter:
95
111
96
112
.. literalinclude :: controllers/006.php
97
113
98
- Private methods
99
- ***************
114
+ Protecting Methods
115
+ ******************
100
116
101
117
In some cases, you may want certain methods hidden from public access.
102
118
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:
105
122
106
123
.. literalinclude :: controllers/007.php
107
124
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
109
128
110
- example.com/index.php/helloworld/utility/
129
+ Auto-routing also will not work.
111
130
112
131
.. _controller-auto-routing-improved :
113
132
0 commit comments