Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在zend framework 2中使用URL重定向进行身份验证_Php_Zend Framework2 - Fatal编程技术网

Php 在zend framework 2中使用URL重定向进行身份验证

Php 在zend framework 2中使用URL重定向进行身份验证,php,zend-framework2,Php,Zend Framework2,我是Zend Framework 2的新手 我创建了一个“管理”模块,还创建了“用户控制器”和“相册控制器”。 UserController包含登录和注销操作。 AlbumController包含正常的CRUD和欢迎操作 现在,当我直接访问http://localhost/websites/zendtest/public/admin/login当我已经登录时 同样的问题是,当我直接访问http://localhost/websites/zendtest/public/admin/album/we

我是Zend Framework 2的新手

我创建了一个“管理”模块,还创建了“用户控制器”和“相册控制器”。 UserController包含登录和注销操作。 AlbumController包含正常的CRUD和欢迎操作

现在,当我直接访问
http://localhost/websites/zendtest/public/admin/login
当我已经登录时

同样的问题是,当我直接访问
http://localhost/websites/zendtest/public/admin/album/welcome
当我还没有登录时

有人能给我建议解决办法吗

我还有另一个问题,如何在layout.phtml中使用控制器动作值,因为我有用于创建菜单的menucontoler。所以我需要从layout.phtml中的MenuController返回数组以创建动态菜单


那么,我怎样才能做到这一点呢?

我想你不想在报告中得到解释。要继续,您必须使用
redirect
plugin:

$this->redirect()->toRoute('actionname');
重定向插件的使用方式如下:

->toRoute($route, array $params = array(), array $options = array());
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
    }
}
重定向到命名路由,使用提供的
$params
$options
组合URL

要验证用户身份,如旧版ZF的
acl
插件,请转到

对于最后一个问题,您可以使用(对于ZF2.1.3)在视图中通过一些值

并在视图中使用

$myvar...

我不知道您是如何验证用户的,但如果您使用的是
Zend\Auth
,则可以执行以下操作:

->toRoute($route, array $params = array(), array $options = array());
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
    }
}
欢迎行动:

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

如果你想在很多页面上做这件事,那么这是相当重复的,所以你应该考虑使它可以重复使用,例如从服务管理器(工厂)获取验证服务。你可能正在寻找一种叫做重定向的东西。另外,由于您是ZF2新手,请展示您已经完成并展示您迄今为止所做的尝试。您使用的是zfcUser还是其他模块?或者直接使用Auth设置?不,我不使用zfcUser,但我需要全局检查用户是否登录,如果已经登录,用户需要访问登录操作,然后在欢迎页面上重定向。如果未登录且用户访问欢迎页面或其他内部页面(如帐户页面),则他会在登录页面上重定向我知道,但我需要全局检查用户是否登录,如果已登录且用户需要访问登录操作,则他会在欢迎页面上重定向。如果未登录,用户访问welcome或其他内部页面,如account page,则他会在登录页面OK上重定向,因此您不希望使用类似于旧版ZF的已知

acl
插件的方法。。。看,是的,我需要这样的东西。。。你能给我另一个问题的答案吗?我的问题是我有用于创建动态菜单的菜单表,所以我创建了menucontoler和模型。现在我如何从MenuController的任何操作中获取数组,我可以在layout.phtml中使用它,因为菜单将位于标题中。