Welcome Guest, Not a member yet? Register   Sign In
Redirect from BaseController
#1

Hey gang, I am trying to redirect from my BaseController with a function called by the child Controller but it doesn't seem to work. Here is what I have.

BaseController.php

PHP Code:
class BaseController extends Controller
{
    /**
    * @var \CodeIgniter\Session\Session|mixed|null
    */
    protected $session;

    public function initController(RequestInterface $requestResponseInterface $responseLoggerInterface $logger)
    {
        // Do Not Edit This Line
        parent::initController($request$response$logger);
        $this->session = \Config\Services::session();
    }

    public function check_something()
    {
        // redirect if something in session doesn't check out
        if (empty($this->session->get('something'))) redirect()->to('/somewhere');
    }



ChildController.php

PHP Code:
class ChildController extends BaseController
{
    public function index(){
        // check something first
        $this->check_something();

        // if we are not redirected...
        echo 'everything checked out!';
    }



To answer the question: Yes, the session variable 'something' has not been created and empty($this->session->get('something')) returns true;

Any help greatly appreciated. Thank you!
Reply
#2

You should look into controller filters for this type of a situation.
Reply
#3

(11-12-2021, 11:10 AM)kilishan Wrote: You should look into controller filters for this type of a situation.

BINGO!

Thanks!
Reply
#4

redirect() is the most changed helper function.

3.x https://codeigniter.com/userguide3/helpe...l#redirect
4.x https://codeigniter.com/user_guide/gener...l#redirect

In CI4, it returns RedirectResponse, and you must return it.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB