How to show the 404 page? - php

I would like to show 404 page in Laravel 5 while MethodNotAllowedHttpException throws.
Can anyone help me in this regard?

Add this to app/Exceptions/Handler.php:
public function render($request, Exception $e)
{
if ($e instanceof MethodNotAllowedHttpException)
{
abort(404);
}
return parent::render($request, $e);
}
Then edit resource/views/errors/404.blade.php to personalize the page.

Simple: all you have to do is create a template at resources/views/errors/404.blade.php.
You can create views for other HTTP status codes if you’re feeling that way inclined, such as a 403.blade.php for Forbidden exceptions and so on.

I'm working on Laravel 5.2 and the answer given here worked for me.
You need to modify the render method in the app/Exceptions/Handler.php.
public function render($request, Exception $e)
{
if ($e instanceof \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException)
{
return response(view('errors.404'), 404);
}
return parent::render($request, $e);
}
abort(404) did not work for me. Looks like this method is removed in Laravel 5.

That's a 405 error, easiest way would be to create a new view in resources/views/errors/405.blade.php..
If you need fine control over what is displayed then you'll have to use the Handler.php to modify what's returned.

Related

How do i redirect user to a error page when there is an error?

I would like to know how can i redirect users to an error page when there is an error with the code.
below are the error which i m trying to redirect
Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_PARSE)
syntax error, unexpected 'dd' (T_STRING)
or when a page is not valid or when there is an undefined variable
I have tried adding the following commands in app/Exceptions/Handler.php
public function render($request, Exception $exception)
{
if ($exception instanceof CustomException) {
dd('hi');
}
if ($exception instanceof ModelNotFoundException or $exception instanceof NotFoundHttpException) {
dd('hi1');
}
return parent::render($request, $exception);
}
and also have tried bellow
public function render($request, Exception $exception)
if ($this->isHttpException($exception)) {
if ($exception instanceof NotFoundHttpException) {
dd('hi2);
}
}
}
But i m still getting the error message. How can i do it so that all error will be redirected to an error page (for now i will just see the dd() instate of redirecting to the page)
Instead of dd('hi') my whole app I spend the time looking at the exception handling.
When prepending dd($e);
in the renderForConsole() method, before line
https://github.com/laravel/framework/blob/5.7/src/Illuminate/Foundation/Exceptions/Handler.php#L467
than all relevant information is printed out.
This helped me to find the error reletively fast. Maybe this is helpfull for anyone in the meantime, although it's a terrible dump of the whole application state, but was extremely helpful for me, as the stacktrace is contained.

Laravel - One view for all possible errors

So I want to cover all posible and unexpected errors (like 401, 404, 500) with just one view. I want the same view to show up on all possible errors. I came up with a solution - to copy/paste the same code and just name the views with different error codes. But that seems stiff and wrong. Is there a better way of achieving this?
In the file app/Exceptions/Handler.php you can change what happens when an exception is thrown. In particular there's a render method in there that you can use to catch all the exceptions in an application.
public function render($request, Exception $e)
{
// Handle your error here. Perhaps you can show a view
// when a condition is met. Anything that isn't caught
// here will be handled by Laravel with the
// parent::render call below
return parent::render($request, $e);
}
The parent::render($request, $e) is where Laravel would normally show it's exception/oops page. So by overriding this method you can catch all application errors, including 404, 401 etc.
A cleaner way to achieve this effect is by modifying Laravel's exception handler.
Modify App\Exceptions\Handler to catch every error and return your shared custom error page.
/**
* Render an exception into an HTTP response.
*
* #param \Illuminate\Http\Request $request
* #param \Exception $e
* #return \Illuminate\Http\Response
*/
public function render($request, Exception $e)
{
if ($e instanceof NotFoundHttpException) {
return response()->view('errors.custom', [], 404);
}
return parent::render($request, $e);
}
Some customization may be required to fully meet exactly what & how you want data passed to your shared custom view.
Pass the error code to your view on the handler, and display the code on your page, use a switch to handle all the messages depending on the error code.
You can create one unique view (default to 404 error), use try catch in your code to capture other errors and call to this view with parameters so you can change the 404 default error to other error.

How to handle MethodNotAllowedHttpException

I'm looking to handle a MethodNotAllowedException. I've viewed other answers available on here that to create what i think should handle this in the exceptions/handler.php class. This is what i came up with.
public function render($request, Exception $e)
{
if ($e instanceof MethodNotAllowedHttpException) {
\Auth::logout();
\Session::flush();
return redirect()->('/')->withErrors(['error' => 'Something went wrong']);
}
return parent::render($request, $e);
}
However where i used to get an error before, all i recieve now is a blank page on the page where i usually recieve an error and a user is not logged out nor are they redirected. Am i placing this handler in the right place and if so, is the function shown below correct?
Thanks

Simple error response in Laravel 5 while JSON request

I have project on Laravel 5, and I need to do async request via jQuery's $.ajax method.
Laravel can catch exception, and then it render special error template with it's own styles and markup.
But for async requests this html-code is redundant.
Is there a way to generate error response without laravel's markup on async requests?
I guess you wanted this to write the web service.
To handle this
Goto app/Exceptions/Handler.php :
And change this function
public function render($request, Exception $e)
{
return parent::render($request, $e);
}
to
public function render($request, Exception $e)
{
if ($this->isHttpException($e))
{
return $this->renderHttpException($e);
}
else
{
return parent::render($request, $e);
}
}
Also if you need to customize in the webview
Change your 404 blade \resources\views\errors\404.blade.php here

Exception handling in Symfony2

How to handle exceptions manually in symfony2 in case like this:
class Foo {
// ..
public function __toString()
{
try {
$this->render();
}
catch (\Exception $e)
{
// log $e
// handle $e - display 500 error page in prod mode
}
}
}
Redirect? But how.
EDIT
My solution so far is to dispatch exception event, and it works.
$dispatcher->dispatch(KernelEvents::EXCEPTION, $event)
But i requires creating an event. Is there some better solution?
You can also define your own exception controller and perform needed behavior.
Check How to customize Error Pages and Configuration: exception_controller
UPD
Creating event listener is a good solution to catch exceptions.
We can also customize specific TWIG error templates according to the HTTP status ( returned )code. For example, create a app/Resources/TwigBundle/views/Exception/error404.html.twig template to display a special page for 404 (page not found) errors.
OR, we can also customize exceptionController.

Categories