Zanshin13
Zanshin13

Reputation: 1066

Laravel 5.2 subdomain dynamical routes to controllers

Lets assume I have a site with cars: cars.com on Laravel 5.

I want to set up my routes.php so a user could type in a browser ford.cars.com/somethingOrnothing and get to the controller responsible for Ford™ cars (FordController). Of course I could use something like this code:

    Route::group(['middleware' => 'web'], function () {     
        Route::group(['domain' => 'ford.cars.com'], function(\Illuminate\Routing\Router $router) {
            return $router->resource('/', 'FordController');
        });
    });

But I am not happy about writing and maintaining routes for hundreds of car brands.

I would like to write something like this:

Route::group(['domain' => '{brand}.cars.com'], function(\Illuminate\Routing\Router $router) {
        return $router->get('/', function($brand) {
            return Route::resource('/', $brand.'Controller');
        });
    });

So the question is: Is it possible to dynamically set routes for sub-domains and how to achieve this?

upd:
the desirable outcome is to have subdomains that completely repeat controllers structure. Like Route::controller() did (but it is now deprecated)

Upvotes: 1

Views: 1028

Answers (2)

Nikolay Zakharov
Nikolay Zakharov

Reputation: 162

To emulate Route::controller() behaviour you could do this:

Route::group(['domain' => '{carbrand}.your.domain'], function () {
    foreach (['get', 'post'] as $request_method) {
        Route::$request_method(
            '{action}/{one?}/{two?}/{three?}/{four?}',
            function ($carbrand, $action, $one = null, $two = null, $three = null, $four = null) use ($request_method) {
                $controller_classname = '\\App\\Http\\Controllers\\' . Str::title($carbrand).'Controller';
                $action_name = $request_method . Str::title($action);

                if ( ! class_exists($controller_classname) || ! method_exists($controller_classname, $action_name)) {
                    abort(404);
                }

                return App::make($controller_classname)->{$action_name}($one, $two, $three, $four);
            }
        );
    }
});

This route group should go after all other routes, as it raises 404 Not found exception.

Upvotes: 1

pnsh
pnsh

Reputation: 164

Probably this is what you need:

Sub-Domain Routing

Route groups may also be used to route wildcard sub-domains. Sub-domains may be assigned route parameters just like route URIs, allowing you to capture a portion of the sub-domain for usage in your route or controller. The sub-domain may be specified using the domain key on the group attribute array:

Route::group(['domain' => '{account}.myapp.com'], function () {
    Route::get('user/{id}', function ($account, $id) {
        //
    });
});

From: Laravel 5.2 Documentation

upd.

If you want to call your controller method you can do it like this:

Route::group(['domain' => '{account}.myapp.com'], function () {
    Route::get('user/{id}', function ($account, $id) {
        $controllerName = $account . 'Controller' //...or any other Controller Name resolving logic goes here
        app('App\Http\Controllers\\' . $controllerName)->controllerMethod‌​($id);
    });
});

Upvotes: 0

Related Questions