Unable to locate publishable resource when adding sanctum to existing project

Viewed 517

The Laravel Sanctum documentation states that to add Sanctum to existing Laravel project one has to:

  1. Require Laravel Sanctum composer require laravel/sanctum
  2. Publish Sanctum's resources to app dir php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"

I'm getting Unable to locate publishable resources. when running (2)

I already tried php artisan clear-compiled and composer dumpautoload in that order

I checked that vendor/laravel/sanctum/src/SanctumServiceProvider.php exists and the boot() method does publishes Sanctum's config and migration

I also checked that SanctumServiceProvider is discovered by autoload by making sure that the source path is listed in autoload_classmap.php

Again, this was an attempt to add Sanctum to an existing Laravel project

Did I miss something?

My setup:

  • Laravel 8.37
  • PHP 7.3.9
  • Composer 2.0.12
1 Answers

I have managed to solve my problem.

Short answer:

After composer require laravel/sanctum do this:

  1. Backup or rename cache/services.php
  2. Remove cache/services.php
  3. Run php artisan. Seems that running artisan in any way will regenerate cache/services.php

After that, continue with the official documentation (publish, migrate, and so on)

Longer explaination

Not entirely sure what is going on. But apparently all discovered service provider is cached in cache/services.php.

I don't know how it's being generated and how service providers get discovered. Googling doesn't yield me any documentation regarding that file (official or otherwise).

I figured I if I add Sanctum's SP to config/app.php (i.e. the documented way of adding service provider) it will get discovered and cached. Apparently that wasn't the case. Removing and regenrating cache/services.php is the only way I found working. After the file was regenerated, Sanctum's SP is listed.

One thing I observe was that this only happens on an existing project (existing as in already being worked on, packages added, config changes and so on). Doesn't happen if I add Sanctum to a fresh project.

Related