我需要从当前网址获取参数,但是当我将代码放入公共函数__construct
时,我收到错误这是我目前的代码:
class BlogController extends AbstractActionController{
public function __construct()
{
//echo $this->params()->fromRoute('controller');
//echo $this->params()->fromRoute('action');
}
}
如果有办法从字符串/当前网址获取参数。没关系。
请帮忙。我需要这个为我的ACL,所以我不会检查每个功能当前的动作和控制器。
答案 0 :(得分:1)
实际上,params()
控制器插件提供了来自五个不同来源的参数:
那么,您需要的是fromQuery()
,而不是fromRoute()
答案 1 :(得分:0)
现在我知道它不可能。
我在一些文档中找到了答案然后我将此代码添加到我的Module.php
public function onBootstrap(MvcEvent $e) {
$this->initAcl($e);
$e->getApplication()->getEventManager()->attach('route', array($this, 'checkAcl'));
}
public function initAcl(MvcEvent $e) {
$acl = new \Zend\Permissions\Acl\Acl();
$roles = array(
'guest'=> array( //functions that the user can access
'registration',
'home',
),
'admin'=> array(
'registration',
),
);
$allResources = array();
foreach ($roles as $role => $resources) {
$role = new \Zend\Permissions\Acl\Role\GenericRole($role);
$acl->addRole($role);
$allResources = array_merge($resources, $allResources);
//adding resources
foreach ($resources as $resource) {
if(!$acl->hasResource($resource))
$acl->addResource(new \Zend\Permissions\Acl\Resource\GenericResource($resource));
}
//adding restrictions
foreach ($resources as $resource) {
$acl->allow($role, $resource);
}
}
$e->getViewModel()->acl = $acl;
}
public function checkAcl(MvcEvent $e) {
$route = $e -> getRouteMatch() -> getMatchedRouteName();
//you set your role
$userRole = 'guest';
//if (!$e -> getViewModel() -> acl -> isAllowed($userRole, $route)) {
if ($e -> getViewModel()->acl->hasResource($route) && !$e->getViewModel()->acl->isAllowed($userRole, $route)) {
$response = $e -> getResponse();
//location to page or what ever
$response -> getHeaders()->addHeaderLine('Location', $e -> getRequest() -> getBaseUrl() . '/404');
$response -> setStatusCode(404);
}
}