the redirect does not work for the filter.
below is my sample code for staff authentication,if i use raw head location which i commented below, it works well.
in my observation, seems there is an issue with the sent out header.
PHP Code:
<?php namespace App\Filters;
use Config\Services;
use CodeIgniter\Events\Events;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Filters\FilterInterface;
class StaffAuth implements FilterInterface
{
public function before(RequestInterface $request)
{
$auth = Services::Auth('Staff');
if ($request->uri->getSegment(3) != 'login')
{
if (!$auth->isLoggedIn())
{
if ($request->isAJAX())
{
$this->jsonresponse = [
'status' => 'error',
'redirectUrl' => route_to('StaffLogin'),
];
return Services::response()->setJson($this->jsonresponse);
}
//return redirect(route_to('StaffLogin').'?redirect_to='.urlencode($request->detectPath('REQUEST_URI')));
//header('Location: ' . site_url(route_to('StaffLogin')), TRUE, 302);
//exit;
return redirect()->route('StaffLogin');
}
Events::trigger('Auth.loggedin', []);
}
}
//--------------------------------------------------------------------
public function after(RequestInterface $request, ResponseInterface $response)
{
// Do something here
}
}