• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Working with Filters Problem, Globals works Filters does not work

#1
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.
I enjoy coding in codeigniter4. SurveyIgniter a project where i learn about codeigniter4.
Reply

#2
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?
I enjoy coding in codeigniter4. SurveyIgniter a project where i learn about codeigniter4.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.