7

sorry for my bad english, i'm from Russia

began to learn zend framework 2... Sample in controller, readAction():

use Zend\View\Helper\Url;

....

$helperUrl = new Url();
$address  = $helperUrl('news', array('action' => 'index'));

As a result, thrown exception:

Zend\View\Exception\RuntimeException

File:
    W:\home\zf2\vendor\zendframework\
       zendframework\library\Zend\View\Helper\Url.php:80
Message:
    No RouteStackInterface instance provided

Please, help me. what I'm doing wrong?

3 Answers 3

13

You can't use a viewhelper in a controller - and you don't need to. There's also a Url controller plugin that does pretty much the same.

Controller plugins are invokable classes, you can use them like this (controller's action context):

$url = $this->url()->fromRoute($route, $params, $options, $reuseMatchesParams);

All parameters are optional. For further information, check the code in Zend\Mvc\Controller\Plugin\Url or read the docs.

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

1 Comment

not true. you can use view helper in controller, but ofc is not very good idea: $uVH = $this->getServiceLocator()->get( 'viewhelpermanager' )->get( 'url' ); $routeUrl = $uvh('route-name',array('param1'=>'param1')); etc
3

Helpers and plugins, while they can be directly instantiated, usually shouldn't, as they typically are managed by a PluginManager. The PluginManager is a subclass of the ServiceManager, and, as such, provides a lot of functionality around plugin creation, including injection of standard collaborators, usage of factories to provide dependency injection, and more.

In the case of the Url helper, there is a factory that ensures it has the configured router injected -- if you instantiate it directly, you won't have this, and it won't be able to do its job, as you noticed!

As Daniel noted, you also want to make sure you're getting a plugin that's appropriate for the context. If you're in a controller, check to see if there's a controller plugin that will do the job for you. Daniel linked to the docs in his post.

1 Comment

u saved me from headache :-)
0

I know there is already an accepted answer but I'll still post this to lessen the burden of beginners like me.

I use smarty as view renderer and in smarty you can't use php codes in the views, thus, everything must be done in the controller, allocating them in variables then passing them to the views.

You can use this:

$url = $this->url()->fromRoute('route',array('name'=>'route-name'));

If you are following the tutorial of zend 2, it will be like this:

$url = $this->url()->fromRoute('album',array('action'=>'add'));
$url = $this->url()->fromRoute('album',array('action'=>'edit'));
$url = $this->url()->fromRoute('album',array('action'=>'delete'));

This will have a value of:

/zf2/index.php/album/add
/zf2/index.php/album/edit
/zf2/index.php/album/delete

As you can see, you need to add the server name to it, which you can do by using these before the generating the route url:

$url = $uri = $this->getRequest()->getUri();
$url = sprintf('%s://%s', $uri->getScheme(), $uri->getHost());

Overall, the code snippet should look like this:

$url = $uri = $this->getRequest()->getUri();
$url = sprintf('%s://%s', $uri->getScheme(), $uri->getHost());
$url .= $this->url()->fromRoute('album',array('action'=>'add'));

To produce:

http://yourservername/zf2/index.php/album/add

Hope this helps beginner zf2 users

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.