Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the App Namespace in Laravel 5

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?

like image 813
Siavosh Avatar asked Sep 29 '22 10:09

Siavosh


1 Answers

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.

like image 161
Pantelis Peslis Avatar answered Oct 05 '22 08:10

Pantelis Peslis