Support the ongoing development of Laravel.io →
posted 6 years ago

I have an application that, unfortunately, loads a view and renders a visual error on the client side (angular.js). This was a design architecture flaw, because pages that should return a 404, in fact, return a 200.

What this means is that if someone goes to http://thesite/subdirectory/foo and foo doesn't exist, the page displays a message ("Oops! This product doesn't exist!"). This message is generated client-side with angular.js.

Here's what's going on as best as I can tell:

Route

...
//item or equipment pages
Route::get('/{dealer}/used-inventory/{entity_slug}/{entity_id}', [
'as' => 'dealerSubsites.usedInventoryShow', 'uses' => 'FrontEndInventoriesController@show',
]);
...

Controller (FrontEndInventoriesController)

public function show(DealerSubsite $dealer, $entity_slug, $entity_id) {
    return view('pages.subsites.inventories.show', compact('dealer', 'entity_id'));
}

View (pages.subsites.inventories.show)

<div ng-if="!(itemNotFound && itemLoaded)">
 //Stuff to render client-side via angular.js if condition evaluates to false
</div>
<div ng-if="itemNotFound && itemLoaded">
// Stuff to render client-side via angular.js if condition evaluates to true -- this should be a 404
</div>

I got around this by passing the route through middleware that tests for the condition and then throws a 404 if needed. This kind of works. However, the custom error page doesn't make sense in the context of this part of the application.

What I need to do is serve up the content in pages.subsites.inventories.show while also pushing a 404 status. Is that even possible? Or will I need to create a custom error page?

Last updated 3 years ago.
0
moderator

You should move the not found logic to the controller and there you can set the statuscode for the view.

public function show(DealerSubsite $dealer, $entity_slug, $entity_id) {
    $statusCode = 200;
    // do something to decide if it is found or not
    if (!$found) {
        $statusCode = 404;
    }
    return response()->view('pages.subsites.inventories.show', compact('dealer', 'entity_id'), $statusCode);
}

See: https://laravel.com/docs/5.6/responses#view-responses

Last updated 6 years ago.
0

Perfect. Thank you for the detailed response, as well as for the link.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Eric Brown eb112 Joined 27 Aug 2018

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.