1. what
i want to redirect url in zend framework 2,and after my search,i found bellow method:
way 1
return $this->redirect()->toUrl('YOUR_URL');
way 2
return $this->redirect()->toRoute('ModuleName',
array('controller'=>$controllerName,
'action' => $actionName,
'params' =>$params));
way 3
return $this->redirect()->toRoute('dns-search', array(
'companyid' => $this->params()->fromRoute('companyid')
));
Where dns-search is the route I want to redirect to and companyid are the url params.
In the end the URL becomes /dns/search/1 (for example)
way 4
header( "Location: " . "other url" );
exit();
exit();
2. error
i get bellow error message:
An error occurred
An error occurred during execution; please try again later.
Additional information:
Zend\ServiceManager\Exception\ServiceNotFoundException
File:
D:\work\zend2\.metadata\.plugins\org.zend.php.framework\org.zend.php.framework.v2.CONTAINER\Zend\ServiceManager\ServiceManager.php:495
Message:
Zend\View\HelperPluginManager::get was unable to fetch or create an instance for redirect
Stack trace:
#0 D:\work\zend2\.metadata\.plugins\org.zend.php.framework\org.zend.php.framework.v2.CONTAINER\Zend\ServiceManager\AbstractPluginManager.php(103): Zend\ServiceManager\ServiceManager->get('redirect', true)
#1 D:\work\zend2\.metadata\.plugins\org.zend.php.framework\org.zend.php.framework.v2.CONTAINER\Zend\View\Renderer\PhpRenderer.php(378): Zend\ServiceManager\AbstractPluginManager->get('redirect', NULL)
#2 D:\work\zend2\.metadata\.plugins\org.zend.php.framework\org.zend.php.framework.v2.CONTAINER\Zend\View\Renderer\PhpRenderer.php(397): Zend\View\Renderer\PhpRenderer->plugin('redirect')
#3 D:\doc\webserver\php\book\module\Application\view\application\rank\index.phtml(45): Zend\View\Renderer\PhpRenderer->__call('redirect', Array)
#4 D:\doc\webserver\php\book\module\Application\view\application\rank\index.phtml(45): Zend\View\Renderer\PhpRenderer->redirect()
#5 D:\work\zend2\.metadata\.plugins\org.zend.php.framework\org.zend.php.framework.v2.CONTAINER\Zend\View\Renderer\PhpRenderer.php(507): include('D:\doc\webserve...')
#6 D:\work\zend2\.metadata\.plugins\org.zend.php.framework\org.zend.php.framework.v2.CONTAINER\Zend\View\View.php(205): Zend\View\Renderer\PhpRenderer->render(Object(Zend\View\Model\ViewModel))
#7 D:\work\zend2\.metadata\.plugins\org.zend.php.framework\org.zend.php.framework.v2.CONTAINER\Zend\View\View.php(233): Zend\View\View->render(Object(Zend\View\Model\ViewModel))
#8 D:\work\zend2\.metadata\.plugins\org.zend.php.framework\org.zend.php.framework.v2.CONTAINER\Zend\View\View.php(198): Zend\View\View->renderChildren(Object(Zend\View\Model\ViewModel))
#9 D:\work\zend2\.metadata\.plugins\org.zend.php.framework\org.zend.php.framework.v2.CONTAINER\Zend\Mvc\View\Http\DefaultRenderingStrategy.php(102): Zend\View\View->render(Object(Zend\View\Model\ViewModel))
#10 [internal function]: Zend\Mvc\View\Http\DefaultRenderingStrategy->render(Object(Zend\Mvc\MvcEvent))
#11 D:\work\zend2\.metadata\.plugins\org.zend.php.framework\org.zend.php.framework.v2.CONTAINER\Zend\EventManager\EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#12 D:\work\zend2\.metadata\.plugins\org.zend.php.framework\org.zend.php.framework.v2.CONTAINER\Zend\EventManager\EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('render', Object(Zend\Mvc\MvcEvent), Array)
#13 D:\work\zend2\.metadata\.plugins\org.zend.php.framework\org.zend.php.framework.v2.CONTAINER\Zend\Mvc\Application.php(347): Zend\EventManager\EventManager->trigger('render', Object(Zend\Mvc\MvcEvent))
#14 D:\work\zend2\.metadata\.plugins\org.zend.php.framework\org.zend.php.framework.v2.CONTAINER\Zend\Mvc\Application.php(322): Zend\Mvc\Application->completeRequest(Object(Zend\Mvc\MvcEvent))
#15 D:\doc\webserver\php\book\public\index.php(12): Zend\Mvc\Application->run()
#16 {main}
why:
because $this->plugin('redirect') is AbstractActionController member function,not in view.
if i call view with redirect plugin parameter in controller liked this:
public function indexAction()
{
return new ViewModel(array('sm' => $this->getServiceLocator(),'redirect' => $this->plugin('redirect')));
}
then in index.phtml view used this plugin it like this:
<a href="' . $this->redirect->toRoute('application',array(
'controller' => 'rank',
'action' => 'index',
'params' => array(),
)) . '" class="divWorkStatistTableColWord">' . $item['key'] . '</a>
then after access this url,it will auto redirect to target url.
but i only want to get target url string,not auto redirect to it.
and more official description is here.
fixed:
See source in Zend\Mvc\Controller\Plugin\Redirect.php.
then get target url in internet.phtml view, can done by bellow code:
<a href="' . $this->controller->plugin('url')->fromRoute('application/image',array('q' => $item['key'])) . '" class="divWorkStatistTableColWord">' . $item['key'] . '</a>
and $this->controller variable is set in parent RankController.php:
public function internetAction()
{
return new ViewModel(array('controller' => $this));
}
and the route name ‘application/image’ is set in parent config/module.config.php:
<?php
// @see http://framework.zend.com/manual/2.1/en/modules/zend.mvc.routing.html
return array(
'router' => array(
'routes' => array(
// ...
'application' => array(
'type' => 'Literal',
'options' => array(
'route' => '/application',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'rank' => array(
'type' => 'Segment',
'options' => array(
'route' => '/rank[/:action[/:lid]]',
'constraints' => array(
'action' => '[a-zA-Z0-9_-]*',
'lid' => '\d*',
),
'defaults' => array(
'controller' => 'Rank',
'action' => 'internet',
'lid' => 1,
),
),
),
'image' => array(
'type' => 'Segment',
'options' => array(
'route' => '/image[/:action[/:q]]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'q' => '.*',
),
'defaults' => array(
'controller' => 'Image',
'action' => 'book',
'q' => 'hello',
),
),
),
),
),
),
),
// ...
);
above ImageController.php code,is no need paste here.
Last:
Thanks!
1 comment:
I see a lot of interesting content on your website. You have to spend a lot of time writing,
i know how to save you a lot of work, there is a tool that creates unique,
SEO friendly articles in couple of seconds, just type in google - k2 unlimited content
Post a Comment