Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

simple validation

Hello how can I make a simple validation If the session variable does not exist redirected to the login view Can this function be the index.volt? If you may not use or routing Acl

In your controller:

 if (!$this->session->has('yourSessionLoginVariable')) $this->response->redirect(["for" => "login"]);
 else; //user is logged in...


31.5k
edited Nov '16

I write to all controllers

In your controller:

if (!$this->session->has('yourSessionLoginVariable')) $this->response->redirect(["for" => "login"]);
else; //user is logged in...

Dispatcher event beforeExecuteAction.



31.5k

By the time I will use this code. This in the main index.volt

<?PHP
       if ($this->session->has("userId")) {
            // Retrieve its value
            $name = $this->session->get("userId");
        }else{
            header ("Location:". $this->url->get('user/login'));
        }
    ?>

Because I was already over the time to make corrections of the thesis. but pondre in the days following the event



7.8k
Accepted
answer
edited Nov '16

In your controller:

 if (!$this->session->has('yourSessionLoginVariable')) $this->response->redirect(["for" => "login"]);
 else; //user is logged in...

if put this code in ControllerBase and extend it to other controller it will check everytime when you access to other controller

header ("Location:". $this->url->get('user/login'));

There is redirect...

The most correct way is to use events manager and attach it to dispatcher. Read docs.



31.5k

Yes, I know but I do not have much time to complete the project, I'll do it later

``` header ("Location:". $this->url->get('user/login')); ```

There is redirect...

The most correct way is to use events manager and attach it to dispatcher. Read docs.