Welcome Guest, Not a member yet? Register   Sign In
Filter's dont work
#1

(This post was last modified: 11-29-2020, 02:21 AM by pippuccio76.)

HI , i try to implement a filter to control if admin is authenticated :

Code:
<?php namespace App\Filters;

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

class AdminFiltersAuth implements FilterInterface
{
    /**
     * Do whatever processing this filter needs to do.
     * By default it should not return anything during
     * normal execution. However, when an abnormal state
     * is found, it should return an instance of
     * CodeIgniter\HTTP\Response. If it does, script
     * execution will end and that Response will be
     * sent back to the client, allowing for error pages,
     * redirects, etc.
     *
     * @param \CodeIgniter\HTTP\RequestInterface $request
     * @param array|null                         $params
     *
     * @return mixed
     */
    public function before(RequestInterface $request, $params = null)
    {
        // if no user is logged in then send them to the login form
        if (!session()->get('admin_id'))
        {           
            session()->set('redirect_url', current_url());
           
            return redirect()->to( base_url().'/admin/login');
        }
    }

    //--------------------------------------------------------------------

    /**
     * Allows After filters to inspect and modify the response
     * object as needed. This method does not allow any way
     * to stop execution of other after filters, short of
     * throwing an Exception or Error.
     *
     * @param \CodeIgniter\HTTP\RequestInterface  $request
     * @param \CodeIgniter\HTTP\ResponseInterface $response
     * @param array|null                          $arguments
     *
     * @return void
     */
    public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
    {

    }

    //--------------------------------------------------------------------

}   // End of YourFilterName Class.


i want filter every controller's method except login to prevent loop redirect

in Config/Filter this is my code:

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,
        'adminFiltersAuth' => \App\Filters\AdminFiltersAuth::class,
        'adminFiltersNoAuth' => \App\Filters\AdminFiltersNoAuth::class,
        'showSessionFilter' => \App\Filters\ShowSessionFilter::class,
    ];

    // Always applied before every request
    public $globals = [
        'before' => [
            //'showSessionFilter'
            //'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 = [

        'adminFiltersAuth' => ['before' => ['admin/*'] ],
        'adminFiltersAuth' => ['except' => ['admin/login']],
        'adminFiltersNoAuth' => ['before' => ['admin/login']],



    ];
}

but if i go to ade admin/changeEmail if the admin are not logged in mut im not redirected . 
in route if i insert this code :

Code:
$routes->add('admin/changeEmail', 'Admin::changeEmail',['filter'=>'adminFiltersAuth']);

it works fine  but in some controller i want to filter every method and is too boring write a route with filter for every metods
Why ?
Reply


Messages In This Thread
Filter's dont work - by pippuccio76 - 11-29-2020, 02:18 AM
RE: Filter's dont work - by InsiteFX - 11-29-2020, 01:25 PM
RE: Filter's dont work - by pippuccio76 - 11-30-2020, 02:25 AM
RE: Filter's dont work - by tgix - 11-29-2020, 11:12 PM
RE: Filter's dont work - by pippuccio76 - 12-03-2020, 01:28 PM
RE: Filter's dont work - by InsiteFX - 12-01-2020, 01:49 PM
RE: Filter's dont work - by pippuccio76 - 12-02-2020, 04:49 AM
RE: Filter's dont work - by Matleyx - 12-02-2020, 09:06 AM
RE: Filter's dont work - by pippuccio76 - 12-02-2020, 10:03 AM
RE: Filter's dont work - by tgix - 12-03-2020, 01:37 PM
RE: Filter's dont work - by pippuccio76 - 12-05-2020, 01:48 AM
RE: Filter's dont work - by tgix - 12-05-2020, 02:01 AM
RE: Filter's dont work - by pippuccio76 - 12-06-2020, 02:27 AM
RE: Filter's dont work - by tgix - 12-06-2020, 07:14 AM
RE: Filter's dont work - by InsiteFX - 12-06-2020, 07:40 AM
RE: Filter's dont work - by pippuccio76 - 12-06-2020, 10:11 AM
RE: Filter's dont work - by SkyRipper - 12-07-2020, 10:11 PM



Theme © iAndrew 2016 - Forum software by © MyBB