title: Define a dynamic route prefix
body: <p>I'm developing a website that needs a city always in the URL. Something like this:</p>
  
  <p><code>http://www.examples.com/city/offers</code></p>
  
  <p>If the user try to access this URL:</p>
  
  <p><code>http://www.examples.com/offers</code></p>
  
  <p>I need to redirect him to the page to choose a city to see the offers. If the user already choose a city, I'm setting a cookie (or Session) to identify the city that he have chosen.</p>
  
  <p>I can do it using Route prefixes, but how do I do dynamically?</p>
  
  <p>I've tried this approach:</p>
  
  <pre><code>$city = Request::segment(1);
  
  Route::group(array('prefix' =&gt; $city), function()
  {
      Route::get('/', array('as' =&gt; 'home', 'uses' =&gt; 'HomeController@index'));
  
      /* Blog Resource */
      Route::resource('blog', 'BlogController', array('only' =&gt; array('index', 'show')));
  });
  </code></pre>
  
  <p>The problem with this code is that if the user access this page:</p>
  
  <pre><code>`http://www.example.com/blog`
  </code></pre>
  
  <p>The system will think that 'blog' is the current city.</p>
  
  <p>Another approach I've tried (the code is a little dirty):</p>
  
  <pre><code>$city = Request::segment(1);
  
  if ($city == null)
  {
      $city = Session::get('cidade');
  }
  
  if ($city != null)
  {
      $city = City::where('slug', '=', $city)-&gt;first();
  }
  
  if (sizeof($city) &gt; 0)
  {
      Session::put('city', $city['slug']);
  
      $requestedPath = Request::path();
  
      if (!str_contains($requestedPath, $city['slug']))
      {
          return Redirect::to('/' . $city['slug'] . $requestedPath)-&gt;sendHeaders();
      }
  }
  else
  {
      Redirect::to('/choose')-&gt;sendHeaders();
      exit();
  }
  
  Route::group(array('prefix' =&gt; $city), function()
  {
      Route::get('/', array('as' =&gt; 'home', 'uses' =&gt; 'HomeController@index'));
  
      Route::resource('blog', 'BlogController', array('only' =&gt; array('index', 'show')));
  });
  </code></pre>
  
  <p>The problem with the code above is the loop. If the user access the page <code>http://www.example.com/offers</code> the system would recognize that <code>offers</code> it is not a city and redirect the user to <code>http://www.example.com/choose</code> so the user can choose the city. But when redirect to <code>choose</code>, the system would recognize again that <code>choose</code> it is not a city and keep redirecting...</p>
  
  <p>I know I have an option to use subdomains, but in this case I can't use because the client need this way.</p>
  
  <p>I've already tried this package (<a href="https://github.com/jasonlewis/enhanced-router" rel="nofollow">https://github.com/jasonlewis/enhanced-router</a>) but it does solve my problem 50%. The other problem is that it always compels me to put a city when creating URL:</p>
  
  <pre><code>{{ URL::action('HomeController@index', 'city' }}
  </code></pre>