CodeIgniter Forums
route filters does not work - 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: route filters does not work (/showthread.php?tid=77726)



route filters does not work - tmtuan - 10-11-2020

Hi, i'm using CI 4.0.4 for my project, i'm working on permission check with routes and filter in it, but it seem to be not woking right. My code look like this:
- In App/Filter i declare an filter alias

[Image: ci-error.png]

- in my Acp module i have 2 routes group like this

[Image: ci-error-1.png]


- and this is my filter class, i'm also print out and exit the code to test

[Image: ci-error-2.png]

- the problem was, the router just call the fillter class once, it only go to the filter class if i visit the permission route group. it run ok like this

[Image: ci-error-3.png]

- and it not working when i'm going to the second route url

[Image: ci-error-4.png]

does any one have the same problem? Can anyone help me with this please


RE: route filters does not work - captain-sensible - 10-11-2020

this is my Filters.php at app/Config

Code:
use CodeIgniter\Config\BaseConfig;

class Filters extends BaseConfig
{
    // Makes reading things below nicer,
    // and simpler to change out script that's used.
    public $aliases = [
        'csrf'     => \CodeIgniter\Filters\CSRF::class,
        'toolbar'  => \CodeIgniter\Filters\DebugToolbar::class,
        'honeypot' => \CodeIgniter\Filters\Honeypot::class,
        'myfilter'=> \App\Filters\MyFilter::class,
        'myfilter2'=> \App\Filters\MyFilter2::class,
        'myfilter3'=> \App\Filters\MyFilter3::class,
        'myfilter4'=> \App\Filters\MyFilter4::class,
        'myfilter5'=> \App\Filters\MyFilter5::class,
        'myfilter6'=> \App\Filters\MyFilter6::class,
        'myfilter7'=> \App\Filters\MyFilter7::class,
        'myfilter8'=> \App\Filters\MyFilter8::class,
        'myfilter9'=> \App\Filters\MyFilter9::class
        
        
        
        
        
    ];

    // Always applied before every request i removed // that was in front of honeypot and csrf below
    public $globals = [
        'before' => [
            'honeypot',
             'csrf',
        ],
        'after'  => [
            'toolbar',
            //'honeypot'
        ],
    ];

    // Works on all of a particular HTTP method
    // (GET, POST, etc) as BEFORE filters only
    //     like: 'post' => ['CSRF', 'throttle'],
    public $methods = [];

    // List filter aliases and any before/after uri patterns
    // that they should run on, like:
    //    'isLoggedIn' => ['before' => ['account/*', 'profiles/*']],
    public $filters = [
    
    
    'myfilter' => ['before' => ['newblog']], //newblog is a  route
    'myfilter2' => ['before' => ['editBlogs']],//editBlogs is a route
    'myfilter3' => ['before' => ['removeBlog']], //etc etc
    'myfilter4' => ['before' => ['addPortfolio']],
    'myfilter5' => ['before' => ['delPortfolio']],
    'myfilter6' => ['before' => ['portfolioForm']],
    'myfilter7' => ['before' => ['delPortfolioForm']],
    'myfilter8' => ['before' => ['form4']],
    'myfilter9' => ['before' => ['admin']]
    
    
    
    
    
    ];
}

maybe i'm missing something but i found that to work you need a separate filter class for every defined route


this is an extract of Routes.php so you can match it to above:

Code:
$routes->get('delPortfolio','Portfolio::delPortfolio');


you will see myfilter5.php matches the above route //myfilter > myfilter9 yeah no imagination i have

so the get request /delPortfolio calls the class method delPortfolio of class Portfolio .

what i'm checking on this is that admin is logged in in order to access what the method does


RE: route filters does not work - ojmichael - 10-11-2020

That seems insane..

What happens if you do something like this?

PHP Code:
'myfilter' => ['before' => ['newblog''editBlogs''removeBlog']], //etc etc 



RE: route filters does not work - captain-sensible - 10-12-2020

(10-11-2020, 10:36 PM)ojmichael Wrote: That seems insane..

What happens if you do something like this?

PHP Code:
'myfilter' => ['before' => ['newblog''editBlogs''removeBlog']], //etc etc 
good suggestion , thats another thing i will look at :^)


RE: route filters does not work - tmtuan - 10-12-2020

(10-11-2020, 10:36 PM)ojmichael Wrote: That seems insane..

What happens if you do something like this?

PHP Code:
'myfilter' => ['before' => ['newblog''editBlogs''removeBlog']], //etc etc 


this way good, and it work perfect, i've done this before i wrote my code like in this post. The reason i dont want to write this way is there're a lot of module that we want to develop in the future and i want to keep all change in module only, and team member can only work in the selected module (folder also) they dont have permission to overwirte the app/config so i'm trying the other way to make it


RE: route filters does not work - captain-sensible - 10-14-2020

(10-11-2020, 10:36 PM)ojmichael Wrote: That seems insane..

What happens if you do something like this?

PHP Code:
'myfilter' => ['before' => ['newblog''editBlogs''removeBlog']], //etc etc 


 The result was sanity was restored and your rep was increased. Good job there is no -ve rep on this site or mine would have gone down; but i guess we all get those code moments ?