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

fix: filters are executed when controller does not exist with Auto Routing (Legacy). #7925

Merged
merged 20 commits into from
Oct 28, 2023
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
7 changes: 5 additions & 2 deletions system/Router/AutoRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,13 @@ public function getRoute(string $uri, string $httpVerb): array

// Load the file so that it's available for CodeIgniter.
$file = APPPATH . 'Controllers/' . $this->directory . $controllerName . '.php';
if (is_file($file)) {
include_once $file;

if (! is_file($file)) {
throw PageNotFoundException::forControllerNotFound($this->controller, $this->method);
}

include_once $file;

// Ensure the controller stores the fully-qualified class name
// We have to check for a length over 1, since by default it will be '\'
if (strpos($this->controller, '\\') === false && strlen($this->defaultNamespace) > 1) {
Expand Down
21 changes: 21 additions & 0 deletions tests/_support/_controller/Admin_user.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* 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 App\Controllers;

use CodeIgniter\Controller;

class Admin_user extends Controller
{
public function show_list(): void
{
}
}
25 changes: 25 additions & 0 deletions tests/_support/_controller/Dash_folder/Dash_controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/**
* 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 App\Controllers\Dash_folder;

use CodeIgniter\Controller;

class Dash_controller extends Controller
{
public function getSomemethod(): void
{
}

public function getDash_method(): void
{
}
}
21 changes: 21 additions & 0 deletions tests/_support/_controller/Dash_folder/Home.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* 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 App\Controllers\Dash_folder;

use CodeIgniter\Controller;

class Home extends Controller
{
public function getIndex(): void
{
}
}
21 changes: 21 additions & 0 deletions tests/_support/_controller/Dash_folder/Mycontroller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* 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 App\Controllers\Dash_folder;

use CodeIgniter\Controller;

class Mycontroller extends Controller
{
public function getSomemethod(): void
{
}
}
25 changes: 25 additions & 0 deletions tests/_support/_controller/Mycontroller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/**
* 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 App\Controllers;

use CodeIgniter\Controller;

class Mycontroller extends Controller
{
public function getIndex(): void
{
}

public function getSomemethod($first = ''): void
{
}
}
21 changes: 21 additions & 0 deletions tests/_support/_controller/Product.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* 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 App\Controllers;

use CodeIgniter\Controller;

class Product extends Controller
{
public function index(): void
{
}
}
21 changes: 21 additions & 0 deletions tests/_support/_controller/Subfolder/Mycontroller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* 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 App\Controllers\Subfolder;

use CodeIgniter\Controller;

class Mycontroller extends Controller
{
public function getSomemethod(): void
{
}
}
21 changes: 21 additions & 0 deletions tests/_support/_controller/foo/bar/baz/Some_controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* 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 App\Controllers\foo\bar\baz;

use CodeIgniter\Controller;

class Some_controller extends Controller
{
public function some_method($first = ''): void
{
}
}
21 changes: 21 additions & 0 deletions tests/_support/_controller/Φ.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* 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 App\Controllers;

use CodeIgniter\Controller;

class Φ extends Controller
{
public function getIndex(): void
{
}
}
21 changes: 21 additions & 0 deletions tests/_support/_controller/Φ/Home.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* 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 App\Controllers\Φ;

use CodeIgniter\Controller;

class Home extends Controller
{
public function getIndex(): void
{
}
}
28 changes: 28 additions & 0 deletions tests/system/CodeIgniterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use CodeIgniter\Config\Services;
use CodeIgniter\Exceptions\ConfigException;
use CodeIgniter\Exceptions\PageNotFoundException;
use CodeIgniter\HTTP\Response;
use CodeIgniter\Router\Exceptions\RedirectException;
use CodeIgniter\Router\RouteCollection;
Expand All @@ -25,6 +26,7 @@
use Config\Modules;
use Config\Routing;
use Tests\Support\Filters\Customfilter;
use Tests\Support\Filters\RedirectFilter;

/**
* @runTestsInSeparateProcesses
Expand Down Expand Up @@ -926,4 +928,30 @@ public static function providePageCacheWithCacheQueryString(): iterable
'$cacheQueryString=array' => [['important_parameter'], 3, $testingUrls],
];
}

/**
* See https://github.com/codeigniter4/CodeIgniter4/issues/7205
*/
public function testRunControllerNotFoundBeforeFilter(): void
{
$_SERVER['argv'] = ['index.php'];
$_SERVER['argc'] = 1;

$_SERVER['REQUEST_URI'] = '/cannotFound';
$_SERVER['SCRIPT_NAME'] = '/index.php';

// Inject mock router.
$routes = Services::routes();
$routes->setAutoRoute(true);

// Inject the before filter.
$filterConfig = config('Filters');
$filterConfig->aliases['redirectFilter'] = RedirectFilter::class;
$filterConfig->globals['before'] = ['redirectFilter'];
Services::filters($filterConfig);

$this->expectException(PageNotFoundException::class);

$this->codeigniter->run($routes);
}
}
13 changes: 11 additions & 2 deletions tests/system/Router/RouteCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace CodeIgniter\Router;

use App\Controllers\Product;
use CodeIgniter\Config\Services;
use CodeIgniter\controller;
use CodeIgniter\Exceptions\PageNotFoundException;
Expand Down Expand Up @@ -1778,10 +1779,14 @@ public function testAutoRoutesControllerNameReturnsFQCN($namespace): void
$routes->setAutoRoute(true);
$routes->setDefaultNamespace($namespace);

copy(TESTPATH . '_support/_controller/Product.php', APPPATH . 'Controllers/Product.php');

$router = new Router($routes, Services::request());
$router->handle('/product');

$this->assertSame('\App\\Controllers\\Product', $router->controllerName());
unlink(APPPATH . 'Controllers/Product.php');

$this->assertSame('\\' . Product::class, $router->controllerName());
}

/**
Expand All @@ -1797,10 +1802,14 @@ public function testRoutesControllerNameReturnsFQCN($namespace): void
$routes->setDefaultNamespace($namespace);
$routes->get('/product', 'Product');

copy(TESTPATH . '_support/_controller/Product.php', APPPATH . 'Controllers/Product.php');

$router = new Router($routes, Services::request());
$router->handle('/product');

$this->assertSame('\App\\Controllers\\Product', $router->controllerName());
unlink(APPPATH . 'Controllers/Product.php');

$this->assertSame('\\' . Product::class, $router->controllerName());
}

public function testRoutePriorityDetected(): void
Expand Down
Loading
Loading