Laravel - CNAME + Subdomain Routing

后端 未结 3 1370
悲&欢浪女
悲&欢浪女 2021-02-11 06:44

I have my routes set up as follows:

 \'{username}.u.\'.env(\'APP_DOMAIN\'),
], function () {
    Route::get(\'/\', \         


        
3条回答
  •  没有蜡笔的小新
    2021-02-11 07:06

    This has been answered, at least partially, by myself here: Creating a route in Laravel using subdomain and domain wildcards

    In short, you cannot make the whole domain a parameter in the routes file. Instead, you assign a middleware that will check the current domain, match it against your pre-defined list of allowed user domains and based on that - take some decision (eg. what method to hit in your controller).

    Consider:

    Route::group([
        'middleware' => 'domain-check',
    ], function () {
        Route::get('/', 'FrontendController@handle');
    });
    

    And then in your FrontendController:

    public function handle(Request $request)
    {
        // Information about the current user/domain is here
        $request->client;
    
        // An example - let's imagine that client object contains a view ID
        return response()->view($request->client->view);
    }
    

提交回复
热议问题