If I write this line on routes/web.php file
dd( app()['config']["auth.guards.web"] );
It outputs this:
array:2 [▼ "driver" => "session" "provider" => "users" ]
That’s cool, but my question is since app()['config'] returns an object so how this ["auth.guards.web"]works? Even there is no index with that name!
Outside of Laravel I tried to write a class named Test so that it returns the same output but I got an error! Which is,
Fatal error: Uncaught Error: Cannot use object of type Test as array
Can anyone explain it with core php?
Advertisement
Answer
Laravel contains a very useful helper function called Arr::get() which retrieves a value from a deeply nested array using “dot” notation. Laravel framework uses this helper function everywhere it needs to get the values inside a deep array; e.g configs and translation and so on.
Example;
config('database.driver');
__('validation.error');
trans('validation.error');
On the other hand, app()['config'] returns IlluminateConfigRepository instance which implements ArrayAccess. As long as get method of IlluminateConfigRepository class uses Arr::get method, you can use “dot” notated array access along with app()['config'].
For more details, see these links; https://laravel.com/docs/8.x/helpers#method-array-get https://github.com/laravel/framework/blob/8.x/src/Illuminate/Config/Repository.php#L53