1

I need to get the params from the current url, but i'm getting an error when i put my code in public function __construct

Here's my current code:

class BlogController extends AbstractActionController{
    public function __construct()
    {
        //echo $this->params()->fromRoute('controller');
        //echo $this->params()->fromRoute('action');
    }
}

if there's a way to get params from a string/current url. It will be fine.

please help. i need this for my ACL so that i will not check in every function what is the current action and controller.

2
  • 2
    You can't do this in the constructor - why do you specifically need it there? Commented Sep 23, 2014 at 10:19
  • i need to put it on the constructor so that before the page load ' will know what controller or what action that the user is accessing. i also need to put it on the constructor so that when i have a new module.. it will not be necessary to put my code in every function inside the controller.. Commented Sep 24, 2014 at 1:51

2 Answers 2

1

Actually, params() controller plugin provides parameters from five different sources:

  • $this->params()->fromRoute() - Parameters you defined in routing configuration
  • $this->params()->fromFiles() - Attached files in request
  • $this->params()->fromHeader() - HTTP header parameters.
  • $this->params()->fromPost() - POST parameters
  • $this->params()->fromQuery() - Query parameters.

So, what you need is fromQuery(), not fromRoute()

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for your answer,, i tried fromQuery(); but it returns an empty array(). Here's my code: echo '</pre>'; print_r($this->params()->fromQuery()); echo '</pre>';
Hmm, what is the output of the var_dump($_GET);? Did you pass querystring parameters properly in http (nginx/apache) level?
var_dump($_GET) returns array(0) { }
Please also check Tim's comment under the question. __construct() may not correct place to do that.
Please provide more info about what u trying to accomplish. var_dump($_GET) must give you an array of url parameters only if the cuurent page url from your browser has it; If it from variable string do different approach
|
0

now i know that its not posible.

I found an answer in some docu then i add this code to my 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);

        }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.