[英]Pass dynamic controller in route in laravel
Route::get($PageController[2], [RollsController::class , $PageController[2]]);
嗨編碼器,我想傳遞動態 class 而不是RollsController
。
假設我有$controller_name = 'RollsController';
所以我想在路由中傳遞這個變量而不是RollsController::class
如何實現它們?
我試過像下面這樣,但它給了我一個錯誤。 提前致謝。
Route::get($PageController[2], [$controller_name::class , $PageController[2]]); //I want this type of route with dynamic controller.
您只能在 object 上使用::class
class
$controller_name = RollsController::class;
Route::get($PageController[2], [(new $controller_name)::class, $PageController[2]]);
或使用反射
$controller_name = RollsController::class;
try {
$oClass = new ReflectionClass($controller_name);
Route::get($PageController[2], [$oClass::class, $PageController[2]]);
} catch (ReflectionException $e) {
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.