2

I have code in my controller

public function __construct()
{
   return redirect()->to('/auth');
   $this->validation =
     \Config\Services::validation();
   $this->title = 'Header Menu';
   $this->controller = 'user';
}

public function index()
{
  $data = [
     'title_prefix' => 'Profil',
     'title' => $this->title,
     'controller' => $this->controller,
     'button_add' => false,
     'validation' => $this->validation
    ];

  return view('user/index', $data);
}

it still show view('user/index'). How to get to return redirect()->to('/auth') in __construct() ? sorry i'm not speaking english well

3 Answers3

3

It is an expected behavior that redirect() doesn't work inside a constructor.

redirect() in CI4 doesn't just set headers but return a RedirectResponse object.
Problem is : while being in the constructor of your controller, you can't return an instance of something else. You're trying to construct a Controller not a RedirectResponse.

Good practice is to use Controller Filters

Or you could add the redirect() inside your index function if there's only at this endpoint that you would like to redirect the user.

Here's an example of the filter that would fit your need :
Watch out your CI version. The parameter $arguments is needed since 4.0.4. Otherwise you have to remove it

<?php

namespace App\Filters;

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

class AuthFilter implements FilterInterface {

    public function before(RequestInterface $request, $arguments = null) {
        return redirect()->to('/auth');
    }

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

    public function after(RequestInterface $request, ResponseInterface $response, $arguments = null) {
    }

}

And in your app/Config/Filters edit those 2 variables in order to activate your filter :

public $aliases = [
        'auth' => \CodeIgniter\Filters\AuthFilter::class,
    ];
public $globals = [
        'before' => ['auth' => 'the_routes_you_want_to_redirect']
    ];

You might want to check this thread aswell : https://forum.codeigniter.com/thread-74537.html

ViLar
  • 1,054
  • 10
  • 18
0

you can't use redirect() inside __construct() function or initController() directly. But you can use $response parameter in initController() or $this->response attribute.

https://stackoverflow.com/a/65814413/1462903

in controller class

<?php namespace App\Controllers;

class Data extends BaseController
{
    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);
        
        if($this->session->get('is_loggedin') !== true){
            $response->redirect(base_url('login')); // or use $this->response->redirect(base_url('login'));
        } 
        
    }
    
    public function index()
    {
        return view('dashboard');
    }
}

in BaseController class

/**
 * 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.
    //--------------------------------------------------------------------

    $this->session = \Config\Services::session();
}
0
 header("Location: ".site_url().'/dashboard');
 die();
Yunat Amos
  • 93
  • 4