Welcome Guest, Not a member yet? Register   Sign In
Redirect Not Working CI4
#11

(This post was last modified: 04-23-2020, 02:55 AM by Leo.)

err, if anyone needs a quick fix for a redirect I just use this:

header('Location: /login');
exit();

it works everywhere so far, I actually have a check login function in my base controller where this is working from.

Beware though, it seems an exit() will prevent any of your "after stuff happens" events or filters from working.
Update: also a check login in base controller doesn't seem like a good idea anymore, so I made it as a public static function in a library
You can see things I made with codeigniter here: itart.pro its not overly impressive as I have very little time to learn.
Reply
#12

(11-07-2019, 07:57 AM)Programaciones Web Wrote: Hello Guys. I started a new Project with CI4 to see the new features and I am struggling trying to redirect between pages.

It's really simple...

I have a Login Controller

PHP Code:
<?php namespace App\Controllers;

use App\Models\Login_Model;

class Login extends BaseController
{

    protected $login_model;

    public function __construct()
    {
        $this->login_model = new Login_Model();
    }

    public function index()
    {
        echo view('login/templates/header');
        echo view('login/dashboard_view');
        echo view('login/templates/footer');
    } 


And a Dashboard Controller


PHP Code:
<?php namespace App\Controllers;

class Dashboard extends BaseController
{

    public function index()
    {
        echo view('admin/templates/header');
        echo view('admin/dashboard_view');
        echo view('admin/templates/footer');
    }






I would like to control the User Sessions. If the user is not log in on the system... Redirect to Login View.
I would like to have this functionality in all my Controllers so I just add it to the BaseController.

PHP Code:
    protected $session;
    
    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);

        
        $this
->session = \Config\Services::session();

        // User Access Control
        if (!isset($_SESSION['user_logged_in'])) {
            return redirect()->to(base_url('/login') );
        }


    


I tried everthing with (/), adding to routes of course. With only redirect(); A lot of things... but seems like something changes in CI4 that I dont get it.


SYSTEMPATH/HTTP\Exceptions\HTTPException.php


Remove de "base_url", like this:

PHP Code:
return redirect()->to('/login'); 

That way works for me.
Reply
#13

(04-20-2020, 02:42 AM)Leo Wrote: err, if anyone needs a quick fix for a redirect I just use this:

header('Location: /login');
exit();

it works everywhere so far, I actually have a check login function in my base controller where this is working from.

Beware though, it seems an exit() will prevent any of your "after stuff happens" events or filters from working.
Update: also a check login in base controller doesn't seem like a good idea anymore, so I made it as a public static function in a library

Thanks for the suggestion. The redirect worked fine using the header('Location...) way. However I still lost session data in the redirected location which was my problem. Have you tried it with session data?
Reply
#14

(05-21-2020, 04:26 AM)jim1001 Wrote: Thanks for the suggestion. The redirect worked fine using the header('Location...) way. However I still lost session data in the redirected location which was my problem. Have you tried it with session data?

That sounds like a Session configuration problem.
Reply
#15

@jim1001

Yes, the sessions work fine. Just put session() in the controller or wherever you are calling the redirect, and put session() whereever you are redirecting to. Also, in my case, I had aggressive session cleaning after each controller, written in an Event - but the exit(), called after header("Location: .... prevented all that, and my session worked in next controller/method/location  
You can see things I made with codeigniter here: itart.pro its not overly impressive as I have very little time to learn.
Reply
#16

(This post was last modified: 06-02-2020, 11:02 AM by jreklund.)

@Chivinsdev and how use this helper? can you paste an example?

Tanks
Reply
#17

(This post was last modified: 07-22-2020, 09:04 AM by jreklund.)

its prettly simple actually
for more clear precise answer u have to add 3 parameters inside the declared constructor
PHP Code:
public function initController($request$response$logger)
{
// Do Not Edit This Line
parent::initController($request$response$logger);
        // for session matters dont forget to declare this
$this->session = \Config\Services::session();


Reply




Theme © iAndrew 2016 - Forum software by © MyBB