Note that there are some explanatory texts on larger screens.

plurals
  1. POSetup Zend Router with and without lang uri parameter
    primarykey
    data
    text
    <p>I'm wondering if anyone can help me with the next issue I have.</p> <p>I want to be able to setup my project with multilanguage support with and without Uri lang flag, and also, with module structure.</p> <p>I mean something like </p> <pre><code>http://mydomain/ http://mydomain/forum http://mydomain/gallery </code></pre> <p>Navigate without lang uri parameter with the Zend_Locale + Zend_Translate default (en)</p> <p>And also :</p> <pre><code>http://mydomain/es http://mydomain/es/forum http://mydomain/es/gallery </code></pre> <p>I've followed some tutos that I've found, but I still can't do so.</p> <p>This is my current code :</p> <p>I have zend _initRoute, _initLocale and _initTranslate setted on my bootstrap.php as follow:</p> <pre><code>protected function _initRoutes() { $front = Zend_Controller_Front::getInstance(); $router = $front-&gt;getRouter(); $module = new Zend_Controller_Router_Route_Module( array(), $front-&gt;getDispatcher(), $front-&gt;getRequest() ); $language = new Zend_Controller_Router_Route( ':lang', array( 'lang' =&gt; 'es' ) ); /** * I didn't chain this route cause doesn't work as I expected, * but this is how I want to be able to navigate. * **/ $default = new Zend_Controller_Router_Route( ':controller/:action/*', array( 'lang' =&gt; 'es', 'module' =&gt; 'default', 'controller' =&gt; 'index', 'action' =&gt; 'index' ) ); $chainRoute = new Zend_Controller_Router_Route_Chain(); $chainRoute-&gt;chain($language) -&gt;chain($module); $router-&gt;addRoute('default', $chainRoute); return $router; } /** * * Default locale: es_AR */ protected function _initLocale() { $local = new Zend_Locale(); $local-&gt;setDefault('es_AR'); Zend_Registry::set('Zend_Locale', $local); } /** * * Inisializacion de los lenguages y el Locale por default */ protected function _initTranslate() { $translate = new Zend_Translate( 'Array', APPLICATION_PATH . "/langs/", null, array( 'scan' =&gt; Zend_Translate::LOCALE_DIRECTORY ) ); Zend_Registry::set('Zend_Translate', $translate); $translate-&gt;setLocale(Zend_Registry::get('Zend_Locale')); } </code></pre> <p>I also register the next Language Plugin :</p> <pre><code>class MyApp_Controller_Plugin_Language extends Zend_Controller_Plugin_Abstract { public function routeShutdown(Zend_Controller_Request_Abstract $request) { $lang = $request-&gt;getParam('lang', null); $locale = Zend_Registry::get('Zend_Locale'); $translate = Zend_Registry::get('Zend_Translate'); if ($translate-&gt;isAvailable($lang)) { $translate-&gt;setLocale($lang); $locale-&gt;setLocale($lang); } else { //throw new Zend_Controler_Router_Exception('Translation language is not available', 404); $lang = $locale-&gt;getLanguage(); $locale-&gt;setLocale($lang); $translate-&gt;setLocale($lang); } $front = Zend_Controller_Front::getInstance(); $router = $front-&gt;getRouter(); $router-&gt;setParam('lang', $lang); } } </code></pre> <p>Also I've created my own Url helper extended by zend url helper which uses the 'Language_Action_Helper':</p> <pre><code>class Zend_View_Helper_MyUrl extends Zend_View_Helper_Url { protected function _getCurrentLanguage() { return Zend_Controller_Action_HelperBroker::getStaticHelper('Language') -&gt;getCurrent(); } public function myUrl($urlOptions = array(), $name = null, $reset = true, $encode = true) { $urlOptions = array_merge( array( "lang" =&gt; $this-&gt;_getCurrentLanguage() ), $urlOptions ); return parent::url($urlOptions, $name, $reset, $encode); } } </code></pre> <p>And then I've created an Anchor helper which uses my url Helper :</p> <pre><code>class Zend_View_Helper_Anchor extends Zend_View_Helper_Abstract { public function anchor ( $anchorText, $controller, $action, Array $params = array() ) { $front = Zend_Controller_Front::getInstance(); $defaultUrl = array( 'module' =&gt; $front-&gt;getRequest()-&gt;getModuleName(), 'controller' =&gt; $controller, 'action' =&gt; $action ); if (!empty($params)) { foreach ($params as $param =&gt; $value) { $defaultUrl[$param] = $value; } } // using my url helper $url = $this-&gt;view-&gt;myUrl( $defaultUrl, null, true ); $anchor =&lt;&lt;&lt;HTML &lt;a href="{$url}"&gt;{$anchorText}&lt;/a&gt; HTML; return $anchor; } } </code></pre> <p>This is the Language Action helper to provide the language inside the controller used by my url helper.</p> <pre><code>class Controller_Helper_Language extends Zend_Controller_Action_Helper_Abstract { /** * * Get Current language * * @return mixed string|null */ public function getCurrent(){ if (!Zend_Registry::isRegistered("Zend_Locale")) return null; return Zend_Registry::get("Zend_Locale")-&gt;getLanguage(); } /** * * Get translator * * @return mixed Zend_Translate|null * */ public function getTranslator(){ if (!Zend_Registry::isRegistered("Zend_Translate")) return null; return Zend_Registry::get("Zend_Translate"); } } </code></pre> <p>My final approach is with this structure, be able to set up standard routes like : </p> <pre><code>http://mydomain/forum/:id http://mydomain/topic/:id http://mydomain/section/:id http://mydomain/user/:user </code></pre> <p>with language support.</p> <p>How these routes must be configured to meet my needs?, I mean, I've tried to do something like : </p> <pre><code>// what may I do here? How do I chain this route with my language router $topicRoute = new Zend_Controller_Router_Route( 'topic/:id', array( 'module' =&gt; 'default', 'controller' =&gt; 'forum', 'action' =&gt; 'topic', 'id' =&gt; ':id' ), array( 'id' =&gt; '^[0-9]$' ) ) </code></pre> <p>Also this router config bring me some problems with my navigation config. When I set this router, and then navigate to /topic/id, when I hover any link given by the navigation.xml, always return the same URI given by this config.</p> <pre><code>http://mydomain/topic/id </code></pre> <p>Is anyone familiar with this?, Is this possible to do? Is any different way to do something like this? I've tried some different things which make me closer to my goal, but this is the most closer I could...</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload