Home  >  Article  >  Backend Development  >  How to write this laravel route?

How to write this laravel route?

WBOY
WBOYOriginal
2016-06-29 16:09:391076browse

这个路由要怎么写
/{model}/lists 转发到{$model}Controller控制器的lists方法
如 /shop/lists => shopController控制器 lists方法,
/user/lists => userController控制器 lists方法,
不能手动一个个写 我有好多model啊

回复内容:

根据规范,应该使用 ShopController 作为类名。

以下代码适用于 Laravel 5.0 及以上:
<code class="language-text">Route::get('{model}/lists', function ($model) {
  $className = 'App\Http\Controllers\\'.ucfirst($model).'Controller';
  $obj = new $className;
  return $obj->lists();
});
</code>
一个一个写最好;
laravelbase.com/collect 最简单的办法,真的是一个一个写。
<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>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn