关于数据获取时不存在时的404报错处理方案
来源:互联网 发布:overture中文版mac 编辑:程序博客网 时间:2024/05/29 04:56
在ArticleContronller.php中出现数据获取不存在时
public function show($id){ $article=Arrticle::find($id) if(is_null($article)){ abort(404) } return view(articles.show,compact('article'));}
注:关于abort()与exit()
exit和abort都是用来终止程序的函数,他们的不同如下:
exit会做一些释放工作:释放所有的静态的全局的对象,缓存,关掉所有的I/O通道,然后终止程序。如果有函数通过atexit来注册,还会调用注册的函数。不过,如果atexit函数扔出异常的话,就会直接调用terminate。
abort:立刻terminate程序,没有任何清理工作。
参考:http://bbs.csdn.net/topics/390130642
注:
Q:Laravel app:abort(404) not showing 404 page
I have a route of /path/{id}, and I want my controller to detect when the id is invalid and product a 404 error page. I tried using App::abort(404);, but this doesn’t produce a 404 error - just a generic exception. How do I make it generate a 404 page?
A:You have to use the Response::make or Response::view functions to generate a specific page. You can pass the error code as an argument to that. Like this:
Response::make("Page not found", 404);
Or to use a specific view, use:
return Response::view('404', array(), 404);
To display the view app/views/404.php with the http response code 404. Remember to replace the slashes with dots for the view name, so if your file is at app/views/errors/404.php, you will need to use the view name ‘errors.404’ for laravel to find it.
参考:http://stackoverflow.com/questions/22011202/laravel-appabort404-not-showing-404-page
注:
当命名空间不一致时,注意使用方式
\App::abort(404);app()->abort(404);
参考https://laravel-china.org/topics/1385/laravel-5-how-to-use-app-correctly-abort-404
使用findorFail()
public function show($id){ $article=Arrticle::findorFail($id) if(is_null($article)){ abort(404) } return view(articles.show,compact('article'));}
- 关于数据获取时不存在时的404报错处理方案
- 关于js的报错处理
- python读取有中文文件时的报错处理
- 安装Oracle数据库时的报错处理[INS-35172]
- 编译时 报错处理(三)
- 关于java.lang.outofmemoryerror的报错处理
- 关于java.lang.outofmemoryerror的报错处理
- 关于VC6和VS2008预编译的报错处理
- Mysql插入数据报错处理
- eclipse中的js文件报错处理方案
- Android Studio 3.0更新项目报错处理方案
- Oracle起动库时1102报错处理
- Oracle起动库时1102报错处理
- 用expdp导出时遇到ORA-12170报错处理
- 编译时 报错处理(一)结构体问题
- 编译时 报错处理(二)id类型
- Maven3.0环境变量设置与安装时的报错处理方法
- MAC OSX系统使用gdb编译程序时的报错处理
- 高仿膜拜单车安卓APP--Mobike 之Splash欢迎界面SplashActivity
- 二项队列
- matplotlib绘图教程
- 关于原码反码补码的相关结论
- CentOS7下安装MySQL5.7安装与配置(YUM)
- 关于数据获取时不存在时的404报错处理方案
- 左式堆
- JNI开发
- 二叉堆
- 常见异常
- iOS平台下闪退原因汇总(一):"Ran out of trampolines of type 0/1/2" 运行时间错误
- 思维的局限
- 表格分页实现
- 第六次上机实验