jeudi 21 mars 2019

Laravel HTTP tests and request attributes

On my app I add attributes to the HTTP request so I can use it later. My app is a multi domain app (.co.uk, .dk, .de). I findout the domain in the RouteServiceProvider and add the detected language to the HTTP request so I can load the data according to the language and some other things.

$website = Website::where('domain', '=', request()->getHttpHost());
request()->attributes->add(['website' => $website]);

Then in my controller or anywere else I just have to query the request

if (!$request->attributes->has('website')) {
    \Log::error('Abort HTTP request: invalid website: ' . request()->getHttpHost());
    abort('500');
}
$language = $request->attributes->get('website')->language();

When testing my app the code execute normally (website is found in the RouteServiceProvider) but then it break in the controller:

testing.ERROR: Abort HTTP request: invalid website

How can I fix this?

Aucun commentaire:

Enregistrer un commentaire