Work with the Zend \ View \ Helper \ Url object, from the controller

Sorry for my bad english, I'm from Russia

started learning zend framework 2 ... Sample in the controller, readAction ():

use Zend\View\Helper\Url; .... $helperUrl = new Url(); $address = $helperUrl('news', array('action' => 'index')); 

As a result of the 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 am I doing wrong?

+7
source share
3 answers

You cannot use viewhelper in the controller - and you do not need. There's also an Url controller plugin that does pretty much the same thing.

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

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

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

+12
source

Helpers and plugins, while they can be directly created, usually should not, since they are usually managed using the PluginManager. PluginManager is a subclass of ServiceManager and, as such, provides many functions for creating plugins, including injecting standard collaborators, using factories to provide dependency injection, etc.

In the case of the Url helper, there is a factory that ensures that it is configured by a configured router β€” if you create it directly, you won’t have it and it won’t be able to do its job, as you noticed!

As Daniel noted, you also want to make sure that you have a plugin that matches the context. If you are in the controller, check if there is a controller plugin that will do the job for you. Daniel is associated with documents at his post.

+3
source

I know that there is already an accepted answer, but I will publish it anyway to reduce the burden of newcomers like me.

I use smarty as a rendering visualization tool, and in smarty you cannot use php codes in views, so everything should be done in the controller, highlighting them in variables, and then passing them to the views.

You can use this:

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

If you follow the zend 2 tutorial, it will look 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')); 

It will make a difference:

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

As you can see, you need to add a server name to it, which you can do using them before generating the route URL:

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

In general, 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

0
source

All Articles