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 in Laravel by kartik
• 35,300 points
123 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 by Niroj
• 76,540 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 in Laravel by Niroj
• 76,540 points
37 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 in Laravel by Helloworld
• 140 points
148 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 in Laravel by Niroj
• 76,540 points
154 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 20 in Laravel by Niroj
• 76,540 points
148 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 in Java-Script by Niroj
• 76,540 points
437 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 in Java-Script by Niroj
• 76,540 points
670 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 in PHP by Niroj
• 76,540 points
88 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 in Node-js by Niroj
• 76,540 points
87 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 in Laravel by Niroj
• 76,540 points
372 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 in Laravel by Niroj
• 76,540 points
954 views