Skip to content

Instantly share code, notes, and snippets.

@chrisliuqq
Last active June 4, 2019 08:14
Show Gist options
  • Save chrisliuqq/be57ac4f7075ac14483c to your computer and use it in GitHub Desktop.
Save chrisliuqq/be57ac4f7075ac14483c to your computer and use it in GitHub Desktop.
How to use laravel Auth::user() outside laravel and pass data in custom php?
The solution above for 5.2 should still work. In 5.5+ you just need to change bootstrap/autoload.php to vendor/autoload.php.
<?php
require '/path/to/laravel/vendor/autoload.php';
$app = require_once '/path/to/laravel/bootstrap/app.php';
$app->make('Illuminate\Contracts\Http\Kernel')
->handle(Illuminate\Http\Request::capture());
// An instance of the Laravel app should be now at your fingertip ;-)
...
$isAuthorized = Auth::check();
?>
old
<?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');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment