0
votes

I am new in Zend Framework 2.

I have created a "Admin" module and also created "UserController" and "AlbumController". UserController contains login and logout actions. AlbumController contains normal CRUD and welcome action.

Now, How can i redirect page on welcome page when i directly access http://localhost/websites/zendtest/public/admin/login when i already loggedin.

And, same question is How can i redirect page on login page when i directly access http://localhost/websites/zendtest/public/admin/album/welcome when i already not loggedin.

Can any one suggest me solution for that?

I also have another question, how can i use controller action value in layout.phtml, because i have MenuContoller for creating menu. So i need return array from MenuController in layout.phtml for create dynamic menu.

So, how can i do that?

2
Your question is very broad. You might be looking for something called Redirect. Also as you're new to ZF2, please show that you've done your homework and show what you've tried so far.hakre
Are you using zfcUser or another module? or directly with the Auth setup?Andrew
no i doesn't use zfcUser, but i need to globally check if user Logged In or not, if already logged in and user need to access login action then he redirect on welcome page. if not logged in and user access welcome or other inner page like account page then he redirect on login pageJimesh Gajera

2 Answers

0
votes

I think that you wan't is explained in the ZF2 doc. To resume, you have to test for session, and to redirect, using redirect plugin :

$this->redirect()->toRoute('actionname');

The redirect plugin is used like this:

->toRoute($route, array $params = array(), array $options = array());

Redirects to a named route, using the provided $params and $options to assembled the URL.

To authenticate a user like the acl plugin for older ZF, go to this page

And for the last question, you can past some value in the view using (for the ZF2.1.3)

$layout = $this->layout();
$layout->myvar = $mymenuarray;

And retrieve it in the view using

$myvar...
0
votes

I don't know how you are authenticating users, but if you are using Zend\Auth, then you can do something like this:

public function loginAction() {
    $authService = new \Zend\Authentication\AuthenticationService();
    $authService->setStorage(new \Zend\Authentication\Storage\Session('user', 'details'));

    if ($authService->hasIdentity()) {
        // User is already logged in; redirect to welcome page
        return $this->redirect()->toRoute('welcome'); // Assumes that you have a 'welcome' route
    }
}

And for the welcome action:

public function welcomeAction() {
    $authService = new \Zend\Authentication\AuthenticationService();
    $authService->setStorage(new \Zend\Authentication\Storage\Session('user', 'details'));

    if (!$authService->hasIdentity()) {
        // User is not logged in; redirect to login page
        return $this->redirect()->toRoute('login'); // Assumes that you have a 'login' route
    }
}

The above can be quite repetitive if you wish to do it on many pages, so you should consider making it reusable, e.g. by fetching the authentication service from the service manager (a factory).