亚洲最大看欧美片,亚洲图揄拍自拍另类图片,欧美精品v国产精品v呦,日本在线精品视频免费

  • 站長資訊網(wǎng)
    最全最豐富的資訊網(wǎng)站

    詳解Laravel路由之domain解決多域名問題

    下面由laravel教程欄目給大家介紹Laravel 路由研究之domain 解決多域名問題,希望對(duì)需要的朋友有所幫助!

    詳解Laravel路由之domain解決多域名問題

    Laravel 路由研究之domain 解決多域名問題

    材料準(zhǔn)備

    • 一份干凈的laravel
    • 兩份Nginx配置文件,主要配置如下:

      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;

    將域名分割為參數(shù)

    Route::domain('{account}.{webname}.{suffix}')->group(function () {      Route::get('user/{id}', function ($account, $webname, $suffix, $id) {         // 可以在請(qǐng)求中接收到被分割的參數(shù),可能的使用場(chǎng)景:在單獨(dú)路由中需要根據(jù)不同的域名處理不同的需求          dd($account, $webname, $suffix, $id);      }); });

    注意: 若account不固定,可以將Nginx Server Name 配置為泛型: *.example.com

    關(guān)于多域名

    配置兩個(gè)不同的域名如下:

    1. server_name *.amor_laravel_test.amor;
    2. server_name *.amor_laravel_test_1.amor;

    如何讓Laravel匹配不同的域名?

    方式1:直接在 route/web.php中使用domain區(qū)分

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

    方式2:通過設(shè)置 RouteServiceProvider 區(qū)分

    • 添加方法:
        protected function mapSelfRoutes()     {         Route::domain('{account}.amor_laravel_test_1.amor')             ->middleware('web')             ->namespace($this->namespace)             ->group(base_path('routes/self.php'));     }
    • 注冊(cè)
        public function map()     {         $this->mapApiRoutes();          $this->mapWebRoutes();          $this->mapSelfRoutes();          //     }
    • 添加路由文件
    Route::get('/user', function ($account) {     dd($account); });

    注意: 必須全部設(shè)置domain,如果只設(shè)置了self 那么在相同請(qǐng)求路徑下,未設(shè)置domain的將會(huì)首先匹配到。

    【推薦:最新的五個(gè)Laravel視頻教程】

    關(guān)于路由中的Action在多域名下的說明

    首先,我們需要知道Action決定了路由會(huì)綁定到哪個(gè)控制器,還有一點(diǎn)需要注意,路由中的Action屬性,決定了輔助函數(shù) route() 生成的url。
    假如,我們的路由配置如下:

    • 第一個(gè)路由

      Route::get('/', function () {     if(IlluminateSupportFacadesAuth::check()) {         return redirect('index');     } else {         return redirect('login');     } });
    • 第二個(gè)路由

      Route::get('/', function () {     if(IlluminateSupportFacadesAuth::check()) {         return redirect('index');     } else {         return redirect('login');     } });

    一模一樣,都是調(diào)用內(nèi)置的login路由,控制器也一樣,我們?cè)倏茨0逯械膄orm表單

    <form method="POST" class="form-horizontal" action="{{ route('login') }}"> --- </form>

    route() 輔助函數(shù),會(huì)去讀取路由 namelist 中加載的 login,如果我們?cè)?RouteServiceProvider 中將這兩個(gè)路由文件同時(shí)加載進(jìn)來,

    public function map() {     $this->mapApiRoutes();      $this->mapWebRoutes();      $this->mapSelfRoutes();      // }

    那么:不區(qū)分namespace或者不區(qū)分控制器都會(huì)導(dǎo)致 route 輔助函數(shù)生成的絕對(duì)路徑是最后一個(gè)路由domain,因此如果我們的邏輯一致,只是想通過簡(jiǎn)單的修改,通過不同的域名區(qū)別不同的站點(diǎn),我們需要做判斷,按需加載:

    public function map() {     if(request()->getHost() == env('ONLINEDOWN_DOMAIN')) {         $this->mapApiRoutes();     }      if(request()->getHost() == env('PCSOFT_DOMAIN')) {         $this->mapPcsoftRoutes();     }      $this->mapWebRoutes();     // }

    總結(jié):

    1.推薦第二種方式來區(qū)分域名,優(yōu)點(diǎn)在于路由分離 ,結(jié)構(gòu)清晰,
    2.domain不僅僅可以作為區(qū)分子域名來使用,也可以做參數(shù)分割,不同域名區(qū)分等
    3.注意Laravel的路由匹配順序,希望大家能認(rèn)真的做一遍,體驗(yàn)一下,做到心中有數(shù)
    4.既然已經(jīng)區(qū)分開域名,那么就可以綁定到不同的控制器,或者綁定不同的模型,大家靈活應(yīng)用

    贊(0)
    分享到: 更多 (0)
    網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)