#Apply middleware to Custom HTTP Error Pages
98 messages · Page 1 of 1 (latest)
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
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
@wet hill What if a middleware is the cause of the error…?
indeed you can't middleware a middleware that's already thrown the exception in the first place??
I have this inside my 404.blade.php
<x-guest-layout>
//whatever 404 error message
</x-guest-layout>
No because it's already thrown
Am I unable to overwrite this?
Right, but translatable strings won't work without my custom locale middleware
ok... so pass the request through your middleware in the first place
How could I pass the request through my own middleware when laravel handles the errors folder
Do I overwrite through handler.php?
This part is a bit strange though
actually not quite my apologies
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
sorry lets slow down I was a bit confused
Not quite custom, just basic but it's not a default middleware given with breeze
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
You mean publish laravel's default error pages?
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
That still doesn't work
My current locale is set to NL (dutch) but it still shows EN (english)
did you change the blade?
i mean in the same way you change it for all other translation pages
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
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
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
Yes that works
ok so it does all work it's just the translation part that doesn't
Exactly
hmm i mean when you set locale for the app (in your other pages) - from where do you get the translated text?
lang directory
ok so in there did you define the translations for the words in the 404
i'm pretty sure you are aware that you need a middleware to display the correct locale right
i have
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...
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
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
Haha all good, appreciate the effort 👍
I'm working on a small side project i'm going to try this sh*t out
😂
it returns the 'locale' key in config/app.php unfortunately not the current locale tho 🥹
yup
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
it doesn't use the fallback
it uses the default locale as previously mentioned
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
The current middleware checks if a session has a locale set, if it doesn't it uses the fallback
yeah that's what i meant before about dumping out the locale maybe it's not set
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
i think it's using it - i think it's more about the locale
**but hey i'm guessing now
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
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
The other pages utilize my custom middleware
I'm losing braincells
sorry i meant /nl/messages.php
aswell as nl.json
also don't forget should be nl_NL or nl_BE
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?