Get the application namespace in Laravel 5

I am developing a package for Laravel 5, but if the user changes the default namespace for the application, the package on command php artisan app:name Testwill not work properly. the package should know that the application namespace is working correctly.

Based on my understanding, Laravel 5 does not provide the application namespace in any of its main classes

So, I decided either to get this namespace from the file composer.json

   "psr-4": {
        "AppNamespace\\": "app/"
    }

or ask the user to provide the application namespace as a configuration file.

Question: please let me know if my presumption about Laravel does not provide that it is correct, and if it is correct, tell me which way is the best way to get this namespaceor if you have a better offer?

+4
source share
1 answer

You can get the application namespace using the trait below, which is included in the Laravel kernel:

Illuminate\Console\AppNamespaceDetectorTrait

Laravel uses this attribute in AppNameCommandto get the current namespace before updating it.

Example:

class MyClass {

    use \Illuminate\Console\AppNamespaceDetectorTrait;

    public function getNamespace()
    {
        return $this->getAppNamespace();
    }

}

Read more about the symptoms here .

+3
source

All Articles