Skip to content

Commit

Permalink
Merge pull request #7925 from ping-yee/230910_filter
Browse files Browse the repository at this point in the history
fix: filters are executed when controller does not exist with Auto Routing (Legacy).
  • Loading branch information
kenjis authored Oct 28, 2023
2 parents b3db566 + 7d2ae12 commit f45040b
Show file tree
Hide file tree
Showing 17 changed files with 344 additions and 28 deletions.
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 <[email protected]>
*
* 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 <[email protected]>
*
* 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 <[email protected]>
*
* 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 <[email protected]>
*
* 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 <[email protected]>
*
* 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 <[email protected]>
*
* 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 <[email protected]>
*
* 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 <[email protected]>
*
* 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 <[email protected]>
*
* 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 <[email protected]>
*
* 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

0 comments on commit f45040b

Please sign in to comment.