-3

I'm building an admin section for a Laravel 10 app using the boilerplate Breeze routes and templates (for now) and I have a group of prefixed routes that I want to return a 404 error for in the following cases:

  1. there is NO signed in user
  2. if there IS a signed in user, they are not an admin

In other words, the ONLY time the prefixed routes should produce anything substantial is if the authenticated user is an admin. Otherwise, I want it to appear as the routes don't even exist.

I am at my wits end trying to return the 404 error view and status code from the middleware. I have been tinkering with the code and reading documentation for HOURS now to no avail, and yet I get the feeling the answer is right in front of me but I'm missing something.

This is code I have in the web.php routes file:

Route::prefix('admin')->middleware(['admin'])->group(function() {
    Route::get('/', [AdminDashboardController::class, 'index'])->name('admin');
});

And here's what I have in the handle() function for the admin middleware:

public function handle(Request $request, Closure $next): Response
{
    if ($request->user() === null || $request->user()->role !== 'admin') {
        return response('This page cannot be found', 404, []);
    }
    
    return $next($request);
}

This code works just fine. BUT, I want to return the 404 page and HTTP 404 status code rather than a plain text page with the response message. If I try to return anything EXCEPT the above it just seems to skip right on to the next bit of middleware. As in, it attempts to render the Breeze dashboard component as if the user was signed in, even though the user isn't signed in.

Could anybody please guide me on how I can return the 404 error page and status code in place of the current response above?

8
  • replace the return response('This page cannot be found', 404, []); with abort(404); Commented Feb 6, 2024 at 23:40
  • @N69S, I've tried this, and I've just tried again. By aborting with 404, it tries to render the resources / views / layouts / navigation.blade.php component that comes standard with Breeze. At the moment, the rendering fails because I'm using slightly different properties on the User model, but I'm not too concerned about that bit (I at least know why it's throwing an error). I'm more concerned that it's trying to render the Breeze dashboard to start with rather than aborting with the 404 error. Commented Feb 6, 2024 at 23:46
  • did you try returning a view response? Response::view('your.404', $dataArray, 404) ? Commented Feb 7, 2024 at 0:05
  • 1
    stop returning that view then and return your own ... ? its the view that has the issue, not you returning it or the 404 ... also exceptions aren't returned, they are thrown Commented Feb 7, 2024 at 1:40
  • 1
    @N69S and others: I managed to get the abort(404) working only after I hand-coded my AuthController rather than using the code provided by Breeze. Could have been due to conflicting middleware or views. Everything works as intended now, thanks everyone :) Commented Feb 7, 2024 at 15:01

1 Answer 1

2
public function handle(Request $request, Closure $next): Response
{
    if ($request->user() === null || $request->user()->role !== 'admin') {
        //remove this return response('This page cannot be found', 404, []);
        abort(404); // replace this
    }
    
    return $next($request);
}

And here reference for design your own 404 error handling page https://magecomp.com/blog/create-custom-error-page-laravel-10/

Sign up to request clarification or add additional context in comments.

2 Comments

Marking as correct as this is technically the correct answer. I think my issue might be due to conflicting middleware because when I hand-coded the authentication controller rather than using the boilerplate provided by Breeze I didn't encounter this issue. Thanks to all who helped and commented on my original post as well.
Route::get('/', [AdminDashboardController::class, 'index'])->name('admin')->middleware('middlewareName'); you can also set middleware this way..try this way and check if work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.