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

lonerunner's avatar

How to use laravel Auth::user() outside laravel and pass data in custom php?

I am trying to implement simogeo filemanager into laravel and using it in combination with tinymce i will use it without tinymce also but that's not the problem.

I have already configured setup and installed all, and it works fine so far, but last one step is to make sure that only authorized users can call filemanager and upload files, and also there is an option to filemanager to have user specific folders. For that i will have to pass Auth::user() from laravel and check if user is loged in, and also if i use Auth::user()->username i would be able to have user specific folders.

Is there a way to pass Auth::user() for outside script from laravel?

0 likes
7 replies
bestmomo's avatar

it's what I did in filemanager/connectors/php/user.config.php :

function auth() { 

  require getcwd() . '/../../../../bootstrap/autoload.php';
  $app = require_once getcwd() . '/../../../../bootstrap/app.php';

  $kernel = $app->make('Illuminate\Contracts\Http\Kernel');

  $response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
  );

  $id = $app['encrypter']->decrypt($_COOKIE[$app['config']['session.cookie']]);
  $app['session']->driver()->setId($id);
  $app['session']->driver()->start();

  if(!$app['auth']->check()) return false;

  $role = $app['auth']->user()->role->slug;

  return($role == 'admin' || $role == 'redac');

}
2 likes
lonerunner's avatar

This doesn't help, i don't have an app.php in bootstrap folder, and i also don't have Illuminate\Contracts\Http\Kernel anywhere in laravel. it's probably difference between versions. i use Laravel Framework version 4.2.16

bestmomo's avatar

Sorry, yes it's for L5, needs to adapt for L4.

Tabun's avatar

It is actually simpler in L4:

    require getcwd() . '\..\..\..\..\bootstrap\autoload.php';
    $app = require_once getcwd() . '\..\..\..\..\bootstrap\start.php';

    $id = $app['encrypter']->decrypt($_COOKIE[$app['config']['session.cookie']]);

    $app->boot();

    $app['session']->driver()->setId($id);
    $app['session']->driver()->start();

    if(!$app['auth']->check()) return false;

    $role = $app['auth']->user()->role->slug;
    return($role == 'admin' || $role == 'redac');
mightybytes's avatar

This doesn't seem to create a new session if one does not exist. The laravel_session cookie is missing, unless you first load laravel through a standard route. I'm using L5.2.

I'm finding that run externally, the headers are never sent. (Probably because it has no view.) Any idea how I might get those headers push out with the response?

Here is the stack overflow: http://stackoverflow.com/questions/35709705/using-laravel-5-sessions-externally

decho's avatar

Hi,

In my case who as check if user logged then show outside file manager for tinymce, this below it work:

in app/Http/Karnell.php move all classes from $middlewareGroups to $middleware.

This is for Lravel 5.2

jago86's avatar

@bestmomo in Laravel 5.3 I have this error with your solution: "Call to undefined method Symfony\Component\HttpKernel\Event\GetResponseEvent::setDispatcher()".

However if I do dd($app['auth']->user()) I can see the Model for the User that is authenticated. Only when I try to follow with some lines of my script the error is shown.

Any idea?

Please or to participate in this conversation.