Php 防止显示laravel消息,并将用户重定向到laravel 5中的自定义页面问题
我想以某种方式捕捉laravel错误,警告消息。我不想从config/app.php文件中禁用它们。我用独白来记录一些信息。 这是我的一段代码:Php 防止显示laravel消息,并将用户重定向到laravel 5中的自定义页面问题,php,laravel,Php,Laravel,我想以某种方式捕捉laravel错误,警告消息。我不想从config/app.php文件中禁用它们。我用独白来记录一些信息。 这是我的一段代码: public function view($id){ try { $tag = Tags::find(12313); // tags is a model }catch(Exception $error){ echo 'error'; exit(); $this->log->logM
public function view($id){
try {
$tag = Tags::find(12313); // tags is a model
}catch(Exception $error){
echo 'error'; exit();
$this->log->logMessage(Logger::ERROR, $error->getMessage());
return redirect()->route('admin.tags')->with(['msg' => 'Smth went wrong']);
}
}
$this->log
是一个类,我使用monog类来记录信息
事实是,现在,它不去捕捉部分。我没有收到错误
消息。我从拉威尔那里得到这个信息:
Trying to get property of non-object (View: ......
我故意把号码12313
放在那里,看看它是否工作。由于某种原因,它不起作用,我也没有被重定向。我的想法是,如果发生了什么事情,我想将用户重定向到一个带有一般错误消息的特定页面。我怎样才能做到这一点 您可以在laravel中完成。您可以在App\Exceptions\Handler类中处理EROR
public function render($request, Exception $exception)
{
if($exception instanceof NotFoundHttpException)
{
return response()->view('errors.404', [], 404);
}
if ($exception instanceof MethodNotAllowedHttpException)
{
return response()->view('errors.405', [], 405);
}
if($exception instanceof MethodNotAllowedHttpException)
{
return response()->view('errors.404', [], 405);
}
return parent::render($request, $exception);
}
您可以在laravel中执行此操作。您可以在App\Exceptions\Handler类中处理EROR
public function render($request, Exception $exception)
{
if($exception instanceof NotFoundHttpException)
{
return response()->view('errors.404', [], 404);
}
if ($exception instanceof MethodNotAllowedHttpException)
{
return response()->view('errors.405', [], 405);
}
if($exception instanceof MethodNotAllowedHttpException)
{
return response()->view('errors.404', [], 405);
}
return parent::render($request, $exception);
}
find()
方法在未找到记录时不会引发异常。因此,请改为:
public function view($id)
{
$tag = Tags::find(12313); // tags is a model
if (is_null($tag)) {
$this->log->logMessage(Logger::ERROR, $error->getMessage());
return redirect()->route('admin.tags')->with(['msg' => 'Smth went wrong']);
}
}
或使用,如果找不到指定的记录,将引发异常
有时,如果找不到模型,您可能希望抛出异常。这在路由或控制器中特别有用。findOrFail和firstOrFail方法将检索查询的第一个结果;但是,如果未找到任何结果,将抛出illumb\Database\Eloquent\ModelNotFoundException
find()
方法在未找到记录时不会引发异常。因此,请改为:
public function view($id)
{
$tag = Tags::find(12313); // tags is a model
if (is_null($tag)) {
$this->log->logMessage(Logger::ERROR, $error->getMessage());
return redirect()->route('admin.tags')->with(['msg' => 'Smth went wrong']);
}
}
或使用,如果找不到指定的记录,将引发异常
有时,如果找不到模型,您可能希望抛出异常。这在路由或控制器中特别有用。findOrFail和firstOrFail方法将检索查询的第一个结果;但是,如果未找到任何结果,将抛出illumb\Database\Eloquent\ModelNotFoundException
如果这是你自己的,你能显示日志类吗class@iCoders我的日志类的内容无关紧要,正如您所看到的,在exit()
函数:)之前,我没有收到error
消息,我没有寻找特定的案例,我想有一个一般情况下,捕捉所有的laravel错误消息,并重定向到一个特定的页面的用户。这只是众多案例中的一个例子:)@chester.ya现在我明白了。谢谢你的帮助comment@iCoders,好的,可能吗?:)如果这是你自己的,你能显示日志类吗class@iCoders我的日志类的内容无关紧要,正如您所看到的,在exit()
函数:)之前,我没有收到error
消息,我没有寻找特定的案例,我想有一个一般情况下,捕捉所有的laravel错误消息,并重定向到一个特定的页面的用户。这只是众多案例中的一个例子:)@chester.ya现在我明白了。谢谢你的帮助comment@iCoders,好的,可能吗?:)我应该在哪里添加此功能?如果您在laravel中签入App\Exceptions\folders。您有处理程序类fileapp->Exceptions->Handler.phpalso如果您想记录错误,那么您可以按照Alex进行操作answer@Chester.glad这对您很有帮助,我应该在哪里添加此功能?如果您签入laravel中的App\Exceptions\folders。您有处理程序类fileapp->Exceptions->Handler.phpalso如果您想记录错误,那么您可以按照Alex进行操作answer@Chester.glad这对你有帮助