CodeIgniter Forums
Working with Filters Problem, Globals works 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: Working with Filters Problem, Globals works Filters does not work (/showthread.php?tid=75983)



Working with Filters Problem, Globals works Filters does not work - anmar_dev - 04-03-2020

Hi everyone,
Ok her is the issue i been trying to implement filter to check session of user is logged in or not.

App\Filters\AuthFilter.php

PHP Code:
<?php namespace App\Filters;


use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;
use 
CodeIgniter\Filters\FilterInterface;

class 
AuthFilter implements FilterInterface
{
    public function before(RequestInterface $request)
    {
      echo 'Filter.......';
      // if (!session('loggedin')) {
      //   return redirect()->to('/auth/login');
      // }

    }
    //--------------------------------------------------------------------

    public function after(RequestInterface $requestResponseInterface $response)
    {
        // Do something here
    }



Fair enough. simple and direct from the UserGuide. Now on config directory

App/Config/Filters.php

PHP Code:
<?php namespace Config;

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,
        
'AuthFilter' => \App\Filters\AuthFilter::class,
    ];

    
// Always applied before every request
    
public $globals = [
        
'before' => [
            
'AuthFilter'
        
],
        
'after'  => [
            
'toolbar',
            
//'honeypot'
        
],
    ];

    public 
$methods = [];
    public 
$filters = [
        
//'AuthFilter' => ['before' => ['account/*']],
    
];



and for my controller 
App/Controllers/Account.php

PHP Code:
<?php namespace App\Controllers;

use 
CodeIgniter\Controller;
use 
CodeIgniter\Exceptions\PageNotFoundException;
use 
App\Models\UserModel;

class 
Account extends BaseController
{
public 
$data = [];

private 
$userM;

public function 
__construct()
{
}
//--------------------------------------------------------------------
//***
public function index()
{
//var_dump(session()->get());
    
if (!session('loggedin')) {
        echo 
'Not logged in';
    }
    elseif (
session('loggedin')) {
        echo 
'Logged In';
    }



Ok here is the thing 

When i make the filter global like the file currently it works.

However when i deactivate $globals and activate $Filters IS JUST DOES NOT WORK!

I really confused. Huh Confused

And a small note if you don't mind me say. CodeIgniter4 Documentation is no more for beginners or php beginners like CI2 and CI3.  Dodgy  It meant to be understood by a people who really have a good grasp of OOP and OOD patterns.  Dodgy  No offence intended.


RE: Working with Filters Problem, Globals works Filters does not work - anmar_dev - 04-03-2020

Ok i further tried debugging my code. here is what i found
It seems when i use any other method (Besides index and __cunstruct) the filter works...

Please could someone elaborate to my why? why it behaves like this?