Lazar Vuckovic
Lazar Vuckovic

Reputation: 808

How to handle Laravel 4 sub-domain route with controller, passing subdomain as an argument

Following the Laravel 4 documentation on Routing, I've been trying to create a domain route that will handle a wildcard subdomain and pass it over to a controller action, but I'm having trouble passing the argument.

Route::group(array('domain' => '{subdomain}.myapp.com'), function()
{
    Route::get('/', function($subdomain)
    {
        die($subdomain);
    });
});

If I write the route like this, it will print out the subdomain, whatever it might be. The problem is that I don't want to write the code that handles these situations in the routes.php file, but use a Controller to handle it all, without redirecting from subdomain.myapp.com to myapp.com/controller/action/subdomain. So, something like this:

Route::group(array('domain' => '{subdomain}.myapp.com'), function()
{
    Route::get('/', 'MyController@myAction', $subdomain);
});

How do I pass the {subdomain} argument to the controller in this case?

Upvotes: 3

Views: 1081

Answers (1)

Lazar Vuckovic
Lazar Vuckovic

Reputation: 808

It seems as though the morning is smarter than the night. I went with a dispatch solution, so if anyone else has a more elegant solution, please feel free to post and I'll accept your answer instead.

Route::group(array('domain' => '{subdomain}.myapp.com'), function()
{
    Route::get('/', function($subdomain) {
        $request = Request::create('/myRoute/' . $subdomain, 'GET', array());
        return Route::dispatch($request)->getContent();
    });
});

Route::get('myRoute/{subdomain}', 'MyController@myAction');

Upvotes: 2

Related Questions