Search code examples
phpregexroutescodeigniter-4

Get current route rule


I'm trying to get the current route rule in my filter.

$routes->group('creditCalculator', ['namespace' => '\Modules\CreditCalculator', "filter" => 'auth:recht1', 'extra_credentials' => array('as'=>'test', 'menu'=>'profile')], function($routes){
$routes->post('/', 'CreditCalculator_Controller::index');
$routes->get('test', 'CreditCalculator_Controller::test', ["filter"=>"auth:test1", 'test2' => 'test3']);
$routes->get('test2/(:num)/(:any)', 'CreditCalculator_Controller::test2/$1/$2', ["as"=>'named_route', "filter"=>"auth:recht3", 'right' => 'right1']);

In the "auth" filter I would like to get access to the current matched route to get the routeOptions.

Target: We want to set some rules for each route. Afterwards we want to check these settings in our Filter. Example: Login-Type. So we can check each route, which login type is needed for this current route.

We are using Codeigniter 4 HMVC.

Any suggestions? The documentation of CI4 and the api-documentation didn't get me far. I tried several methods of the router, but no method gave me the current matched route.

What I need as return: /test/(:num)/(:any) and the options array of that route

So far - our filter:

<?php 

namespace App\Filters;

use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Filters\FilterInterface;

class filterTest implements FilterInterface {
public function before(RequestInterface $request, $arguments = null) {
    $list    = \Config\Services::routes();

    $routes  = $list->getRoutesOptions();
}

public function after(RequestInterface $request, ResponseInterface $response, $arguments = null) {

}
}

Solution

  • I just included the wrong Service. Over Service Router I can access the 'getMatchedRouteOptions' method.

    $router       = \Config\Services::router();
    $routeOptions = $router->getMatchedRouteOptions();