我有一个面板页面,我想在其中显示多种功能,如显示表单、显示数据库中的文章列表。
但是,由于每条路由每次只能运行一个方法,我该如何实现呢?
我想做一些类似的事情
Route::get('/dashboard','Dashboard@index');
Route::get('/dashboard','Dashboard@showArticles');
Route::get('/dashboard','Dashboard@showUsersList');
我知道这不起作用,但还有什么选择呢?既然我想在同一页上做这一切。
您必须将所有方法组合成这样的一个方法,并将其传递给查看
public function getIndex()
{
$users = User::all();
$articles = Articles::all();
return view('page.your_view')->with('users', $users)->with('articles', 'articles');
}
您可以使用来实现
public function index()
{
$users = User::all();
$articles = Articles::all();
return view('page.your_view', compact(['users' => $users, 'articles' => 'articles']);
}
Route::get('/dashboard/{?type}','Dashboard@index');
控制器内
public function getIndex($type)
{
if(isset($type) && !empty($type) && $type=='article'){
return $this->article();
}
return view('page.index');
}
public function article(){
...YOUR CODE
}