How to get the application name in laravel?

2020-08-09 08:41发布

I have set the application name using

php artisan app:name xyz

How can I progmatically access the application name in laravel ?

4条回答
我欲成王,谁敢阻挡
2楼-- · 2020-08-09 09:18

If you want to get this variable form .env file:

//.env

APP_NAME=My appication name from .env file

APP_ENV=local

You get that way:

config('app.name')

If you want to get another variable, for example APP_ENV, just write:

config('app.local')
查看更多
太酷不给撩
3楼-- · 2020-08-09 09:31

To get application namespace:

use Illuminate\Container\Container;
Container::getInstance()->getNamespace();

To get application name:

config('app.name');
查看更多
Lonely孤独者°
4楼-- · 2020-08-09 09:38

There's Illuminate\Console\AppNamespaceDetectorTrait which you can use. Add this in your class:

class MyClass {

    use \Illuminate\Console\AppNamespaceDetectorTrait;

And then you can use it anywhere in that class with $this->getAppNamespace()

查看更多
够拽才男人
5楼-- · 2020-08-09 09:40

From laravel version 5.3^ there is a function to call the app name

config('app.name');

You can change the default name 'Laravel' inside this file

config/app.php

'name' => 'Laravel',

And also inside .env file

APP_NAME=your_app_name
查看更多
登录 后发表回答