Welcome Guest, Not a member yet? Register   Sign In
Exclude uri in controller filters
#1

Hi guys,


I have a filter for 'account/*', but it is too general and will also match 'api/account'. I wish to exclude any uri with api from matching.


I tried to do the following code in Config/Filters.php, but it didn't work as I would have expected. Is there a simple way that I'm missing?


Code:
/**
* List of filter aliases that are always
* applied before and after every request.
*
* @var array
*/
public $globals = [
  'before' => [
    'isLoggedIn' => ['except' => 'api/*'],
  ],
  'after'  => [
    'toolbar',
  ],
];

/**
* List of filter aliases that should run on any
* before or after URI patterns.
*
* Example:
* 'isLoggedIn' => ['before' => ['account/*', 'profiles/*']]
*
* @var array
*/
public $filters = [
  'isLoggedIn' => ['before' => ['account/*', 'profiles/*']]
];
Reply
#2

(This post was last modified: 02-17-2022, 01:46 PM by iRedds.)

PHP Code:
//routes
$routes->get('accounts/(:any)''Home::account/$1');
$routes->get('api/accounts/(:any)''Home::account/$1/api');

//controller
    public function account($i$api null)
    {
        dd($i$api$_POST);
    }

// filter 
    public function before(RequestInterface $request$arguments null)
    {
        $_POST['xxx'] = 123;
    }

//filters config
    public $filters = [
        'hp' => ['before' => ['accounts/*']]
    ];

    public $globals = [
        'before' => [
            'hp' => ['except' => ['api/*']] 

for globals + filters or only globals or only filters
Code:
http://localhost:8080/accounts/1
$i string (1) "1"
$api null
$_POST array (1)

http://localhost:8080/api/accounts/1
$i string (1) "1"
$api string (3) "api"
$_POST array (0)
Reply
#3

Many thanks, exactly what I wanted!
Reply
#4

Thanks! it was just what I needed
Reply




Theme © iAndrew 2016 - Forum software by © MyBB