diff --git a/user_guide_src/source/incoming/controllers.rst b/user_guide_src/source/incoming/controllers.rst index e7c4015046fb..d945a0f28bf0 100644 --- a/user_guide_src/source/incoming/controllers.rst +++ b/user_guide_src/source/incoming/controllers.rst @@ -16,23 +16,23 @@ A Controller is simply a class file that is named in a way that it can be associ Consider this URI:: - example.com/index.php/blog/ + example.com/index.php/helloworld/ -In the above example, CodeIgniter would attempt to find a controller named Blog.php and load it. +In the above example, CodeIgniter would attempt to find a controller named Helloworld.php and load it. **When a controller's name matches the first segment of a URI, it will be loaded.** Let's try it: Hello World! ========================== -Let's create a simple controller so you can see it in action. Using your text editor, create a file called Blog.php, +Let's create a simple controller so you can see it in action. Using your text editor, create a file called Helloworld.php, and put the following code in it:: setDefaultController('Blog'); + $routes->setDefaultController('Helloworld'); + +Where 'Helloworld' is the name of the controller class you want to be used. + +A few lines further down **Routes.php** in the "Route Definitions" section comment out the line:: + +$routes->get('/', 'Home::index'); + +If you now browse to your site without specifying any URI segments you'll +see the “Hello World” message. -Where 'Blog' is the name of the controller class you want to be used. If you now -load your main index.php file without specifying any URI segments you'll -see your "Hello World" message by default. +.. note:: The line ``$routes->get('/', 'Home::index');`` is an optimization that you will want to use in a "real-world" app. But for demonstration purposes we don't want to use that feature. ``$routes->get()`` is explained in :doc:`URI Routing ` For more information, please refer to the "Routes Configuration Options" section of the :doc:`URI Routing ` documentation. @@ -213,18 +232,18 @@ Private methods =============== In some cases, you may want certain methods hidden from public access. -In order to achieve this, simply declare the method as being private -or protected and it will not be served via a URL request. For example, -if you were to have a method like this:: +To achieve this, simply declare the method as private or protected. +That will prevent it from being served by a URL request. For example, +if you were to define a method like this for the `Helloworld` controller:: protected function utility() { // some code } -Trying to access it via the URL, like this, will not work:: +then trying to access it using the following URL will not work:: - example.com/index.php/blog/utility/ + example.com/index.php/helloworld/utility/ Organizing Your Controllers into Sub-directories ================================================ @@ -299,7 +318,7 @@ modify this by passing the duration (in seconds) as the first parameter:: helpers ------- -You can define an array of helper files as a class property. Whenever the controller is loaded, +You can define an array of helper files as a class property. Whenever the controller is loaded these helper files will be automatically loaded into memory so that you can use their methods anywhere inside the controller:: @@ -314,11 +333,13 @@ inside the controller:: Validating data ====================== -The controller also provides a convenience method to make validating data a little simpler, ``validate()`` that -takes an array of rules to test against as the first parameter, and, optionally, -an array of custom error messages to display if the items don't pass. Internally, this uses the controller's -**$this->request** instance to get the data through. The :doc:`Validation Library docs ` -has details on the format of the rules and messages arrays, as well as available rules.:: +To simplify data checking, the controller also provides the convenience method ``validate()``. +The method accepts an array of rules in the first parameter, +and in the optional second parameter, an array of custom error messages to display +if the items are not valid. Internally, this uses the controller's +**$this->request** instance to get the data to be validated. +The :doc:`Validation Library docs ` have details on +rule and message array formats, as well as available rules.:: public function updateUser(int $userID) { @@ -335,8 +356,8 @@ has details on the format of the rules and messages arrays, as well as available // do something here if successful... } -If you find it simpler to keep the rules in the configuration file, you can replace the $rules array with the -name of the group, as defined in ``Config\Validation.php``:: +If you find it simpler to keep the rules in the configuration file, you can replace +the $rules array with the name of the group as defined in ``Config\Validation.php``:: public function updateUser(int $userID) { @@ -350,8 +371,7 @@ name of the group, as defined in ``Config\Validation.php``:: // do something here if successful... } -.. note:: Validation can also be handled automatically in the model. Where you handle validation is up to you, - and you will find that some situations are simpler in the controller than then model, and vice versa. +.. note:: Validation can also be handled automatically in the model, but sometimes it's easier to do it in the controller. Where is up to you. That's it! ==========