Skip to content

Commit

Permalink
Merge pull request #1523 from natanfelles/resource_filter
Browse files Browse the repository at this point in the history
Check if the matched route regex is filtered
  • Loading branch information
jim-parry authored Nov 22, 2018
2 parents c527ad4 + a6c9737 commit 3a3218f
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 3 deletions.
2 changes: 1 addition & 1 deletion system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ public function displayPerformanceMetrics(string $output): string
* @param RouteCollectionInterface $routes An collection interface to use in place
* of the config file.
*
* @return array
* @return string
*/
protected function tryToRouteIt(RouteCollectionInterface $routes = null)
{
Expand Down
4 changes: 2 additions & 2 deletions system/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ public function handle(string $uri = null)

if ($this->checkRoutes($uri))
{
if ($this->collection->isFiltered($uri))
if ($this->collection->isFiltered($this->matchedRoute[0]))
{
$this->filterInfo = $this->collection->getFilterForRoute($uri);
$this->filterInfo = $this->collection->getFilterForRoute($this->matchedRoute[0]);
}

return $this->controller;
Expand Down
104 changes: 104 additions & 0 deletions tests/system/Router/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,110 @@ public function testRouteWorksWithFilters()

//--------------------------------------------------------------------

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/1247
*/
public function testGroupedResourceRoutesWithFilters()
{
$group = [
'api',
[
'namespace' => 'App\Controllers\Api',
'filter' => 'api-auth',
],
function (RouteCollection $routes) {
$routes->resource('posts', [
'controller' => 'PostController',
]);
},
];

// GET
$this->collection->setHTTPVerb('get');

$this->collection->group(...$group);

$router = new Router($this->collection);

$router->handle('api/posts');

$this->assertEquals('\App\Controllers\Api\PostController', $router->controllerName());
$this->assertEquals('index', $router->methodName());
$this->assertEquals('api-auth', $router->getFilter());

$router->handle('api/posts/new');

$this->assertEquals('\App\Controllers\Api\PostController', $router->controllerName());
$this->assertEquals('new', $router->methodName());
$this->assertEquals('api-auth', $router->getFilter());

$router->handle('api/posts/50');

$this->assertEquals('\App\Controllers\Api\PostController', $router->controllerName());
$this->assertEquals('show', $router->methodName());
$this->assertEquals('api-auth', $router->getFilter());

$router->handle('api/posts/50/edit');

$this->assertEquals('\App\Controllers\Api\PostController', $router->controllerName());
$this->assertEquals('edit', $router->methodName());
$this->assertEquals('api-auth', $router->getFilter());

// POST
$this->collection->setHTTPVerb('post');

$this->collection->group(...$group);

$router = new Router($this->collection);

$router->handle('api/posts');

$this->assertEquals('\App\Controllers\Api\PostController', $router->controllerName());
$this->assertEquals('create', $router->methodName());
$this->assertEquals('api-auth', $router->getFilter());

// PUT
$this->collection->setHTTPVerb('put');

$this->collection->group(...$group);

$router = new Router($this->collection);

$router->handle('api/posts/50');

$this->assertEquals('\App\Controllers\Api\PostController', $router->controllerName());
$this->assertEquals('update', $router->methodName());
$this->assertEquals('api-auth', $router->getFilter());

// PATCH
$this->collection->setHTTPVerb('patch');

$this->collection->group(...$group);

$router = new Router($this->collection);

$router->handle('api/posts/50');

$this->assertEquals('\App\Controllers\Api\PostController', $router->controllerName());
$this->assertEquals('update', $router->methodName());
$this->assertEquals('api-auth', $router->getFilter());

// DELETE
$this->collection->setHTTPVerb('delete');

$this->collection->group(...$group);

$router = new Router($this->collection);

$router->handle('api/posts/50');

$this->assertEquals('\App\Controllers\Api\PostController', $router->controllerName());
$this->assertEquals('delete', $router->methodName());
$this->assertEquals('api-auth', $router->getFilter());
}

//--------------------------------------------------------------------

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/1240
*/
Expand Down

0 comments on commit 3a3218f

Please sign in to comment.