流明护照不在对象上下文中时使用$ this

时间:2018-02-24 18:09:13

标签: php laravel lumen

嗨,我是Lumen的新手,我想用oauth2构建一个安静的应用程序。 所以我安装了流明护照。 其中一个步骤是添加

Dusterio\LumenPassport\LumenPassport::routes($this->app);
在app.php中

稍后我运行php artisan list或任何其他cmd时会显示此错误:

In app.php line 97:

  Using $this when not in object context  

请有人告诉我如何解决这个问题。 这里是我追求的文档的链接:

https://packagist.org/packages/dusterio/lumen-passport 这是我的app文件:

<?php

require_once __DIR__.'/../vendor/autoload.php';

try {
    (new Dotenv\Dotenv(__DIR__.'/../'))->load();
} catch (Dotenv\Exception\InvalidPathException $e) {
    //
}

/*
|--------------------------------------------------------------------------
| Create The Application
|--------------------------------------------------------------------------
|
| Here we will load the environment and create the application instance
| that serves as the central piece of this framework. We'll use this
| application as an "IoC" container and router for this framework.
|
*/

$app = new Laravel\Lumen\Application(
    realpath(__DIR__.'/../')
);
// Enable Facades
$app->withFacades();
// Enable Eloquent
$app->withEloquent();



/*
|--------------------------------------------------------------------------
| Register Container Bindings
|--------------------------------------------------------------------------
|
| Now we will register a few bindings in the service container. We will
| register the exception handler and the console kernel. You may add
| your own bindings here if you like or you can make another file.
|
*/

$app->singleton(
    Illuminate\Contracts\Debug\ExceptionHandler::class,
    App\Exceptions\Handler::class
);

$app->singleton(
    Illuminate\Contracts\Console\Kernel::class,
    App\Console\Kernel::class
);

/** Add auth for passport**/
$app->configure('auth');

/*
|--------------------------------------------------------------------------
| Register Middleware
|--------------------------------------------------------------------------
|
| Next, we will register the middleware with the application. These can
| be global middleware that run before and after each request into a
| route or middleware that'll be assigned to some specific routes.
|
*/

// $app->middleware([
//    App\Http\Middleware\ExampleMiddleware::class
// ]);

// Enable auth middleware (shipped with Lumen)
 $app->routeMiddleware([
    'auth' => App\Http\Middleware\Authenticate::class,
]);

/*
|--------------------------------------------------------------------------
| Register Service Providers
|--------------------------------------------------------------------------
|
| Here we will register all of the application's service providers which
| are used to bind services into the container. Service providers are
| totally optional, so you are not required to uncomment this line.
|
*/
$app->register(App\Providers\AppServiceProvider::class);
 $app->register(App\Providers\AuthServiceProvider::class);
 $app->register(App\Providers\EventServiceProvider::class);
//added for passport
$app->configure('auth');

// added for flipbox to generate controller and model in lumen
$app->register(Flipbox\LumenGenerator\LumenGeneratorServiceProvider::class);
//for lumen passport
$app->register(Laravel\Passport\PassportServiceProvider::class);
$app->register(Dusterio\LumenPassport\PassportServiceProvider::class);
Dusterio\LumenPassport\LumenPassport::routes($this->app);

/*
|--------------------------------------------------------------------------
| Load The Application Routes
|--------------------------------------------------------------------------
|
| Next we will include the routes file so that they can all be added to
| the application. This will provide all of the URLs the application
| can respond to, as well as the controllers that may handle them.
|
*/

$app->router->group([
    'namespace' => 'App\Http\Controllers',
], function ($router) {
    require __DIR__.'/../routes/web.php';
});

return $app;

1 个答案:

答案 0 :(得分:2)

如错误所示:$this只能在实例方法中访问。 要获取应用程序对象,可以调用app()函数。

在你的情况下:

Dusterio\LumenPassport\LumenPassport::routes(app());