CodeIgniter Forums
Multiple filters set on a route in routes.php - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Multiple filters set on a route in routes.php (/showthread.php?tid=90761)



Multiple filters set on a route in routes.php - abatrans - 04-29-2024

How would I define different multiple before and after filters for a route in the routes.php config file.
When I define the filters
PHP Code:
$routes->get'beelevels''Beelevels::index', [
   'filter' => [
      'authorize',
      'permissions:view,lookups',
   ]
] ); 
the filter are attached to both before and after, if I define the filters
PHP Code:
$routes->get'beelevels''Beelevels::index', [
   'filter' => [
      'before' => [
         'authorize',
         'permissions:view,lookups',
      ]
   ]
] ); 
I get an exception
Code:
CodeIgniter\Filters\Filters::enableFilter(): Argument #1 ($name) must be of type string, array given, called in ....\system\Filters\Filters.php on line 547
at SYSTEMPATH\Filters\Filters.php:486

The documentation on multiple routes is a bit light.


RE: Multiple filters set on a route in routes.php - kenjis - 04-29-2024

You cannot. There is no such feature.


RE: Multiple filters set on a route in routes.php - luckmoshy - 04-30-2024

You can achieve this by using a condition ( if) or (switch) in a single filter class and property but not by that


RE: Multiple filters set on a route in routes.php - abatrans - 04-30-2024

(04-29-2024, 05:43 PM)kenjis Wrote: You cannot. There is no such feature.

Perhaps add it to the wish list for the next release.

In my case I would like the filters to only be added to the before and not to the after as well. If I needed to add filters to the AFTER event my App/Config/Filters.php file becomes a nightmare to maintain.


RE: Multiple filters set on a route in routes.php - kenjis - 04-30-2024

Why don't you write filters that work only in before?


RE: Multiple filters set on a route in routes.php - abatrans - 04-30-2024

(04-30-2024, 02:20 AM)kenjis Wrote: Why don't you write filters that work only in before?

I have 145 routes and counting and it just seem easier to add the filters to the route definitions rather than adding the routes to config/filters.php.  It looks like I will have to re-evaluate what the best method for my application will be. Perhaps go to a method based i.s.o. route based.


RE: Multiple filters set on a route in routes.php - kenjis - 04-30-2024

I meant, why don't you create your own filters that work only in before?
That is, no code in the after() method.