Skip to content

docs: improve controllers.rst #6368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions user_guide_src/source/incoming/controllers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,27 @@ What is a Controller?

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

Every controller you create should extend ``BaseController`` class.
This class provides several features that are available to all of your controllers.

Constructor
***********

The CodeIgniter's Controller has a special constructor ``initController()``.
It will be called by the framework after PHP's constructor ``__construct()`` execution.

If you want to override the ``initController()``, don't forget to add ``parent::initController($request, $response, $logger);`` in the method:

.. literalinclude:: controllers/023.php

.. important:: You cannot use ``return`` in the constructor. So ``return redirect()->to('route');`` does not work.

The ``initController()`` method sets the following three properties.

Included Properties
*******************

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

**Request Object**

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

.. literalinclude:: controllers/006.php

Private methods
***************
Protecting Methods
******************

In some cases, you may want certain methods hidden from public access.
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:
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:

.. literalinclude:: controllers/007.php

then trying to access it using the following URL will not work::
and to define a route (``helloworld/utitilty``) for the method. Then trying to access it using the following URL will not work::

example.com/index.php/helloworld/utility

example.com/index.php/helloworld/utility/
Auto-routing also will not work.

.. _controller-auto-routing-improved:

Expand Down
22 changes: 22 additions & 0 deletions user_guide_src/source/incoming/controllers/023.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Controllers;

use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;

class Product extends BaseController
{
public function initController(
RequestInterface $request,
ResponseInterface $response,
LoggerInterface $logger
) {
parent::initController($request, $response, $logger);

// Add your code here.
}

// ...
}