The Laravel Sanctum documentation states that to add Sanctum to existing Laravel project one has to:
- Require Laravel Sanctum
composer require laravel/sanctum
- Publish Sanctum’s resources to app dir
php artisan vendor:publish --provider="LaravelSanctumSanctumServiceProvider"
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
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
I have managed to solve my problem.
Short answer:
After composer require laravel/sanctum
do this:
- Backup or rename
cache/services.php
- Remove
cache/services.php
- Run
php artisan
. Seems that running artisan in any way will regeneratecache/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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0