Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In Laravel 4 i used to be able to simply call

App::abort(404)

Is there an equivalent in 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 NotFoundHttpExceptions but that isn't what I want, as the url structure is already processed by my routes.php file. To give some more background, here's a simplified version of what I am 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'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?
    }
}
like image 863
Inigo Avatar asked Oct 03 '15 12:10

Inigo


People also ask

What is abort in laravel?

The abort function throws an HTTP exception which will be rendered by the exception handler: abort(403) .

How do I fix 404 Not Found in laravel 9?

How do I fix 404 Not Found in laravel 9? It should be enough to just run php artisan vendor:publish –tag=laravel-errors and then edit the newly created resources/views/errors/404. blade.

How can I get current action name in laravel?

echo Route::getActionName();


1 Answers

You just have to look at the Official documentation.

HTTP Exceptions

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:

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

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.

Update

Seems that this function has been removed and will soon be replaced as written here. A "workaround" can be creating a 404 response.

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:

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 documentation and the Symfony API documentation.

like image 80
IlGala Avatar answered Sep 27 '22 23:09

IlGala