CodeIgniter Forums
Filters just made my head explode? - Printable Version

+- CodeIgniter Forums (
+-- Forum: CodeIgniter 4 (
+--- Forum: CodeIgniter 4 Support (
+--- Thread: Filters just made my head explode? (/thread-77490.html)

Filters just made my head explode? - blaasvaer - 09-07-2020

OK, here's another thing I don't get in CI4 ... Filters!

HOW do I protect AdminControllers using Filters? I want to be able to create AdminControllers that are proctected by 'some' authorization process ... now, I've read the doc (many times), and I simply cannot wrap my head around HOW to APPLY them to my SPECIFIC controllers.

I've created a filter (Authorize.php) just for testing purposes that I placed in the App\Filters folder.
I'm now STARING at the App\Config\Filters.php file where I've added this line (yes, simply monkeying from the docs):

'authorize' => \App\Filters\Authorize::class,

under '$aliases'.

Now, HOW do I apply it to protect all AdminControllers? I simply cannot figure this out from the docs ... yes, I AM stupid, obviously.

If I try to add it to the 'before' array, then it gets called on ALL controllers, sending me into an infinite redirection loop ... HOW do I APPLY it to ONLY AdminControllers? Is this ROUTE based or what?

Please, make the docs CLEAR on HOW to make the filter work in SPECIFIC areas of the app ... this simply wont cut it:

Configuring Filters

Once you’ve created your filters, you need to configure when they get run. This is done in app/Config/Filters.php. This file contains four properties that allow you to configure exactly when the filters run.
Don't know what's wrong with this editor but it seems to bloat replies with white space when previwing posts in edit mode ... yeah, this thing is broken to.

RE: Filters just made my head explode? - InsiteFX - 09-07-2020

To add a filter to a route you do it like so.

PHP Code:

namespace Insitefx\Admin\Config;

 * Insitefx/Name: routes file.

/** @var TYPE_NAME $routes */
$routes->group('', ['namespace' => 'Insitefx\Admin\Controllers'], function($routes)
    $routes->get('dashboard''Dashboard::index', ['filter' => 'login']);

See the filter in the dashboard route? If they are not logged in it will load the login view.