php App::abort(404) 等效于 Laravel 5?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32922316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
App::abort(404) equivalent for Laravel 5?
提问by Inigo
In Laravel 4
i used to be able to simply call
在Laravel 4
我曾经能够简单地打电话
App::abort(404)
Is there an equivalent in Laravel 5
?
中是否有等价物Laravel 5
?
There seems to be surprisingly limited information out there about this at the time of writing. I've found discussions on how to catch NotFoundHttpExceptionsbut that isn't what I want, as the url structure is already processed by my routes.phpfile. To give some more background, here's a simplified version of what I am trying to do:
在撰写本文时,关于此的信息似乎非常有限。我找到了关于如何捕获 NotFoundHttpExceptions 的讨论,但这不是我想要的,因为我的routes.php文件已经处理了 url 结构。为了提供更多背景信息,这是我正在尝试做的简化版本:
Routes.php:
路线.php:
Route::get('/info/{page}', array('as' => 'info', 'uses' => 'Primary@infoPage'));
Primary.php (controller)
Primary.php(控制器)
public function infoPage($page){
$pageData = DB::table('pages')->where('url_title', $page)->first();
if(!empty($pageData)){
// great, there's a corresponding row in the database for this page, go ahead and do stuff...
}else {
// This page doesn't exist, please abort with a 404 error... but how?
}
}
回答by IlGala
You just have to look at the Official documentation.
你只需要查看官方文档。
HTTP Exceptions
HTTP 异常
Some exceptions describe HTTP error codes from the server. For example, this may be a "page not found" error (404), an "unauthorized error" (401) or even a developer generated 500 error. In order to return such a response, use the following:
一些异常描述了来自服务器的 HTTP 错误代码。例如,这可能是“找不到页面”错误(404)、“未经授权的错误”(401)甚至是开发人员生成的 500 错误。为了返回这样的响应,请使用以下内容:
abort(404);
Optionally, you may provide a response:
或者,您可以提供回复:
abort(403, 'Unauthorized action.');
This method may be used at any time during the request's lifecycle.
在请求的生命周期中的任何时候都可以使用此方法。
Custom 404 Error Page
自定义 404 错误页面
To return a custom view for all 404 errors, create a resources/views/errors/404.blade.php
file. This view will be served on all 404 errors generated by your application.
要为所有 404 错误返回自定义视图,请创建一个resources/views/errors/404.blade.php
文件。此视图将针对您的应用程序生成的所有 404 错误提供服务。
Update
更新
Seems that this function has been removed and will soon be replaced as written here. A "workaround" can be creating a 404 response.
似乎此功能已被删除,并将很快被替换为此处所写。“解决方法”可以是创建 404 响应。
Creating Custom Responses
创建自定义响应
For most routes and controller actions, you will be returning a full Illuminate\Http\Response
instance or a view. Returning a full Response
instance allows you to customize the response's HTTP status code and headers. A Response
instance inherits from the Symfony\Component\HttpFoundation\Response
class, providing a variety of methods for building HTTP responses:
对于大多数路由和控制器操作,您将返回一个完整的Illuminate\Http\Response
实例或视图。返回完整Response
实例允许您自定义响应的 HTTP 状态代码和标头。一个Response
实例继承自Symfony\Component\HttpFoundation\Response
该类,提供了多种构建 HTTP 响应的方法:
use Illuminate\Http\Response;
return (new Response($content, $status))
->header('Content-Type', $value);
For convenience, you may also use the response helper:
为方便起见,您还可以使用响应助手:
return response($content, $status)
->header('Content-Type', $value);
Note: For a full list of available Response methods, check out its API documentationand the Symfony API documentation.
注意:有关可用响应方法的完整列表,请查看其API 文档和Symfony API 文档。