尝试根据通配符子域过滤路由。所以基本上我们有一个网站example.com
。我想根据某些子域名对其进行过滤,例如local.admin.example.com
和admin.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
陈述是要走的路?
答案 0 :(得分:0)
您应该在组内的路由元素上调用where
函数。如果您只想获得特定的子域,则无需添加变量,只需将其添加到组函数中即可。
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');
});
OR
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', '.*');
});
答案 1 :(得分:0)
试一试:
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