-
ggilrandy
Newbie
-
Posts: 4
Threads: 2
Joined: Aug 2021
Reputation:
0
Hi,
Im trying to use filter to all of my controller except login controller
but its doesn't seems to work,
this is my filter config
PHP Code: <?php
namespace Config;
use CodeIgniter\Config\BaseConfig; use CodeIgniter\Filters\CSRF; use CodeIgniter\Filters\DebugToolbar; use CodeIgniter\Filters\Honeypot;
class Filters extends BaseConfig { /** * Configures aliases for Filter classes to * make reading things nicer and simpler. * * @var array */ public $aliases = [ 'csrf' => CSRF::class, 'toolbar' => DebugToolbar::class, 'honeypot' => Honeypot::class, 'loginfilter' => \App\Filters\LoginFilter::class ];
/** * List of filter aliases that are always * applied before and after every request. * * @var array */ public $globals = [ 'before' => [ 'loginfilter' => ['except' => 'Login/*'], // 'csrf', ], 'after' => [ 'toolbar', // 'honeypot', ], ];
/** * List of filter aliases that works on a * particular HTTP method (GET, POST, etc.). * * Example: * 'post' => ['csrf', 'throttle'] * * @var array */ public $methods = [];
/** * List of filter aliases that should run on any * before or after URI patterns. * * Example: * 'isLoggedIn' => ['before' => ['account/*', 'profiles/*']] * * @var array */ public $filters = []; }
this is my filter
PHP Code: <?php
namespace App\Filters;
use CodeIgniter\HTTP\RequestInterface; use CodeIgniter\HTTP\ResponseInterface; use CodeIgniter\Filters\FilterInterface;
class LoginFilter implements FilterInterface { public function before(RequestInterface $request, $arguments = null) { if(empty(session()->login)){ return redirect()->to(base_url()); } }
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null) { // Do something here } }
and this is my routing, im using autorouting. Like Default Class/Function/Parameter Routing
PHP Code: <?php
namespace Config;
// Create a new instance of our RouteCollection class. $routes = Services::routes();
// Load the system's routing file first, so that the app and ENVIRONMENT // can override as needed. if (file_exists(SYSTEMPATH . 'Config/Routes.php')) { require SYSTEMPATH . 'Config/Routes.php'; }
/* * -------------------------------------------------------------------- * Router Setup * -------------------------------------------------------------------- */ $routes->setDefaultNamespace('App\Controllers'); $routes->setDefaultController('Login'); $routes->setDefaultMethod('index'); $routes->setTranslateURIDashes(false); $routes->set404Override(); $routes->setAutoRoute(true);
/* * -------------------------------------------------------------------- * Route Definitions * -------------------------------------------------------------------- */
// We get a performance increase by specifying the default // route since we don't have to scan directories. $routes->get('/', 'Login::index');
/* * -------------------------------------------------------------------- * Additional Routing * -------------------------------------------------------------------- * * There will often be times that you need additional routing and you * need it to be able to override any defaults in this file. Environment * based routes is one such time. require() additional route files here * to make that happen. * * You will have access to the $routes object within that file without * needing to reload it. */ if (file_exists(APPPATH . 'Config/' . ENVIRONMENT . '/Routes.php')) { require APPPATH . 'Config/' . ENVIRONMENT . '/Routes.php'; }
it got me error like ERR_TOO_MANY_REDIRECTS.
help please
-
manager
Member
-
Posts: 74
Threads: 7
Joined: Dec 2020
Reputation:
1
10-29-2021, 10:35 PM
(This post was last modified: 10-30-2021, 06:18 AM by manager.)
Hi. You get error because of redirect loop.
Problem is here:
Code: loginfilter' => ['except' => 'Login/*'],
So when you enter to the login page, user is not authenticated so filter sends him to the base_url().
But in "base_url" your global filter apply again, so filter redirect user to base_url() again and it's an infinite loop.
Solution:
Try to change it like:
Code: 'loginfilter' => ['except' => ['/','login', 'login/*']],
-
ggilrandy
Newbie
-
Posts: 4
Threads: 2
Joined: Aug 2021
Reputation:
0
It works like a charm, thank you mr manager
i get it now so the '/' is the base_url, and i dont include it inside except. so it become infinite loops.
-
ggilrandy
Newbie
-
Posts: 4
Threads: 2
Joined: Aug 2021
Reputation:
0
(10-29-2021, 10:35 PM)manager Wrote: Hi. You get error because of redirect loop.
Problem is here:
Code: loginfilter' => ['except' => 'Login/*'],
So when you enter to the login page, user is not authenticated so filter sends him to the base_url().
But in "base_url" your global filter apply again, so filter redirect user to base_url() again and it's an infinite loop.
Solution:
Try to change it like:
Code: 'loginfilter' => ['except' => ['/','login', 'login/*']],
thank you mr manager, it works like a charm
i get it now, so the '/' it means base_url right? and i dont include it inside except. so it become infinite loop.
-
manager
Member
-
Posts: 74
Threads: 7
Joined: Dec 2020
Reputation:
1
|