Material preparation
- A clean laravel
-
Two Nginx configuration files, the main configuration is as follows:
server_name *.amor_laravel_test_1.amor; root /var/www/amor_laravel_test/public; index index.php index.html index.htm;
server_name *.amor_laravel_test.amor; root /var/www/amor_laravel_test/public; index index.php index.html index.htm;
Split the domain name into parameters
Route::domain('{account}.{webname}.{suffix}')->group(function () { Route::get('user/{id}', function ($account, $webname, $suffix, $id) { // 可以在请求中接收到被分割的参数,可能的使用场景:在单独路由中需要根据不同的域名处理不同的需求 dd($account, $webname, $suffix, $id); }); });
Note: If the account is not fixed, you can configure the Nginx Server Name as generic: *.example.com
About multiple domain names
Configure two different domain names as follows:
server_name *.amor_laravel_test.amor;
server_name *.amor_laravel_test_1.amor;
How to make Laravel match different domain names?
Method 1: Use domain to distinguish directly in route/web.php
Route::domain('{account}.amor_laravel_test.amor')->group(function () { Route::get('user/{id}', function ($account, $id) { // dd($account, $id); }); }); Route::domain('{account}.amor_laravel_test_1.amor')->group(function () { Route::get('user/{id}', function ($account, $id) { // dd(111, $account, $id); }); });
Method 2: Distinguish by setting RouteServiceProvider
- Add method:
protected function mapSelfRoutes() { Route::domain('{account}.amor_laravel_test_1.amor') ->middleware('web') ->namespace($this->namespace) ->group(base_path('routes/self.php')); }
- Registration
public function map() { $this->mapApiRoutes(); $this->mapWebRoutes(); $this->mapSelfRoutes(); // }
- Add routing file
Route::get('/user', function ($account) { dd($account); });
Note: All domains must be set. If only self is set, then under the same request path, the domain that is not set will be matched first.
[Recommended: The latest five Laravel video tutorials]
Instructions on Action in routing under multiple domain names
First, we need Knowing that Action determines which controller the route will be bound to, there is another point to note. The Action attribute in the route determines the url generated by the auxiliary function route().
Suppose, our routing configuration is as follows:
-
First route
Route::get('/', function () { if(\Illuminate\Support\Facades\Auth::check()) { return redirect('index'); } else { return redirect('login'); } });
-
Second route
Route::get('/', function () { if(\Illuminate\Support\Facades\Auth::check()) { return redirect('index'); } else { return redirect('login'); } });
Exactly the same, both call the built-in login route, and the controller is the same. Let's look at the form form in the template
<form method="POST" class="form-horizontal" action="{{ route('login') }}"> --- </form>
route() auxiliary function, which will read the route namelist Loaded login, if we load these two routing files at the same time in RouteServiceProvider,
public function map() { $this->mapApiRoutes(); $this->mapWebRoutes(); $this->mapSelfRoutes(); // }
Then: not distinguishing namespace or controller will cause the absolute path generated by the route auxiliary function to be the last route domain , so if our logic is consistent and we just want to distinguish different sites through different domain names through simple modifications, we need to make judgments and load on demand:
public function map() { if(request()->getHost() == env('ONLINEDOWN_DOMAIN')) { $this->mapApiRoutes(); } if(request()->getHost() == env('PCSOFT_DOMAIN')) { $this->mapPcsoftRoutes(); } $this->mapWebRoutes(); // }
Summary:
1. The second way is recommended to distinguish domain names. The advantage is that routing is separated and the structure is clear.
2. Domain can not only be used to distinguish sub-domain names, but can also be used for parameter segmentation, different domain names, etc.
3. Pay attention to Laravel I hope you can do it carefully, experience it, and have a good idea of the routing matching order
4. Now that the domain names have been distinguished, you can bind them to different controllers or bind different models. Flexible application