Skip to content
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

docs: update description for 404 Override #8488

Merged
merged 2 commits into from
Jan 31, 2024
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
5 changes: 2 additions & 3 deletions app/Config/Routing.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,12 @@ class Routing extends BaseRouting

/**
* Sets the class/method that should be called if routing doesn't
* find a match. It can be either a closure or the controller/method
* name exactly like a route is defined: Users::index
* find a match. It can be the controller/method name like: Users::index
*
* This setting is passed to the Router class and handled there.
*
* If you want to use a closure, you will have to set it in the
* class constructor or the routes file by calling:
* routes file by calling:
*
* $routes->set404Override(function() {
* // Do something here
Expand Down
5 changes: 2 additions & 3 deletions system/Config/Routing.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,12 @@ class Routing extends BaseConfig

/**
* Sets the class/method that should be called if routing doesn't
* find a match. It can be either a closure or the controller/method
* name exactly like a route is defined: Users::index
* find a match. It can be the controller/method name like: Users::index
*
* This setting is passed to the Router class and handled there.
*
* If you want to use a closure, you will have to set it in the
* class constructor or the routes file by calling:
* routes file by calling:
*
* $routes->set404Override(function() {
* // Do something here
Expand Down
14 changes: 9 additions & 5 deletions user_guide_src/source/incoming/routing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -667,15 +667,19 @@ to only those defined by you, by setting the ``$autoRoute`` property to false:
404 Override
============

When a page is not found that matches the current URI, the system will show a generic 404 view. You can change
what happens by specifying an action to happen with the ``set404Override()`` method. The value can be either
a valid class/method pair, just like you would show in any route, or a Closure:
When a page is not found that matches the current URI, the system will show a
generic 404 view. Using the ``$override404`` property within the routing config
file, you can define controller class/method for 404 routes.

.. literalinclude:: routing/051.php

Using the ``$override404`` property within the routing config file, you can use closures. Defining the override in the Routing file is restricted to class/method pairs.
You can also change what happens by specifying an action to happen with the
``set404Override()`` method in Routes config file. The value can be either a
valid class/method pair, or a Closure:

.. note:: The ``set404Override()`` method does not change the Response status code to ``404``.
.. literalinclude:: routing/069.php

.. note:: The 404 Override feature does not change the Response status code to ``404``.
If you don't set the status code in the controller you set, the default status code ``200``
will be returned. See :php:meth:`CodeIgniter\\HTTP\\Response::setStatusCode()` for
information on how to set the status code.
Expand Down
9 changes: 0 additions & 9 deletions user_guide_src/source/incoming/routing/051.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,3 @@ class Routing extends BaseRouting
public ?string $override404 = 'App\Errors::show404';
// ...
}

// In app/Config/Routes.php
// Would execute the show404 method of the App\Errors class
$routes->set404Override('App\Errors::show404');

// Will display a custom view
$routes->set404Override(static function () {
echo view('my_errors/not_found.html');
});
10 changes: 10 additions & 0 deletions user_guide_src/source/incoming/routing/069.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

// In app/Config/Routes.php
// Would execute the show404 method of the App\Errors class
$routes->set404Override('App\Errors::show404');

// Will display a custom view
$routes->set404Override(static function () {
echo view('my_errors/not_found.html');
});
Loading