Use Auth class in Laravel configuration file

Asked

Viewed 347 times

2

I’m using the barryvdh/Laravel-elfinder. This generates the configuration file /config/elfinder.php. Inside this file, I inform the path to upload Elfinder files.

I need each user to have their uploads folder. I thought of creating a variable $dir according to logged in user id but cannot use class Auth inside the archive /config/elfinder.php.

There is way to get the user logged in otherwise?

The file is like this:

<?php

$dir = 'arquivos';    
return array('dir' => [$dir] );

I want something like that:

<php

$dir = 'arquivos/'.Auth::id();
return array('dir' => [$dir] );

I’ve tried it like this:

<?php

use Auth;
$id=Auth::id();

$dir = 'arquivos/'.$id;    
return array('dir' => [$dir] );

And returns this error:

Warning: The use statement with non-compound name 'Auth' has no effect in /var/www/html/dna/config/elfinder.php on line 3

Fatal error: Class 'Auth' not found in /var/www/html/dna/config/elfinder.php on line 5
  • Can’t put it at the top of the page use Auth;

  • It doesn’t work, I’ve tried

  • It will really suck. This class probably hasn’t even gone into the alias processing that Laravel does internally. It’s probably the order the script is running internally.

  • See if it works session. By logging in, you put the ID in the session and take the elfinder.php file..

  • @Gumball’s not gonna work either. There is a much more serious problem than that in Laravel 5. Middleware and autoload affect uploading data from Auth as well.

  • When it comes to the files in the folder config it is true. It is necessary to have another way out.

Show 1 more comment

1 answer

3


This error usually happens because the configuration file is loaded before the internal processing that Laravel does for the class Auth (which is an alias).

In addition to this problem, depending on the application locations where you may want to try to access the value of Auth::user, the return may come null whether or not you are logged in. This is because Auth are accessible only after loading these middleware below:

    \Tmt\Http\Middleware\EncryptCookies::class,
    \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
    \Illuminate\Session\Middleware\StartSession::class,

They are responsible for the session, cookies and the like.

Solution

You can consider using the function config to be able to define according to the logged in user within a Middleware. Because using a middleware, you are working with data coming from the session, since it has already been loaded by the previous middleware.

For example

class Authenticate
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        // Restante do código

        config(['elfinder.dir' => ['arquivos/' . auth()->user()->id]])

        return $next($request);
    }
}

It is good to note that the idea of using the config is to work with immutable data.

Perhaps it would still be interesting to note if the library you are using does not exist a method for you to define which directory will work.

  • 1

    Exactly. You need to rewrite the variable at the time of login using a Middleware.Type: Route::group(['prefix' => 'auth', 'middleware' => ['auth', 'path-upload']], function(){ });. From there on Middleware path-upload rewrite.

  • @Gumball this, if she needs it she can set a middleware global, or else put it within the group web.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.