我是Zend Framework 2的新手。
我已经创建了一个"Admin"模块,还创建了"UserController"one_answers"AlbumController"。UserController包含登录和注销操作。AlbumController包含正常的CRUD和欢迎动作。
现在,当我直接访问http://localhost/websites/zendtest/public/admin/login
时,我如何在欢迎页面重定向页面,当我已经登录。
并且,同样的问题是,当我直接访问http://localhost/websites/zendtest/public/admin/album/welcome
时,我如何在登录页面上重定向页面,当我还没有登录时。
我还有另一个问题,我如何在布局中使用控制器动作值。因为我有创建菜单的menucontroller。所以我需要在布局中从菜单控制器返回数组。创建动态菜单。
那么,我该怎么做呢?
我想你不想在ZF2文档中解释。要恢复,你必须测试会话,并重定向,使用redirect
插件:
$this->redirect()->toRoute('actionname');
重定向插件的用法如下:->toRoute($route, array $params = array(), array $options = array());
重定向到指定的路由,使用提供的$params
和$options
来组装URL。
要验证用户,如acl
插件的旧ZF,请转到这个页面
和对于最后一个问题,您可以使用(对于ZF2.1.3)
在视图中传递一些值$layout = $this->layout();
$layout->myvar = $mymenuarray;
并在视图中使用
检索它$myvar...
我不知道你是如何验证用户的,但如果你使用ZendAuth
,那么你可以这样做:
public function loginAction() {
$authService = new ZendAuthenticationAuthenticationService();
$authService->setStorage(new ZendAuthenticationStorageSession('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
}
}
欢迎动作:
public function welcomeAction() {
$authService = new ZendAuthenticationAuthenticationService();
$authService->setStorage(new ZendAuthenticationStorageSession('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
}
}
如果你希望在许多页面上执行上述操作,那么上面的操作可能相当重复,因此你应该考虑使其可重用,例如,通过从服务管理器(工厂)获取认证服务。