Zend Framework - Redirect to routed direction

别等时光非礼了梦想. 提交于 2020-01-06 03:59:52

问题


I have a small problem with PHP Zend Framework. I have the following routes in file application.ini:

resources.router.routes.mainpage.route = "main-page.html"
resources.router.routes.mainpage.defaults.controller = "index"
resources.router.routes.mainpage.defaults.acion = "index"

If i do a direct in any action:

$this->_helper->redirector('index', 'index');

then i will be redirect to adress: my-project/public/index/index But i want to adress be a my-project/public/main-page.html (as it is determined application.ini)

Can somebody help me? P.S. Sorry for my english.


回答1:


Use gotToUrl methods from Redirector helper

$this->_redirector = $this->_helper->getHelper('Redirector');
$this->_redirector->gotoRoute(
        array('fooRouteArgument' => fooValue),
        'route-name'
);

For your case this result in:

$this->_redirector->gotoRoute(array(), 'mainpage');



回答2:


First of all, it seems your syntax in your application.ini is wrong. See ZF-Manual: Resource Router

In your case it would bes sth. like:

resources.router.routes.mainpage.route = "/index"
resources.router.routes.mainpage.defaults.controller = "index"
resources.router.routes.mainpage.defaults.acion = "MainPage"

However, it seems you're trying to redirect to a static page, which is not part of ZF. Zend_Controller_Router can only route to Controllers within your project, as far as I know.

If so, you could simply add the following to redirect:

//In view-scripts:
<?php echo $this->baseUrl('main-page.html'); ?>
//In controllerActions:
$this->_helper->getHelper('Redirector')
        ->gotoUrl('/main-page.html');


来源:https://stackoverflow.com/questions/15717440/zend-framework-redirect-to-routed-direction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!