0

I've got the following snippet successfully routing subdomain request. I'm looking for a way where I can detect the subdomain and do some quick logic to assign a value to a global variable depending on the subdomain. Where could I do this?

Route::group(array('domain' => '{username}.murch.co'), function() {
    Route::controller('checkout', 'CheckoutController');
    Route::get('/', 'UserController@getProfile');
});

I'd like to do something like

Route::group(array('domain' => '{username}.murch.co'), function() {
    $var = "dog".$username;
    View::share('var', $var);
    Route::controller('checkout', 'CheckoutController');
    Route::get('/', 'UserController@getProfile');
});
2
  • What's wrong with what you did. And why do you need a global var for? Commented Sep 14, 2014 at 1:49
  • for some reason you can't access $username like that. I have no idea why Commented Sep 14, 2014 at 19:41

2 Answers 2

1

I still don't know how to achieve this is routes.php but I ended up finding a solution using filters

 App::before(function($request)
 {
    $urlParts = explode('.', $_SERVER['HTTP_HOST']);
    $subdomain = $urlParts[0];
    $userModel = new User;
    $user = $userModel->getUserByUsername($subdomain);
    View::share('user', $user);
 });
Sign up to request clarification or add additional context in comments.

1 Comment

Which file i need to put this code, i am using laravel 5.3
0

For Laravel 5.x, you can call it from anywhere - controller, view, etc.

Route::current()->parameter('username');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.