We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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


81.1k
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.



81.1k

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



11.0k
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.



81.1k

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.