In my composer.json, I have multiple namespaces setup to be autoloaded. When Laravel attempts to auto-register commands, it only registers commands that are under the first namespace in the list.
See this demonstration:
https://github.com/fitztrev/laravel-auto-register-commands/compare/demo
There's a command at MyCustomApp/Console/Commands/ThisCommandWillNotBeFound.php that is not being registered even though I'm loading its directory.
Did you run composer dump-autoload after composer.json changes?
Did you run composer dump-autoload after composer.json changes?
Yes. You can see in the linked PR that I attempted that shows why it happens. getNamespace() returns only the first namespace from the compose.json file. So when load() tries to prepend that to the file path it finds, it is not resolvable.
Yes it's meant to auto-load only commands in Console\Commands, for commands in other locations you'll have to register manually.
I know this is old, but why is this restriction in place? What is the benefit to forcing commands to exist in the App namespace? I have an application with multiple PSR-4 root paths, but I can only use $this->load() for paths in the one determined to be in the app_path().
It's common across the framework, to offer convenient "magic" functionality if you use the conventions. For instance, here's a recent tweet from Taylor, concerning a similar way of registering policies:
https://twitter.com/taylorotwell/status/1097582054349459456?s=21
These are conveniences for using the conventions. Laravel apps would probably take a small efficiency hit if all of those auto-registering utilities were changed to cover multiple/configurable locations.
The load() method already requires a specific directory as a parameter. The issue here is that even though I'm explicitly choosing a folder outside of app_path(), the load() method itself does not allow it.
It's an imposed restriction, not a common convenience or any kind of efficiency hit.
Most helpful comment
The
load()method already requires a specific directory as a parameter. The issue here is that even though I'm explicitly choosing a folder outside ofapp_path(), theload()method itself does not allow it.It's an imposed restriction, not a common convenience or any kind of efficiency hit.