Welcome Guest, Not a member yet? Register   Sign In
how to use controllers filters on CI4
#4

The first thing that catches my eye is that your Service you made won't work because you use a different name in the cached part ( authlibrary) than the method is named. It should be:

Code:
public static function authentif($getShared=false){
        if($getShared){
            return self::getSharedInstance('authentif');
        }
        return new \App\Libraries\AuthLibrary();
    }

That explains why your authentif function wasn't working.

With a quick look over the rest I think it's probably fine. However, don't expect echoing anything out of the filter to do anything. It would get lost in the shuffle I imagine. The only thing filters pay attention to is what is returned from the filter. In this case, you're returning nothing. Primarily it looks for either the Request or Response object to be returned from the filter. That's pretty much it. You could probably issue a die() statement and have that work but I can't recall off the top of my head. To make your method work, you would do something like this:

Code:
public function before(RequestInterface $request)
{
    $auth = \Config\Services::authentif();

    if ($auth->isLoggedIn() === false)
    {
       return redirect('login');
    }
}

This way, if the user is not logged in, it returns the Response object (through the redirect() function) which causes an immediate redirect. If they are logged in, then you don't need to do anything.

Does that help?
Reply


Messages In This Thread
how to use controllers filters on CI4 - by casa - 01-10-2017, 02:46 PM
RE: how to use controllers filters on CI4 - by kilishan - 01-11-2017, 09:12 PM



Theme © iAndrew 2016 - Forum software by © MyBB