Welcome Guest, Not a member yet? Register   Sign In
Redirection in a helper
#1

Hello there,

With ci3, I usually created a helper with a function called "redirect_if_not_logged_in()" that I called at the top of some controllers.

The goal of that function was quite self-explanatory: it checked if the visitor was logged in and if not, redirected them to the login page.

Alas, when trying to do the same with ci4, things get a bit complicated. The "redirect()" function I used to use doesn't actually redirect, but returns an elusive RedirectResponse (completely missing from the doc, by the way).

The only thing I imagine myself doing for now is this:
PHP Code:
// in a helper:

if (!function_exists('redirectIfNotLoggedIn'))
{
    $session = \Config\Services::session();
    if ($session->get('loggedUser') === null)
    {
        return redirect('user/login');
    }
    return null;
}

// in a controller:

public function create()
{
    $redirection redirectIfNotLoggedIn();
    if ($redirection !== null)
    {
        return $redirection;
    }
    // ...


But sincerely, that's pretty much as much code to call the helper and use it than to simple copy-paste the code inside the controller.

How can I redirect FROM the helper?

Nice day to you all!
Reply
#2

Hello,

I think the error is simply about the redirect() call. It has changed - try:
PHP Code:
return redirect()->to('user/login'); 

Code Igniter 4 is a change of paradigm. From my point of view, you should not work this way. I would rather:
- create a method in my custom controller to check if a user is logged in or not,
- call this method in the initController() method which is called by the framework.

The code would be:
PHP Code:
<?php
namespace App\Controllers;

use 
CodeIgniter\Controller;

class 
BaseController extends Controller
{

    public function 
redirectIfNotLoggedIn() {

        
$session = \Config\Services::session();
        if (
$session->get('loggedUser') === null)
        {
            return 
redirect()->to('user/login');
        }

    }

    
/**
     * Constructor.
     */
    
public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
    {
        
// Do Not Edit This Line
        
parent::initController($request$response$logger);

        
//--------------------------------------------------------------------
        // Preload any models, libraries, etc, here.
        //--------------------------------------------------------------------
        // E.g.:
        // $this->session = \Config\Services::session();

        
$this->redirectIfNotLoggedIn();
    }





All (sub-)Controllers inheriting from BaseController would then require to be logged in.

You can also use redirectIfNotLoggedIn() case-by-case to limit the audience in the same Controller (one view requires login, another does'nt).

Regards,
Reply
#3

OK, so I'm not calling a helper, I'm calling $this->redirectIfNotLoggedIn() inherited from the BaseController.

Here is my new code:
PHP Code:
// in BaseController:

protected function redirectIfNotLoggedIn()
{
    if ($session->get('loggedUser') === null)
    {
        $this->response->setHeader('Location'site_url('user/login'));
    }
}

// in a controller:

public function create()
{
    $this->redirectIfNotLoggedIn();
    // ...


But that doesn't work! The response is correctly modified, but the page still displays without the redirection. Any other idea on what I should do to redirect?

Site note:

https://codeigniter4.github.io/userguide...l#redirect states that redirect() takes a string parameter called URI. This might be an error in the doc.
Reply
#4

Your best solution is to use Controller Filters to do a logged in check and redirect if needed.
Reply
#5

YES!!! A thousand times yes! This est exactly and precisely what I needed. Thanks a lot.
Reply
#6
Smile 

(10-14-2019, 10:28 PM)SteeveDroz Wrote: YES!!! A thousand times yes! This est exactly and precisely what I needed. Thanks a lot.

I use this code and it is work,... :-)

Quote:if (empty($email)) {
  header("Location: auth");
  die;
}

Smile Smile Smile
Reply




Theme © iAndrew 2016 - Forum software by © MyBB