I'm lost in the url rewriting of Zend Framework ! I have a route.ini
like that :
routes.page.route = :slug
and I have a call in the bootstrap like that :
protected function _initRoutes() {
$uri = explode('/', $_SERVER['REQUEST_URI']);
if (!in_array('admin', $uri)) {
//$front = Zend_Controller_Front::getInstance();
$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini');
$router = $front->getRouter();
$router->addConfig($config, 'routes');
$front->registerPlugin(new Rez_Controller_Plugin_Routing(), 2);
}
}
the plugin is like that:
class Rez_Controller_Plugin_Routing extends Zend_Controller_Plugin_Abstract {
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
{
$item_db = new Application_Model_DbTable_Item();
if(NULL != $request->getParam("slug")){
if("admin" == $request->getParam("slug")){
$request->setModuleName('admin');
$request->setControllerName("index");
$request->setActionName('index');
}
$page = $item_db->getPage($request->getParam("slug"));
if(FALSE == $page){
$request->setModuleName('default');
$request->setControllerName($request->getParam("slug"));
$request->setActionName('index');
return;
}else{
if(NULL !== $request->getParam("language")){
$page["lang"] = $request->getParam("language");
}
$request->setModuleName('default');
$request->setControllerName("index");
$request->setActionName('page');
$request->setParams($page);
return;
}
}
}
the problem is that I have to prepend a language prefix in URL "htttp://mywebsite.com/en/a-simple-page"
only when the language of the browser is English or when the user have clicked the English flag, otherwise, the URL have to be like "http://mywebsite.com/a-simple-page"
.
I tried to add this in route.ini :
routes.page.route = :language/:slug
routes.page.reqs.language = "^(en|nl)$"
routes.page.defaults.language = "nl"
but when I tried "http://mywebsite.com/a-simple-page"
it doesn't work while "http://mywebsite.com/en/a-simple-page"
worked and I can get the param in the plugin. How to make it accept all URL even when a language code is not in the URL?
For this to work you will need to chain 2 routes.
resources.router.routes.defaultmodule.type = Zend_Controller_Router_Route_Module
resources.router.routes.defaultmodule.abstract = On
resources.router.routes.defaultmodule.defaults.module = "default"
resources.router.routes.locale.type = Zend_Controller_Router_Route
resources.router.routes.locale.route = ":locale"
resources.router.routes.locale.reqs.locale = "^(en)$"
resources.router.routes.locale.defaults.locale = "nl"
resources.router.routes.default.type = Zend_Controller_Router_Route_Chain
resources.router.routes.default.chain = "locale,defaultmodule"
Keep in mind that having same content under 2 urls is not good due to SEO implications. A redirect would be more appropriate.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.