How to get the subdomain in a subdomain-route in laravel

0 votes

I'm building an application where a subdomain points to a user. How can I get the subdomain-part of the address elsewhere than in a route?

Route::group(array('domain' => '{subdomain}.project.dev'), function() {

    Route::get('foo', function($subdomain) {
        // Here I can access $subdomain
    });

    // How can I get $subdomain here?

});

I've built a messy work-around, though:

Route::bind('subdomain', function($subdomain) {

    // Use IoC to store the variable for use anywhere
    App::bindIf('subdomain', function($app) use($subdomain) {
        return $subdomain;
    });

    // We are technically replacing the subdomain-variable
    // However, we don't need to change it
    return $subdomain;

});

The reason I want to use the variable outside a route is to establish a database-connection based on that variable.

Nov 2, 2020 in Laravel by kartik
• 37,520 points
6,992 views

1 answer to this question.

0 votes

Hello,

$subdomain is injected in the actual Route callback, it is undefined inside the group closure because the Request has not been parsed yet.

If want you want is to have it available everywhere ONCE the Request has been parsed, you could setup an after filter to store the $subdomain value:

Config::set('request.subdomain', Route::getCurrentRoute()->getParameter('subdomain'));

Hope it helps!!

answered Nov 2, 2020 by Niroj
• 82,800 points

Related Questions In Laravel

0 votes
1 answer

How to identify wheather the request is HTTP GET or HTTP POST in Laravel?

Hey, In order to identify the type of ...READ MORE

answered Mar 19, 2020 in Laravel by Niroj
• 82,800 points
3,975 views
0 votes
1 answer

.How to turn off CSRF protection for a particular route in Laravel?

Hey, We can add that particular URL or ...READ MORE

answered Mar 24, 2020 in Laravel by Niroj
• 82,800 points
4,864 views
0 votes
1 answer

How to get the id when you're validating in the model Validation unique on update using laravel?

Hello @kartik, in Laravel's inbuilt auth system, the ...READ MORE

answered Sep 11, 2020 in Laravel by Niroj
• 82,800 points
7,307 views
0 votes
1 answer

How to get route action name in laravel?

Hello @kartik, To get action name, you need ...READ MORE

answered Sep 28, 2020 in Laravel by Niroj
• 82,800 points
5,525 views
+1 vote
1 answer