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

feat: 404 controller also can get PageNotFoundException message #8491

Merged
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: 4 additions & 1 deletion system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,10 @@ protected function display404errors(PageNotFoundException $e)
$this->method = $override[1];

$controller = $this->createController();
$returned = $this->runController($controller);

$returned = $controller->{$this->method}($e->getMessage());

$this->benchmark->stop('controller');
}

unset($override);
Expand Down
24 changes: 24 additions & 0 deletions tests/_support/Errors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace Tests\Support;

use CodeIgniter\Controller;

class Errors extends Controller
{
public function show404(string $message): string
{
return $message;
}
}
9 changes: 5 additions & 4 deletions tests/system/CodeIgniterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,21 +122,22 @@ public function testRunClosureRoute(): void

public function testRun404Override(): void
{
$_SERVER['argv'] = ['index.php', '/'];
$_SERVER['argc'] = 2;
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = '/pages/about';
$_SERVER['SCRIPT_NAME'] = '/index.php';

// Inject mock router.
$routes = Services::routes();
$routes->setAutoRoute(false);
$routes->set404Override('Tests\Support\Controllers\Hello::index');
$routes->set404Override('Tests\Support\Errors::show404');
$router = Services::router($routes, Services::incomingrequest());
Services::injectMock('router', $router);

ob_start();
$this->codeigniter->run($routes);
$output = ob_get_clean();

$this->assertStringContainsString('Hello', $output);
$this->assertStringContainsString("Can't find a route for 'GET: pages/about'.", $output);
}

public function testRun404OverrideControllerReturnsResponse(): void
Expand Down
11 changes: 7 additions & 4 deletions user_guide_src/source/changelogs/v4.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,13 @@ Others
- **AutoRouting Improved:** The ``$translateUriToCamelCase`` option has been added
that allows using CamelCase controller and method names. See
:ref:`controller-translate-uri-to-camelcase`.
- **Routing:** Added option ``$multipleSegmentsOneParam``. When this option is
enabled, a placeholder that matches multiple segments, such as ``(:any)``, will
be passed directly as it is to one parameter, even if it contains multiple segments.
See :ref:`multiple-uri-segments-as-one-parameter` for details.
- **Routing:**
- Added option ``$multipleSegmentsOneParam``. When this option is
enabled, a placeholder that matches multiple segments, such as ``(:any)``, will
be passed directly as it is to one parameter, even if it contains multiple segments.
See :ref:`multiple-uri-segments-as-one-parameter` for details.
- Now the 404 controller's method that you set in ``$override404`` also receive
a ``PageNotFoundException`` message as the first parameter.
- **Autoloader:**
- Autoloading performance when using Composer has been improved.
Adding the ``App`` namespace in the ``autoload.psr4`` setting in **composer.json**
Expand Down
Loading