CodeIgniter Forums
Redirect to another controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: Redirect to another controller (/thread-75102.html)



Redirect to another controller - ARAmiss - 12-21-2019

Hello.
There is my custom filter:
PHP Code:
<?php

namespace App\Filters;

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

class 
Authorization implements FilterInterface
{
    public function before(RequestInterface $request)
    {
        $auth service('auth');
        if (! $auth->isAuth())
            return redirect()->route('403');
    }
    
    
public function after(RequestInterface $requestResponseInterface $response)
    {
        
    
}


It's work fine: if user is not authorized at some page he will be redirected to page 403.

Question: is there any way to do the same without physically redirect?

For example:
PHP Code:
<?php

namespace App\Filters;

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

class 
Authorization implements FilterInterface
{
    public function before(RequestInterface $request)
    {
        $error_403_Controller = new \App\Controllers\Error403Controller();
        return $error_403_Controller->index();
        exit;
    }
    
    
public function after(RequestInterface $requestResponseInterface $response)
    {
        
    
}

It's not working fine.

i need something like this:
redirect()->controller('403');

or this:
return loadController('403');