以防萬一你需要多個通配符檢查,我把這個答案
與Symfony\Component\HttpFoundation\ParameterBag
去我只是建立一個運行ParameterBag::add()
也ParameterBag::get()
接下來的路線/ web.php一個新的別名
// Create the wildcard route
Route::get('{user}', function($user){
$template = ParameterBag::get('template');
$userdata = ParameterBag::get('userdata');
return view($template, [ 'userdata' => $userdata ]);
})->middleware('userroute');
在應用程序\的Http創建自定義$routeMiddleware
\ Kernel.php
protected $routeMiddleware = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
....
'userroute' => \App\Http\Middleware\WildcardRoutes::class,
];
而且在應用程序\ HTTP協議處理程序\中間件\ WildcardRoutes.php前
<?php
namespace App\Http\Middleware;
use Closure;
use ParameterBag; // I have this alias in app.php
use User; // alias about Users
use Group; // alias about Groups
class WildcardRoutes{
public function __construct(){ /* do anything */ }
/* Handle an incoming request. */
public function handle($request, Closure $next){
// Check if user exists
if($user = User::getUser($request->user)){
// When exist store the information
ParameterBag::add([ 'userdata' => $user, 'template' => 'templates.user' ]);
return $next($request); // means everything going well
}
// Checking in the group
if($group = Group::getGroup($request->user)){
// When exist store the information
ParameterBag::add([ 'userdata' => $group, 'template' => 'templates.group' ]);
return $next($request);
}
// When nothing found
return abort(404);
}
}
認沽foopage {}用戶? – Scuzzy