How throw forbidden exception from middleware in laravel5

0 votes

I am writing a middleware in laravel 5. I want to throw a forbidden exception with code 403 from middleware. My middleware function is given below:

use Exception;

public function handle($request, Closure $next)
{
    if (!Auth::check()) {
        throw new Exception("Access denied", 403);
    }
    return $next($request);
}

I am calling my middleware from controller and I am getting error message with code 500 but not 403. How can I resolve this?

Oct 23, 2020 in Laravel by kartik
• 37,520 points
5,476 views

1 answer to this question.

0 votes

Hello @kartik,

You can simply use the abort() helper. (Or App::abort())

public function handle($request, Closure $next) {
    if (!Auth::check()) {
        abort(403, 'Access denied');
    }
    return $next($request);
}

You can handle these exceptions inside App\Exceptions\Handler by overriding render() For example:

public function render($request, Exception $e)
{
    if($e instanceof HttpException && $e->getStatusCode() == 403){
        return new JsonResponse($e->getMessage(), 403);
    }    
    return parent::render($request, $e);
}
answered Oct 23, 2020 by Niroj
• 82,840 points

Related Questions In Laravel

0 votes
1 answer

How change view folder in Laravel5?

Hello @kartik, See line 16 of config/view.php (the "View Storage ...READ MORE

answered Sep 25, 2020 in Laravel by Niroj
• 82,840 points
1,093 views
0 votes
2 answers

How to delete file from public folder in laravel?

I just want to add @niroj answer  Use ...READ MORE

answered Nov 5, 2020 in Laravel by Helloworld
• 140 points
27,501 views
0 votes
1 answer

How to select year and month from the created_at attributes of database table in laravel 5.1?

Hello @kartik, There are date helpers available in ...READ MORE

answered Sep 30, 2020 in Laravel by Niroj
• 82,840 points
15,439 views
0 votes
1 answer

How to gett selected values from a multiple select form in Laravel?

Hello @kartik, First, if you want to have ...READ MORE

answered Oct 21, 2020 in Laravel by Niroj
• 82,840 points
7,380 views
0 votes
1 answer

jQuery AJAX fires error callback on window unload - how do I filter out unload and only catch real errors?

Hello, In the error callback or $.ajax you have three ...READ MORE

answered Apr 27, 2020 in Java-Script by Niroj
• 82,840 points
4,190 views
0 votes
1 answer

How do I pass command line arguments to a Node.js program?

Hello @kartik, If your script is called myScript.js ...READ MORE

answered May 5, 2020 in Java-Script by Niroj
• 82,840 points
3,243 views
0 votes
1 answer

Error:Issue when trying to use IN() in wordpress database

Hello @kartik, Try this code : // Create an ...READ MORE

answered May 8, 2020 in PHP by Niroj
• 82,840 points
1,116 views
+2 votes
1 answer

How do I debug Node.js applications?

Hello @kartik, Use node-inspector  from any browser supporting WebSocket. Breakpoints, ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,840 points
1,028 views
0 votes
1 answer

What is middleware? How can we register middeleware in Laravel?

Middleware acts as a bridge between a ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
2,767 views
0 votes
1 answer

How to get data from Laravel backend and display in Vue/Nuxt frontend

Hello, You have to use the package dotenv. Then ...READ MORE

answered Apr 8, 2020 in Laravel by Niroj
• 82,840 points
3,231 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP