laravel-7include-guardspackage-developmentlaravel-sanctum

How to use Laravel/sanctum in package development


I try to use the Laravel/Sanctum in a custom Laravel package. As in a default Laravel application, I added it to the composer file, added the migration and set it up in the routes file.

The next error message appears:

"Auth guard [sanctum] is not defined."

I hope it is even possible to use sanctum in another package?

Composer.json file:

"require": {
    "php": "7.4.*",
    "laravel/sanctum": "^2.2"
  },

routes file:

Route::group(['middleware' => 'auth:sanctum'], function () {
Route::post('/approve', ['uses' => 'MemberRequestController@response', 'response' => 'approve'])->name('approve_member_request');
}

ServiceProvider

/**
     *
     */
    private function registerRoutes()
    {
        Route::group($this->routeConfig(), function () {
            $this->loadRoutesFrom(__DIR__ . '/../../routes/api.php');
        });
    }

    /**
     * @return string[]
     */
    private function routeConfig(): array
    {
        return [
            'prefix' => ums::path(),
            'namespace' => 'martijn\UMS\Http\Controllers',
            'middleware' => ['api', 'auth:sanctum']
        ];
    }

Solution

  • You should publish the Laravel\Sanctum\SanctumServiceProvider.

    It will register the missing guard.