When using the following Laravel helper function in a view:
abort( 404 );
Instead of displaying the default Laravel 404 error page, I'm getting:
Symfony\Component\HttpKernel\Exception\NotFoundHttpException
Stack Trace:
public function abort($code, $message = '', array $headers = [])
{
if ($code == 404) {
throw new NotFoundHttpException($message);
}
throw new HttpException($code, $message, null, $headers);
}
How can I get abort( 404 ); to display the stock Laravel 404 error page?
Related
I have a Project Laravel Jetstream with inertia i need change the error page Like This to Vue page
I use https://inertiajs.com/error-handling but not work
public function render($request, Throwable $e)
{
$response = parent::render($request, $e);
if (/*!app()->environment(['local', 'testing']) &&*/in_array($response->status(), [500, 503, 404, 403])) {
return Inertia::render('Error', ['status' => $response->status()])
->toResponse($request)
->setStatusCode($response->status());
} else if ($response->status() === 419) {
return back()->with([
'message' => 'The page expired, please try again.',
]);
}
return $response;
}
This work only in production
to work in local
remove !app()->environment(['local', 'testing']) from the code
I want to create custom 404 page for prefix backend. but it is only show front site 404 error page.. when backend/404-url i want to get with backend. layout 404..
Route::prefix('/backend')->group(function(){
Route::get('/test','BackController#index');
Route::get('/home','BackController#home');
Route::resources([
'categories' => 'CategoryController',
'posts' => 'PostController',
'users'=>'UserController'
]);
});
You should add/modify the render method in app/Exceptions/Handler.php to return a json response for an ajax request or a view for a normal request if the exception is one of ModelNotFoundException or NotFoundHttpException.
so your code will be look like this:
protected function renderHttpException(HttpException $e) {
$status = $e->getStatusCode();
if(Request::is('/backend/*')) { //Chane to your backend your !
return response()->view("backend/errors.{$status}", ['exception' => $e], $status, $e->getHeaders());
}else {
return response()->view("errors.{$status}", ['exception' => $e], $status, $e->getHeaders());
}
}
some links to help you achieve this:
Laravel Documentation on errors
Scoth.io tutorial for creating a 404 page using custom exception handler
I am creating a facebook application in Laravel 4, the problem is it is giving me following error while running as a facebook application
Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException
but the same thing is working fine out of facebook.I followed this tutorial
http://maxoffsky.com/code-blog/integrating-facebook-login-into-laravel-application/
Following is my routes.php
Route::get('home', 'HomeController#showWelcome');
Route::get('/', function() {
$facebook = new Facebook(Config::get('facebook'));
$params = array(
'redirect_uri' => url('/login/fb/callback'),
'scope' => 'email,publish_stream',
);
return Redirect::to($facebook->getLoginUrl($params));
});
Route::get('login/fb/callback', function() {
$code = Input::get('code');
if (strlen($code) == 0) return Redirect::to('/')->with('message', 'There was an error communicating with Facebook');
$facebook = new Facebook(Config::get('facebook'));
$uid = $facebook->getUser();
if ($uid == 0) return Redirect::to('/')->with('message', 'There was an error');
$me = $facebook->api('/me');
return Redirect::to('home')->with('user', $me);
});
Edit: I have checked chrome console and getting this error
Refused to display 'https://www.facebook.com/dialog/oauth?client_id=327652603940310&redirect_ur…7736c22f906b948d7eddc6a2ad0&sdk=php-sdk-3.2.3&scope=email%2Cpublish_stream' in a frame because it set 'X-Frame-Options' to 'DENY'.
Put this somewhere inside bootstrap/start.php:
$app->forgetMiddleware('Illuminate\Http\FrameGuard');
You can read this post:
http://forumsarchive.laravel.io/viewtopic.php?pid=65620
Try changing your callback to be Route::post not Route::get. If my memory serves me correctly, Facebook places a POST request, not a GET request.
<?php
Route::get('login/fb/callback', function() {
$code = Input::get('code');
if (strlen($code) == 0) {
return Redirect::to('/')->with('message', 'There was an error communicating with Facebook');
}
$facebook = new Facebook(Config::get('facebook'));
$uid = $facebook->getUser();
if ($uid == 0) {
return Redirect::to('/')->with('message', 'There was an error');
}
$me = $facebook->api('/me');
return Redirect::to('home')->with('user', $me);
});
After looking at the link that you sent, the error actually tells you what is wrong.
REQUEST_URI /
REQUEST_METHOD POST
Loading that page is making a POST request to /, and like I suggested above, you'll need to change the route for the index to Route::post.
I am creating an api using Laravel 4.1. I am having problem with authentication and custom errors. I want to check first if the user is authenticated and then show error message. For example localhost:8080/trips/1 is not a valid a resource; if I go to that url it giving me 404 not found error even though I am not authenticated. My point is to check the authentication first then the errors. I am using laravel http basic authentication. Here is my filter code:
Route::filter('api.auth', function()
{
if (!Request::getUser())
{
App::abort(401, 'A valid API key is required');
}
$user = User::where('api_key', '=', Request::getUser())->first();
if (!$user)
{
App::abort(401);
}
Auth::login($user);
});
Here is my custom errors:
App::error(function(Symfony\Component\HttpKernel\Exception\HttpException $e, $code)
{
$headers = $e->getHeaders();
switch ($code)
{
case 401:
$default_message = 'Invalid API key';
$headers['WWW-Authenticate'] = 'Basic realm="REST API"';
break;
case 403:
$default_message = 'Insufficient privileges to perform this action';
break;
case 404:
$default_message = 'The requested resource was not found';
break;
default:
$default_message = 'An error was encountered';
}
return Response::json(array(
'error' => $e->getMessage() ?: $default_message,
), $code, $headers);
});
Here is my routes:
Route::group(array('before' => 'api.auth'), function()
{
Route::resource('trips', 'TripController', array(
'except' => array('create', 'edit')
));
});
The error code is executing before the filters thats why I am getting 404 error instead of getting 401. Is there any way to execute filter first then the error ?
I got this issue too and the way I worked around it (though I was not a fan at all) was doing the following inside the group(s) that don't contain a route sending requests to a controller's index method:
Route::any('/', function(){});
Granted, this shouldn't be how it works, the filter should trigger no matter what.
I am able to use the Event::Override function successfully with the 404 event but not the 500 event. I simply wish the event to redirect to the front page with a flash message, as I am able to do fine with 404 events.
Is there something else I need to do to get the 500 events also redirecting to my front page? see code below in routes.php file:
Event::listen('404', function() {
return Response::error('404');
});
Event::listen('500', function() {
return Response::error('500');
});
Event::override('404', function() {
Session::flash('error', '<strong>Error 404 - Not Found.</strong> The page you are looking for is either <u>invalid</u> or <u>may no longer exist</u>. You are now back at our home page.');
return Redirect::to_route('home');
});
Event::override('500', function() {
Session::flash('error', '<strong>Error 500 - Internal Server Error.</strong> Something went wrong on our servers, apologies for that. Please contact us if this persists.');
return Redirect::to_route('home');
});
any ideas?
I've been able to work around this by updating the 500 error file directly in the \application\views\error folder.
One of my mobile application need a error as json format. I got it's solution from laravel forum . For catching http error
App::error( function(Symfony\Component\HttpKernel\Exception\HttpException $exception) {
$code = $exception->getStatusCode();
// you can define custome message if you need
return Response::json( array(
'message' => $exception->getMessage() ,
'code' => $code ,
) , $code);
});
Catch unhandled like db error, ..
App::error(function(Exception $exception , $code )
{
return Response::json( array(
'message' => $exception->getMessage() ,
'code' => $code ,
), $code );
});