繁体   English   中英

在 laravel 路由中传递动态 controller

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM