How to create custom error pages laravel
Hi!
Have created a application in laravel8 and vue
If i try to access a page that i not allowed to enter. I get error 403 from the controller in the console How can i insted show this error on screen (and not show datatable with No data") ?
Want to use this view (showing error messages) for every error like - 403, 402 .....
@martinbean Thank you :)
Hi! Tryed it, but error page dosent show up ?
@birdietorerik They will if you set APP_DEBUG to false.
@martinbean Hi! Dosent help ? But i am showing a VUE file, maby not working if now blade file ?
@birdietorerik What do you mean?
If you make a request to a URL and Laravel encounters and error, then it will return the appropriate error view.
@martinbean Hi! Working fine, if it comes from blade but not working when come from:
example this from controller:
public function index()
{
abort_if(Gate::denies('country_access'), Response::HTTP_FORBIDDEN, '403 Forbidden');
return new CountryResource(Country::advancedFilter());
}
@birdietorerik That’s an awful long-handed way to check a gate…
public function index()
{
Gate::authorize('country_access');
}
But if you’re dealing with models then you probably should be creating a corresponding policy instead of random, ad hoc gates:
class CountryController extends Controller
{
public function __construct()
{
$this->authorizeResource(Country::class);
}
public function index()
{
// CountryPolicy::viewAny will automatically be called here
// If policy fails, 403 error will be thrown
return CountryResource::collection(Country::advancedFilter());
}
}
https://laravel.com/docs/8.x/authorization#authorizing-or-throwing-exceptions
@martinbean Hi!
Tryed what you say, but still dosent trigger the 403 customize error 403.blade.php file ?
@birdietorerik What does it show? 🤷♂️
@martinbean Hi!
It show Datatable, with NO DATA
@birdietorerik If this is some sort of Vue application then you need to handle the status code from the API request you’re making. If you get a 403 from the API, then you need to display an error message or whatever you want to happen.
@martinbean Hi!
I want to display error.blade file on screen.
Have tested abort(403); in controller Just to test, but only get error message in console:
<?php
namespace App\Http\Controllers\Api\V1\Admin;
use App\Http\Controllers\Controller;
use Gate;
use Illuminate\Http\Response;
class WalkieTalkieApiController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
* Check if user is logged in
*/
public function __construct()
{
$this->middleware(['auth', 'verified']);
}
public function index()
{
//abort_if(Gate::denies('walkie_talkie_access'), Response::HTTP_FORBIDDEN, '403 Forbidden');
abort(403);
return response()->json([
'data' => [
'message' => 'It works! This message is retrieved from an API. Your component is ready to be implemented.',
],
]);
}
}
This give me only error in console:
Failed to load resource: the server responded with a status of 403 (Forbidden)
So how to get this to work ?
@birdietorerik I’ve already told you what to do.
If you’re using Vue to interface with an API, the Laravel error page isn’t going to magically appear on screen. You need to show an error if you make an API request and that endpoint returns a 403 error or whatever.
Please or to participate in this conversation.