2

我有一个在我拥有的 Laravel 5.2 应用程序之外运行的 php 脚本,我想从这个外部 php 脚本访问 Auth:: 类。因此,如果用户登录 laravel 应用程序,他也可以执行这个 php 文件。

你能告诉我这是怎么做到的吗?

现在我有这个:

require __DIR__.'../../../../bootstrap/autoload.php';
$app = require __DIR__.'../../../../bootstrap/app.php';

$app->boot();
$app->register(\Illuminate\Auth\AuthServiceProvider::class);
dd($app['auth']);

这给了我一些 auth 实例,但不知道如何处理它。我需要有类似的东西:

if ($app['auth']::guest()) {
    dd("Go away you guest.");
}

谢谢

4

2 回答 2

0

你不需要在 Laravel 核心文件中做任何事情......只需添加一些路由并将Auth中间件添加到您想要的路由中,因此未经身份验证的用户将无法访问提到的 URL

Route::group(['prefix'=>'admin', 'middleware'=>'auth'], function(){
 Route::get('dashboard',['as'=>'user-dashboard','uses'=>'Admin\UserController@dashboard']);
});

现在使用这个middleware'=>'auth'只有登录用户可以访问Dashboard page

于 2016-03-31T09:23:27.757 回答
0

对于 5.7,在外部文件上添加以下代码:

require $_SERVER['DOCUMENT_ROOT'].'/appname/vendor/autoload.php';
$app = require_once $_SERVER['DOCUMENT_ROOT'].'/appname/bootstrap/app.php';
$app->make('Illuminate\Contracts\Http\Kernel')
    ->handle(Illuminate\Http\Request::capture());
于 2019-01-02T15:29:43.153 回答