【发布时间】:2017-06-09 17:17:46
【问题描述】:
我刚刚学习了一个教程,其中我需要在 web.php 中调用控制器函数的唯一代码是:
Route::resource('/articles', 'ArticleController');
这适用于我在创建项目时已经存在的所有功能(编辑、更新、销毁……) 现在我需要做第二个更新功能(upvote)。但是在控制器中创建一个新函数并路由到它时,我收到以下错误:
路线 [articles.upvote] 未定义。 (查看:D:\school\web-backend\Oplossingen\hackernews\resources\views\articles\index.blade.php)
如何让“a”标签进入控制器的投票功能? 我的代码:
public function upvote(Request $request, $id)
{ /* ArticleController.php */
$article = Article::find($id);
$article->points += 1;
$article->save();
Session::flash('succes', 'Upvote was a succes');
return redirect()->route('articles.index');
}
td> <!-- index.blade.php !-->
<a href="{{ route('articles.upvote', ['articles'=>$storedArticle->id]) }}" class="btn btn-default">upvote</a>
</td>
<td>downvote</td>
<td>
<a href="{{ route('articles.edit', ['articles'=>$storedArticle->id]) }}" class="btn btn-default">edit</a>
</td>
Route::get('/', function () { /*route\web.php */
return view('welcome');
});
Route::resource('/articles', 'ArticleController');
【问题讨论】:
-
注意:我使用的 Laravel 版本是 v5.3.29
标签: php laravel routes laravel-5.3