• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Apply filter for all functions inside a controller

#1
Hi! I created a filter for auth but i would like apply this filter for all methods inside the controller.

This is my routes:
PHP Code:
$routes->get('articles''Articles',['filter' => 'auth']);
$routes->get('articles/(:any)''Articles',['filter' => 'auth']); 

Is possible do it with a unique route? Someone can help me please? Thanks.
Reply

#2
(08-17-2020, 04:25 AM)eleumas Wrote: Hi! I created a filter for auth but i would like apply this filter for all methods inside the controller.

This is my routes:
PHP Code:
$routes->get('articles''Articles',['filter' => 'auth']);
$routes->get('articles/(:any)''Articles',['filter' => 'auth']); 

Is possible do it with a unique route? Someone can help me please? Thanks.

Check Grouping Routes in the CodeIgniter 4 documentation here: https://codeigniter.com/user_guide/incom...ing-routes.  Apply your filter to the group.
Reply

#3
(08-19-2020, 12:08 PM)mlurie Wrote:
(08-17-2020, 04:25 AM)eleumas Wrote: Hi! I created a filter for auth but i would like apply this filter for all methods inside the controller.

This is my routes:
PHP Code:
$routes->get('articles''Articles',['filter' => 'auth']);
$routes->get('articles/(:any)''Articles',['filter' => 'auth']); 

Is possible do it with a unique route? Someone can help me please? Thanks.

Check Grouping Routes in the CodeIgniter 4 documentation here: https://codeigniter.com/user_guide/incom...ing-routes.  Apply your filter to the group.

I tried this code, but in order to work i have to add the name of group to url and this is a problem for me.

My url : articles/create 
With filter group: mygroup/articles/create

PHP Code:
$routes->group('mygroup', ['filter' => 'auth'], function($routes)
{
    $routes->resource('dashboard');
    $routes->resource('articles');
}); 

I would like apply the filter to controller and all methods if possible with a unique route. 
Is it possible? Some code for example? Thanks. Blush
Reply

#4
(08-20-2020, 02:18 AM)eleumas Wrote:
(08-19-2020, 12:08 PM)mlurie Wrote:
(08-17-2020, 04:25 AM)eleumas Wrote: Hi! I created a filter for auth but i would like apply this filter for all methods inside the controller.

This is my routes:
PHP Code:
$routes->get('articles''Articles',['filter' => 'auth']);
$routes->get('articles/(:any)''Articles',['filter' => 'auth']); 

Is possible do it with a unique route? Someone can help me please? Thanks.

Check Grouping Routes in the CodeIgniter 4 documentation here: https://codeigniter.com/user_guide/incom...ing-routes.  Apply your filter to the group.

I tried this code, but in order to work i have to add the name of group to url and this is a problem for me.

My url : articles/create 
With filter group: mygroup/articles/create

PHP Code:
$routes->group('mygroup', ['filter' => 'auth'], function($routes)
{
    $routes->resource('dashboard');
    $routes->resource('articles');
}); 

I would like apply the filter to controller and all methods if possible with a unique route. 
Is it possible? Some code for example? Thanks. Blush

Auto-routing only works if you include the class name as part of the path.  If you don't want the class name (group name) in the URL, you will need to manually specify the Controller\method like this:
PHP Code:
$routes->group('mygroup', ['filter' => 'auth'], function($routes) {       
     
$routes->add('dashboard', 'Mygroup::dashboard');
     
$routes->add('articles', 'Mygroup::articles');

Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.