Laravel Routing Research domain Method Example of Solving Multi domain Name Problem
- 2021-12-05 05:39:25
- OfStack
Material preparation
1 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 domain names into parameters
Route::domain('{account}.{webname}.{suffix}')->group(function () {
Route::get('user/{id}', function ($account, $webname, $suffix, $id) {
// Partitioned parameters can be received in the request. Possible usage scenarios: Different requirements need to be handled according to different domain names in separate routes
dd($account, $webname, $suffix, $id);
});
});
Note: If account is not fixed, you can configure 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?
Mode 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);
});
});
Mode 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 a routing file
Route::get('/user', function ($account) {
dd($account);
});
Note: All domain must be set. If only self is set, those without domain will be matched first under the same request path.
Explanation of Action in routing under multiple domain names
First, we need to know that Action determines which controller the route will bind to, and one more point to note is that the Action attribute in the route determines the url generated by the helper function route ().
Suppose our routing configuration is as follows:
Route 1
Route::get('/', function () {
if(\Illuminate\Support\Facades\Auth::check()) {
return redirect('index');
} else {
return redirect('login');
}
});
The second route
Route::get('/', function () {
if(\Illuminate\Support\Facades\Auth::check()) {
return redirect('index');
} else {
return redirect('login');
}
});
1 model 1 sample, is to call the built-in login routing, controller also 1 sample, we look at the template form form
<form method="POST" class="form-horizontal" action="{{ route('login') }}">
---
</form>
The route () helper function will read the login loaded in the route namelist. If we load these two route files in the RouteServiceProvider at the same time,
server_name *.amor_laravel_test.amor;
root /var/www/amor_laravel_test/public;
index index.php index.html index.htm;
0
Then: not distinguishing namespace or not distinguishing controllers will cause the absolute path generated by route auxiliary function to be the last route domain. Therefore, if our logic is 1, we just want to distinguish different sites through different domain names through simple modifications, we need to make judgments and load them as needed:
server_name *.amor_laravel_test.amor;
root /var/www/amor_laravel_test/public;
index index.php index.html index.htm;
1
Summary:
The second way to distinguish domain names is recommended, which has the advantages of separate routes, clear structure, domain can not only be used as a domain name, but also can be used for parameter segmentation, different domain name distinction and so on Pay attention to the routing matching sequence of Laravel. I hope everyone can do it carefully and experience it once, so that they can know fairly well Now that domain names have been distinguished, you can bind to different controllers or bind different models, and everyone can apply them flexibly