App :: abort (404) equivalent for Laravel 5?

In Laravel 4 I used to just call

 App::abort(404) 

Is there an equivalent in Laravel 5 ?

It seems that at the time of this writing, there was surprisingly limited information about this. I found discussions on how to catch NotFoundHttpExceptions , but this is not what I want, since the URL structure is already being processed by my routes .php file. To give more information, here is a simplified version of what I'm trying to do:

routes.php:

 Route::get('/info/{page}', array('as' => 'info', 'uses' => ' Primary@infoPage ')); 

Primary.php (controller)

 public function infoPage($page){ $pageData = DB::table('pages')->where('url_title', $page)->first(); if(!empty($pageData)){ // great, there 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? } } 
+5
source share
1 answer

You just need to look at the white papers .

HTTP Exceptions

Some exceptions describe HTTP error codes from the server. For example, it could be a "page not found" error (404), an "unauthorized error" (401), or even an error caused by the developer 500. To return this answer, use the following:

 abort(404); 

If you wish, you can provide an answer:

 abort(403, 'Unauthorized action.'); 

This method can be used at any time during the request life cycle.

404 User Error Page

To return a custom view for all 404 errors, create a resources/views/errors/404.blade.php . This view will be serviced in all 404 errors generated by your application.

Update

It seems that this function has been removed and will soon be replaced as written here . A workaround may produce a 404 response.

Creating custom responses

For most routes and controller actions, you will return a full Illuminate\Http\Response instance or view. Returning a full Response instance allows you to configure the HTTP response code and response headers. The Response instance is inherited from the Symfony\Component\HttpFoundation\Response class, providing many methods for creating HTTP responses:

 use Illuminate\Http\Response; return (new Response($content, $status)) ->header('Content-Type', $value); 

For convenience, you can also use the answer helper:

 return response($content, $status) ->header('Content-Type', $value); 

Note For a complete list of available response methods, check out its API documentation and the Symfony API documentation .

+12
source

Source: https://habr.com/ru/post/1232855/


All Articles