Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

smuzammilali's avatar

How to access Auth user details in config files?

I'm looking ways to access the login user details under config files.

Basically I would like manipulate the config/imagecache.php paths as per login user.

Example:

    'paths' => array(
        public_path('image/'.Auth::user()->username),
        public_path('image')
    ),

Or something like this:

config(['imagecache.paths' => [public_path('image\\'.Auth::user()->username)]]);
0 likes
10 replies
akaamitgupta's avatar

You can set configuration values at runtime by passing an array to the config helper:

config(['imagecache.paths' => [
        public_path('image/'.Auth::user()->username),
        public_path('image')
    ]
]);

And to set it in every request, you can do this in a service provider. Add the following code in your boot method of App\Providers\AppServiceProvider class:

public function boot()
{
    if (auth()->check()) {
        config(['imagecache.paths' => [
                public_path('image/'.Auth::user()->username),
                public_path('image')
            ]
        ]);
    }
}
3 likes
smuzammilali's avatar

@akaamitgupta thank you for the feedback but it's not working?

I have pasted this code uner boot method of App\Providers\AppServiceProvider class:

    public function boot()
    {
        //
        if (auth()->check()) {
            config(['imagecache.paths' => [
                    public_path('image/'.Auth::user()->username),
                    public_path('image')
                ]
            ]);
        }
        var_dump(Auth::user());
        var_dump(config('imagecache.paths'));
    }

Still I can't access Auth::user the above code give this:

var_dump(Auth::user());

B:\wamp64\www\Laravel\app\Providers\AppServiceProvider.php:25:null

var_dump(config('imagecache.paths')); still showing the default paths:

0 => string 'B:\wamp64\www\Laravel\public\upload' (length=35)

akaamitgupta's avatar

The above code will only work when you are logged-in. But your var_dump(Auth::user()); returns null which means you are logged out. So please make sure you are logged-in before testing it.

ohffs's avatar

Authentication info, afair, isn't available at that stage in the app. A more basic way to do it might be to have a helper on the user model that returns their path, like :

public function cachePath()
{
  return public_path('image/'.$this->id);
}
1 like
nikocraft's avatar

hi what package are you using that gives you imagecache.php I do not have such a config file in my project.

smuzammilali's avatar

@ohffs It doesn't work as I need to update the route as per the logged in user info.

I need a user Auth details to update the route of the image cache.

I'm using URL manipulation and I need to define source directory as per the logged in user.

http://image.intervention.io/use/url

akaamitgupta's avatar

The other options to override the values of config are in middlewares. Create a new middleware and add the following code in handle method:

public function handle($request, Closure $next)
{
    if (auth()->check()) {
        config(['imagecache.paths' => [
                public_path('image/'.Auth::user()->username),
                public_path('image')
            ]
        ]);
    }

    return $next($request);
}

And use this middleware in routes wherever you want to override the values.

OR

As an alternative, you may define a Closure based middleware directly in your controller's constructor. Before using this feature, make sure that your application is running Laravel 5.3.4 or above:

public function __construct()
{
    $this->middleware(function ($request, $next) {
        if (auth()->check()) {
            config(['imagecache.paths' => [
                    public_path('image/'.Auth::user()->username),
                    public_path('image')
                ]
            ]);
        }

        return $next($request);
    });
}
smuzammilali's avatar

Thank you @akaamitgupta

The solution update the config file and also Auth is accessible under AdminController construct.

But now the problem is (artisan route:list):

GET|HEAD | image/{template}/{filename} | imagecache | Intervention\Image\ImageCacheController@getResponse

Not passing any middleware not even web or guest.

Config successfully shows updated config when accessing from any AdminController page, but when comes to image loading it still shows default path which is /upload.

Please or to participate in this conversation.