Home > Article > Backend Development > How to write this laravel route?
这个路由要怎么写
/{model}/lists 转发到{$model}Controller控制器的lists方法
如 /shop/lists => shopController控制器 lists方法,
/user/lists => userController控制器 lists方法,
不能手动一个个写 我有好多model啊
<code class="language-text">Route::get('{model}/lists', function ($model) {
$className = 'App\Http\Controllers\\'.ucfirst($model).'Controller';
$obj = new $className;
return $obj->lists();
});
</code>
一个一个写最好;<code class="language-php"><span class="x">Route::group(['prefix' => 'shop'], function() {</span>
<span class="x"> Route::get('list', 'ShopController@list');</span>
<span class="x">})</span>
</code>