Zend Framework - Redirect to routed direction

2019-07-27 22:07发布

问题:

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');