Welcome Guest, Not a member yet? Register   Sign In
Filter controller without authentication
#3

(This post was last modified: 08-25-2020, 07:06 AM by pippuccio76.)

(08-25-2020, 03:42 AM)InsiteFX Wrote: It would be something like this not tested.

PHP Code:
<?php namespace Your\Name\Space;

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

class 
YourFilterName 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 (! isset($_SESSION['user_id']))
        {
            
session()->set('redirect_url'current_url());
            return 
redirect('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 $requestResponseInterface $response$arguments null)
    {

    }

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

}   // End of YourFilterName Class.

/**
 * -----------------------------------------------------------------------
 * Filename: YourFilterName.php
 * Location: ./app/Filters/YourFilterName.php
 * -----------------------------------------------------------------------
 */ 

Put your own namespace and class names in.


Why this : session()->set('redirect_url', current_url());  ?

Now how can use it in route for every controller ? for example for User controller must be for every method except login
Reply


Messages In This Thread
RE: Filter controller without authentication - by pippuccio76 - 08-25-2020, 07:03 AM



Theme © iAndrew 2016 - Forum software by © MyBB