【问题标题】:Laravel how to filter widlcard subdomains?Laravel如何过滤通配符子域?
【发布时间】:2018-05-16 00:12:42
【问题描述】:

尝试根据通配符子域过滤路由。所以基本上我们有一个网站example.com。我想根据local.admin.example.comadmin.example.com 等子域对其进行过滤。

我有这样的路线设置:

Route::group(['domain' => '{sub}.example.com', 'namespace' => 'Controllers\Admin'], function()
{
    Route::get('/', function(){ return Redirect::to('/dashboard'); });

    Route::any('{all}', function($uri){ return Redirect::to('/404'); })->where('all', '.*');

})->where('sub', 'local.admin');

当我添加 ->where 时,它只会给我一个错误:

Call to a member function where() on a non-object

基本上,我只想在域中捕获带有admin 的任何内容并将其发送到该路由组。我知道我可以用一些if 语句来做到这一点,但在我看来,与一个小组一起做这件事更干净。或者if 语句是要走的路吗?

【问题讨论】:

    标签: php laravel laravel-4 laravel-routing


    【解决方案1】:

    您应该在组内的路由元素上调用where 函数。如果只想获取特定的子域,不需要添加变量,在group函数中添加即可。

    Route::group(['domain' => '{sub}.example.com', 'namespace' => 'Controllers\Admin'], function()
    {
        Route::get('/', function(){ return Redirect::to('/dashboard'); })->where('sub', 'local.admin');
        Route::any('{all}', function($uri){ return Redirect::to('/404'); })->where('all', '.*')->where('sub', 'local.admin');
    });
    

    Route::group(['domain' => 'local.admin.example.com', 'namespace' => 'Controllers\Admin'], function()
    {
        Route::get('/', function(){ return Redirect::to('/dashboard'); });
        Route::any('{all}', function($uri){ return Redirect::to('/404'); })->where('all', '.*');
    });
    

    【讨论】:

    • 但是我必须在该组中的每条路线上添加->where('sub', 'local.admin'),似乎有点乏味。
    • 在这种情况下,使用我的第二个例子:)
    【解决方案2】:

    试试看:

    Route::domain('{estado}.venezuela.gob.ve')->group(function () {
      Route::get('/', 'EstadoController@index')->where('estado', 'distrito-capital|amazonas|anzoategui|apure|aragua|barinas|bolivar|carabobo|cojedes|delta-amacuro|falcon|guarico|lara|merida|miranda|monagas|nueva-esparta|portuguesa|sucre|tachira|trujillo|vargas|yaracuy|zulia|federales-federales');
      Route::get('/Otro', 'EstadoController@index')->where('estado', 'distrito-capital|amazonas|anzoategui|apure|aragua|barinas|bolivar|carabobo|cojedes|delta-amacuro|falcon|guarico|lara|merida|miranda|monagas|nueva-esparta|portuguesa|sucre|tachira|trujillo|vargas|yaracuy|zulia|federales-federales');
    });
    
    Route::domain('venezuela.gob.ve')->group(function () {
      Route::get('/', 'NacionController@index');
      Route::get('/Otro', 'NacionController@index');
    });
    // Another universal route
    Route::get('/algo', function () {
        return "algo";
    });
    

    请求通配符值的一个很好的做法可能是:

    状态控制器:

        namespace Venezuela\Http\Controllers;
    
        use Illuminate\Http\Request;
        use Venezuela\EstadosModel;
    
        class EstadoController extends Controller {
            public $estado;
            public $estado_actual;
            public function __construct(Request $request)
            {
                $this->estado = $request->estado;
                // Global process for all States request
                $this->estado_actual = EstadosModel::where('slug', $this->estado)->first();
            }
    
            public function index($estado) {
                // return view('estado.portada', ['estado' => $estado, 'estado_actual' => $this->estado_actual]);
                return 'index for: '.$estado;
            }
    
            public function otro($estado) {
                // return view('estado.otro', ['estado' => $estado, 'estado_actual' => $this->estado_actual]);
                return 'other action for: '.$estado;
            }
    
        }
    

    国家控制器:

        <?php
    
        namespace Venezuela\Http\Controllers;
    
        use Illuminate\Http\Request;
    
        class NacionController extends Controller {
    
    
            public function index() {
                // return view('nacion.portada');
                return 'index';
            }
    
            public function otro() {
                // return view('nacion.otro');
                return 'other action';
            }
        }
    

    结果:

    http://venezuela.gob.ve -> index
    http://venezuela.gob.ve/Otro -> other action
    http://venezuela.gob.ve/algo -> algo
    http://lara.venezuela.gob.ve -> index for: lara
    http://lara.venezuela.gob.ve/Otro -> Other for: lara
    http://lara.venezuela.gob.ve/algo -> algo
    http://yaracuy.venezuela.gob.ve -> index for: yaracuy
    http://yaracuy.venezuela.gob.ve/Otro -> Other for: yaracuy
    http://yaracuy.venezuela.gob.ve/algo -> algo
    http://another.venezuela.gob.ve -> 404
    http://another.venezuela.gob.ve/Otro -> 404
    http://another.venezuela.gob.ve/algo -> algo
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-12
      • 2017-06-22
      • 2017-12-14
      • 2014-09-06
      • 1970-01-01
      • 1970-01-01
      • 2020-08-23
      • 1970-01-01
      相关资源
      最近更新 更多