1

I am using Laravel 3 with two sets of login controllers - the main domain goes to login, all subdomains should route to portal/login@index

I am using the following code in my routes.php:

Route::filter('before', function()
{
   $server = explode('.', Request::server('HTTP_HOST'));
   if (count($server) == 3)
   {
      $account = Account::where('subdomain', '=', $server[0])->first();
      Session::put('account_id', $account->id);
      Route::get('login', 'portal.login@index');
      Route::post('login', 'portal.login@index');
      Route::get('logout/(:any)', 'portal.login@logout');
   }
   else
   {
     // some other stuff - no routing calls in here
   }
 }

This code works fine for capturing the subdomain & doing the other tasks (such as setting the $account_id), but seem to have no affect on the routing

test.mydomain.com/login should go to portal/login, but instead goes to the main login controller.

I've searched through to be sure there are no filters affecting this (it is an inherited app)

Is this the correct way to set this up, and if so, what else might be affecting this?

TIA!

jmadsen
  • 3,635
  • 2
  • 33
  • 49

1 Answers1

0

It's because when you are inside

if (count($server) == 3)
{
    // Here
}

The registering of new routes using get/post is not going to respond because the system has already done the route matching, in this case you can forward the request to a new route using

Route::forward($method, $uri);

Which is in laravel/routing/route.php file as given nelow

/**
 * Calls the specified route and returns its response.
 *
 * @param  string    $method
 * @param  string    $uri
 * @return Response
 */
public static function forward($method, $uri)
{
    return Router::route(strtoupper($method), $uri)->call();
}

So, if you want to create a request similar to Route::get('login', 'portal.login@index'); then you can do it as

Route::forward('GET', 'login'); In this case, you have keep this route registered just normally you register a route. So, register/add the requests in the routes.php that you want to create dynamically and use Route::forward() method inside

if (count($server) == 3)
{
    Route::forward('GET', 'login'); // for a get request
    Route::forward('POST', 'login'); // for a post request
}

That's it.

The Alpha
  • 143,660
  • 29
  • 287
  • 307
  • Thanks, I will look at that. However, I don't believe it is because it is inside the count($server); it is because it is in the before filter. I moved it to the normal routing & it worked correctly. Your answer seems basically correct, though - I'll investigate & let you know – jmadsen Jun 25 '13 at 03:05