I am developing a package for Laravel 5, but if a user change the default namespace of the application the package by command php artisan app:name Test
won't work properly. the package needs to know the App's namespace to work properly.
Based on my understanding the Laravel 5 doesn't provide the namespace of the app in any of its core classes
So I decided either to get this namespace from the composer.json
file
"psr-4": {
"AppNamespace\\": "app/"
}
or ask the user to provide the namespace of the application in a kind of config file.
Question:
please let me know if my presumption about Laravel doesn't provide this is correct and if it is correct please let me know which way is the best way to get this namespace
or if you have any better suggestion?
You can get the app's namespace by using the below trait that is included to the core of Laravel:
Illuminate\Console\AppNamespaceDetectorTrait
Laravel uses this trait in the AppNameCommand
to get the current namespace before it updated.
An example:
class MyClass {
use \Illuminate\Console\AppNamespaceDetectorTrait;
public function getNamespace()
{
return $this->getAppNamespace();
}
}
You can read more about the traits here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With