title: Adding a prefix to every URL in CakePHP
body: <p>What's the cleanest way to add a prefix to every URL in CakePHP, like a language parameter?</p>
  
  <pre><code>http://example.com/en/controller/action
  http://example.com/ru/admin/controller/action
  </code></pre>
  
  <p>It needs to work with "real" prefixes like <code>admin</code>, and ideally the bare URL <code>/controller/action</code> could be redirected to <code>/DEFAULT-LANGUAGE/controller/action</code>.</p>
  
  <p>It's working in a retro-fitted application for me now, but it was kind of a hack, and I need to include the language parameter by hand in most links, which is not good.</p>
  
  <p>So the question is twofold:</p>
  
  <ul>
  <li>What's the best way to structure Routes, so the language parameter is implicitly included by default without having to be specified for each newly defined Route?
  
  <ul>
  <li><code>Router::connect('/:controller/:action/*', ...)</code> should implicitly include the prefix.</li>
  <li>The parameter should be available in <code>$this-&gt;params['lang']</code> or somewhere similar to be evaluated in <code>AppController::beforeFilter()</code>.</li>
  </ul></li>
  <li>How to get <code>Router::url()</code> to automatically include the prefix in the URL, if not explicitly specified?
  
  <ul>
  <li><code>Router::url(array('controller' =&gt; 'foo', 'action' =&gt; 'bar'))</code> should return <code>/en/foo/bar</code></li>
  <li>Since <code>Controller::redirect()</code>, <code>Form::create()</code> or even <code>Router::url()</code> directly need to have the same behavior, overriding every single function is not really an option. <code>Html::image()</code> for instance should produce a prefix-less URL though.</li>
  </ul></li>
  </ul>
  
  <hr>
  
  <p>The following methods seem to call <code>Router::url</code>.</p>
  
  <ul>
  <li><code>Controller::redirect</code></li>
  <li><code>Controller::flash</code></li>
  <li><code>Dispatcher::__extractParams</code> via <code>Object::requestAction</code></li>
  <li><code>Helper::url</code></li>
  <li><code>JsHelper::load_</code></li>
  <li><code>JsHelper::redirect_</code></li>
  <li><code>View::uuid</code>, but only for a hash generation</li>
  </ul>
  
  <p>Out of those it seems the Controller and Helper methods would need to be overridden, I could live without the <code>JsHelper</code>. My idea would be to write a general function in <code>AppController</code> or maybe just in <code>bootstrap.php</code> to handle the parameter insertion. The overridden Controller and Helper methods would use this function, as would I if I wanted to manually call <code>Router::url</code>. Would this be sufficient?</p>