Welcome Guest, Not a member yet? Register   Sign In
Apply filter for all functions inside a controller
#1

(This post was last modified: 08-17-2020, 07:30 AM by eleumas.)

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

(This post was last modified: 08-19-2020, 12:09 PM by mlurie.)

(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

(This post was last modified: 08-20-2020, 03:23 AM by eleumas.)

(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




Theme © iAndrew 2016 - Forum software by © MyBB