Home > Mobile >  Laravel: Strange bug with cache:clear and request()
Laravel: Strange bug with cache:clear and request()

Time:11-30

I'm experiencing a weird bug with artisan cache:clear.
Here's my function:

public static function getSegments()
{
    $json = file_get_contents(base_path().'/routes/segments/post.json', true);
    $segments = json_decode($json);
    $locale = request()->segment(1);

    return $segments->$locale;
}

Now if I run: php artisan cache:clear

I get this error:

ErrorException Undefined property: stdClass::$

➜ 93▕ return $segments->$locale;

The $json value is a perfectly valid object and the value returned by request()->segment(1) is correct.
Even weirder, the app works fine in the browser, there is no error or whatever.

Now if I set the $locale variable like this:

$locale = 'fr';

Everything works.

So I try with strval just in case:

$locale = strval(request()->segment(1));

But the error is back again.
Can someone tell me where is the problem ?

CodePudding user response:

cache:clear is an Artisan command, so there won't be a request segment to get, and $locale won't have an actual value. You can check to see if the command line is hitting that function and defaulting the value if it is:

$locale = \App::runningInConsole() ? 'fr' : request()->segment(1);
  • Related