Home > PHP Framework > Laravel > Detailed explanation of Laravel routing domain to solve the problem of multiple domain names

Detailed explanation of Laravel routing domain to solve the problem of multiple domain names

藏色散人
Release: 2021-03-31 09:00:49
forward
3916 people have browsed it

The following is the tutorial column of laravel to introduce Laravel routing research domain to solve the problem of multiple domain names. I hope it will be helpful to friends in need!

Detailed explanation of Laravel routing domain to solve the problem of multiple domain names

Laravel routing research on domain to solve the problem of multiple domain names

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;
    Copy after login
    server_name *.amor_laravel_test.amor;
    root /var/www/amor_laravel_test/public;
    index index.php index.html index.htm;
    Copy after login

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);

    });
});
Copy after login

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:

  1. server_name *.amor_laravel_test.amor;
  2. 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);

    });
});
Copy after login

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'));
    }
Copy after login
  • Registration
    public function map()
    {
        $this->mapApiRoutes();

        $this->mapWebRoutes();

        $this->mapSelfRoutes();

        //
    }
Copy after login
  • Add routing file
Route::get('/user', function ($account) {
    dd($account);
});
Copy after login

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');
        }
    });
    Copy after login
    Copy after login
  • Second route

    Route::get('/', function () {
        if(\Illuminate\Support\Facades\Auth::check()) {
            return redirect('index');
        } else {
            return redirect('login');
        }
    });
    Copy after login
    Copy after 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(&#39;login&#39;) }}">
---
</form>
Copy after login

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();

    //
}
Copy after login

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();
    //
}
Copy after login

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

The above is the detailed content of Detailed explanation of Laravel routing domain to solve the problem of multiple domain names. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template