【发布时间】:2024-01-18 19:44:01
【问题描述】:
我有一个小问题,我正在尝试在我的 laravel 项目中添加验证(Try and Catch),问题是在某些控制器中工作正常,但在特定情况下不起作用,验证是当尝试加载页面时出现问题,应用程序会将用户重定向到另一个稳定页面并显示消息;这是我的代码:
public function info($id)
{
try {
$likes = $this->interactionAndUser($id)[0];
$dislikes = $this->interactionAndUser($id)[1];
$downloads = $this->interactionAndUser($id)[4];
$favorite = $this->interactionAndUser($id)[5];
$myLike= $this->interactionAndUser($id)[2];
$myDisLike = $this->interactionAndUser($id)[3];
$book = $this->interactionAndUser($id)[6];
$fileExistEpub = $this->interactionAndUser($id)[9];
$fileExistPdf = $this->interactionAndUser($id)[10];
$books = Book::find($id);
$forum = Forum::where('book_id', $id)->first();
$forumId = $forum->id;
$forumTheme = $forum->theme_id;
$forumHasTheme = Theme::where('id', $forumTheme)->first();
$comments = Comment::where(['forum_id' => $forumId, 'comment_id' => null])
->paginate(5);
if(Cache::has($id)==false) { // Si el ID tiene un valor falso o 0 para el cache, agregue 1
Cache::add($id, 'contador', 0.05); // Cada 0.05 segundos se contara una nueva visita por usuario, que recargue la pagina
$book->views+=1;
$book->save();
}
return view('books/info', compact('books', 'book', 'likes', 'dislikes', 'favorite', 'downloads', 'myLike', 'myDisLike', 'forum', 'forumHasTheme', 'comments', 'fileExistEpub', 'fileExistPdf'));
} catch (\Exception $e) {
return redirect('books')->with('errors', 'Ha ocurrido un errror, lo sentimos');
}
} -> This code work perfectly
如你所见,是对 Laravel 异常的简单验证,这很好用,如果出现问题,用户将被重定向到另一个稳定页面,这段代码在一个名为 BookController 的控制器中,问题与 FrontController 和另一个功能一起使用,当我尝试通过验证时,永远不要将用户重定向到稳定页面;这是我的代码
public function info_novelty($id)
{
try {
$novelty = Novelty::find($id);
return view('news.show', compact('novelty'));
} catch (\Exception $e) {
return redirect('noticias')->with('errors', 'Ha ocurrido un errror, lo sentimos');
}
} -> This validation doesn't work
我不知道为什么会这样,如果有人可以帮助我,我将非常感激
这是我得到的错误
试图获取非对象的属性(查看:/home/vagrant/Code/Biblio/resources/views/news/show.blade.php)
【问题讨论】:
标签: php laravel validation try-catch