Zend Framework - how to rewrite url to seo friendl

2019-05-10 23:35发布

问题:

I got website on Zend Framework (im total noob in Zend). For example I would like to make one URL "somewebsite.com/test/about" to look like this "somewebsite.com/for-fun-link". How do i achieve this in Zend ? Im newbie in Zend and Apache server. Where i do rewrites in Zend ? I want static URL somewebsite.com/page/about rewrite to somewebsite.com/about-product

And last question: where do i usually create rewrites ? its depends of sever/technology ?

回答1:

In your bootstrap, you'll need to configure some "routes". So, if your bootstrap ends something like this:

$frontController = Zend_Controller_Front::getInstance();
$frontController->dispatch();

you can just add in some route definitions like this:

$frontController = Zend_Controller_Front::getInstance();

$router = $frontController->getRouter();
$router->addRoute( 'mylovelyroute',
    new Zend_Controller_Router_Route_Static( 'for-fun-link',
        array( 'controller' => 'test', 'action' => 'about' )
    )
);
$router->addRoute( 'myotherroute',
    new Zend_Controller_Router_Route_Static( 'about-product',
        array( 'controller' => 'page', 'action' => 'about' )
    )
);
$router->addRoute( 'justonemore',
    new Zend_Controller_Router_Route_Static( 'another/longer/path',
        array( 'controller' => 'mycontroller',
            'action' => 'myaction',
            'someparameter' => 'foo'
        )
    )
);

$frontController->dispatch();

The first parameter of addRoute() is just a unique name. Zend_Controller_Router_Route_Static takes the path you'd like to capture, and then an array of parameters, including a controller and action (and module if it applies).

If you wanted to add complexity, you could load the routes out of a database, or start using more dynamic routes. The docs here are a useful next step: http://framework.zend.com/manual/en/zend.controller.router.html#zend.controller.router.routes.standard