You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Lumen Passport报错“Using $this when not in object context”求助

Fixing "Using $this when not in object context" with lumen-passport

Hey there! I’ve seen this exact error pop up a lot when setting up dusterio/lumen-passport in Lumen, so let’s get you sorted out quickly.

The root cause here is almost always a misplaced $this in your bootstrap/app.php file. Unlike Laravel’s service providers where $this refers to the provider instance, the app.php file runs in a global context—so $this doesn’t exist there. You need to use the $app variable instead.

Step 1: Audit your bootstrap/app.php for $this references

Go through your file and look for lines where you might have used $this instead of $app. Here are the most common mistakes:

Wrong:

$this->configure('passport');

Correct:

$app->configure('passport');

Wrong (in service provider registration closure):

$app->register(Dusterio\LumenPassport\PassportServiceProvider::class, function () {
    $this->loadMigrationsFrom(__DIR__.'/../vendor/dusterio/lumen-passport/database/migrations');
});

Correct (use the $app parameter passed to the closure):

$app->register(Dusterio\LumenPassport\PassportServiceProvider::class, function ($app) {
    $app->loadMigrationsFrom(__DIR__.'/../vendor/dusterio/lumen-passport/database/migrations');
});

Step 2: Verify your core Lumen setup

Make sure you’ve enabled the necessary features in app.php before registering the passport provider:

// Enable Facades (required for Passport)
$app->withFacades();

// Enable Eloquent (since Passport uses database models)
$app->withEloquent();

// Load passport configuration
$app->configure('passport');

Step 3: Double-check your AuthServiceProvider (if used)

If you’ve added Passport routes or policies in app/Providers/AuthServiceProvider.php, ensure you’re using $this only inside class methods (like boot()), which is valid:

public function boot()
{
    $this->registerPolicies();
    Passport::routes();
    // Any other Passport configuration here is safe with $this
}

Once you fix all those misplaced $this references, run php artisan list again—it should work without the error. If you’re still stuck, feel free to share snippets of your app.php and I can help dig deeper!

内容的提问来源于stack exchange,提问作者DINA TAKLIT

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:53:35