#Apply middleware to Custom HTTP Error Pages

98 messages · Page 1 of 1 (latest)

wet hill
#

Since laravel handles exceptions automatically when creating a errors folder in views, I can't really apply any middlewares

fiery void
#

The link you posted literally tells you how to do this - unless i don't get the question

#

You can create your own exception and throw it - you can create your own middleware and throw said exception if you wish

#

php artisan vendor:publish --tag=laravel-errors from the link you posted means you can edit this page to be whatever you want

wet hill
#

So what I'm doing is, including navbar/footer which use the custom locale middleware to translate the text

#

Wrapping the 404.blade.php in a guest layout

minor rampart
#

@wet hill What if a middleware is the cause of the error…?

wet hill
#

The issue here is that I can't apply any middlewares to 404.blade.php

#

Let me show you

fiery void
#

indeed you can't middleware a middleware that's already thrown the exception in the first place??

wet hill
#

I have this inside my 404.blade.php

<x-guest-layout>
    //whatever 404 error message
</x-guest-layout>
fiery void
#

No because it's already thrown

wet hill
#

Am I unable to overwrite this?

fiery void
#

you can overwrite whats displayed to the user

#

see above and your link you shared

wet hill
#

Right, but translatable strings won't work without my custom locale middleware

fiery void
#

ok... so pass the request through your middleware in the first place

wet hill
#

How could I pass the request through my own middleware when laravel handles the errors folder

#

Do I overwrite through handler.php?

fiery void
#

no

#

in your route define the middleware

wet hill
#
Route::middleware('locale')->group(function () {
    //
});
#

Right

fiery void
#

actually not quite my apologies

wet hill
#

That's the thing

#
public function handle(Request $request, Closure $next): Response
    {
        if (session()->has('locale')) {
            App::setLocale(session()->get('locale'));
        }

        return $next($request);
    }
#

This is the custom locale middleware that I'm using

fiery void
#

sorry lets slow down I was a bit confused

wet hill
#

Not quite custom, just basic but it's not a default middleware given with breeze

fiery void
#

your picture was indeed correct - so if you get a 404 here and you've published the 404 how are you telling that blade to use new text

#

that's the problem to solve - that 404 is totally yours to play with if you've published it

wet hill
#

You mean publish laravel's default error pages?

fiery void
#

it's the same as any other blade file in your app

#

yes as per your original link

#

php artisan vendor:publish --tag=laravel-errors

#

they will be published and you can do what you want with them - i haven't used blade in a little while but if i remember righty something like {{__value}} to use translations

#

**don't change the filenames after publish or Laravel will use the default - it's just an override

wet hill
#

That still doesn't work

#

My current locale is set to NL (dutch) but it still shows EN (english)

fiery void
#

did you change the blade?

#

i mean in the same way you change it for all other translation pages

wet hill
#

Yup

#

It uses the fallback instead of what I currently have set

fiery void
#

there is something missing..

#

bit consufed in whats not working - i mean after publish did you stop and restart the server

#

I mean that and vite

wet hill
#

Yeah that isn't the issue though

#

I believe translatable strings aren't working due to the fact I can't assign my locale middleware

fiery void
#

in the end that page will render if you published it and edited it

#

did you try changing the text just to see if it's rendering your page

wet hill
#

Yes that works

fiery void
#

ok so it does all work it's just the translation part that doesn't

wet hill
#

Exactly

fiery void
#

hmm i mean when you set locale for the app (in your other pages) - from where do you get the translated text?

wet hill
#

lang directory

fiery void
#

ok so in there did you define the translations for the words in the 404

wet hill
#

i'm pretty sure you are aware that you need a middleware to display the correct locale right

fiery void
#

hmm i wonder if there is a conflicting translation entry - i mean if you are actually in NL then local should come as correct from the browser i guess - i mean i'm impressed that it took you 4 minutes to publish then find the file and modify that then again modify the guest-layout component

#

I run out of ideas on it really, I mean now you have the file in your control which was the first question, I don't know why doesn't translate maybe others have suggestions...

wet hill
#

I have had past experience with this, however I haven't gotten as deep as translating the error pages, which makes sense if the user preferes dutch for example

#

Thanks though

fiery void
#

Well that's always a debate, I mean I'm in DE working with NL speaking BE folks and I'm from GB but in the end the browser handles all of this these days...i tend not to spend too much time on it development wise for that reason - but yeah always nice if a site can use the users locale (except for me where chrome always makes it from DE to English) - in the end you can also assume that NL users will understand 'not found' in English - but i totally get it, that's not really ideal when rest of the site is in Dutch...sorry i couldn't be more help but lets see i'm curious on this now too tbh

wet hill
#

Haha all good, appreciate the effort 👍

fiery void
#

I'm working on a small side project i'm going to try this sh*t out

wet hill
#

😂

#

it returns the 'locale' key in config/app.php unfortunately not the current locale tho 🥹

fiery void
#

this would be ok aside from the rest of your site working correct

#

ahh

wet hill
#

yup

fiery void
#

so you know what to do now right?

#

did you DD your session locale - this literally should work if you have a correct value in translation...maybe change fallback to NL or some other and see what happens

#

At least then you'd know it IS using the fallback which means something is off in your nl.json

wet hill
#

it doesn't use the fallback

wet hill
fiery void
#

yeah but your default is set in any given request by the set locale you do in the middleware

#

...normally

#

Can it just be that you need to clear cache or something or at least dd the result of getLocale

wet hill
fiery void
#

yeah that's what i meant before about dumping out the locale maybe it's not set

wet hill
#

The isssue here is that the 404.blade.php that laravel generates doesn't use this middleware

#

So it never sets the preferred locale and instead goes straight to fallback

fiery void
#

i think it's using it - i think it's more about the locale

#

**but hey i'm guessing now

wet hill
#

I dumped just now, it didn't set it just went back to fallback :/

#

I tried

public function register(): void
    {
        $this->renderable(function (NotFoundHttpException $e, Request $request) {
            if (session()->has('locale')) {
                App::setLocale(session()->get('locale'));
            }
        });
    }
#

But that doesn't work either

#

(in handler.php

fiery void
#

that's progress

#

paybe dump the whole session - if there's no locale then there's the answer - but i don't know why it's not set when you say it is set for other pages

wet hill
#

The other pages utilize my custom middleware

fiery void
#

sorry i meant /nl/messages.php

#

aswell as nl.json

#

also don't forget should be nl_NL or nl_BE

split ibex
#

Sorry for posting on a slightly dead thread - but this is my issue and unfortunately the original link as been deleted, anyone got what it is?