laravel 如何在laravel中获取应用程序名称?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30046823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:59:51  来源:igfitidea点击:

How to get the application name in laravel?

laravellaravel-5

提问by Ganesh Ghalame

I have set the application name using

我已经使用设置了应用程序名称

php artisan app:name xyz

How can I progmatically access the application name in laravel ?

如何以编程方式访问 laravel 中的应用程序名称?

采纳答案by lukasgeiter

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

还有Illuminate\Console\AppNamespaceDetectorTrait,您可以使用。在你的班级中添加这个:

class MyClass {

    use \Illuminate\Console\AppNamespaceDetectorTrait;

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

然后你可以在那个班级的任何地方使用它 $this->getAppNamespace()

回答by lewis4u

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

从 laravel 5.3 版开始,有一个函数可以调用应用程序名称

config('app.name');

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

您可以更改此文件中的默认名称“Laravel”

config/app.php

'name' => 'Laravel',

And also inside .env file

也在 .env 文件中

APP_NAME=your_app_name

回答by Adam Kozlowski

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

如果你想从 .env 文件中获取这个变量:

//.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:

如果您想获取另一个变量,例如 APP_ENV,只需编写:

config('app.local')

回答by Javier Solis

To get application namespace:

获取应用程序命名空间:

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

To get application name:

获取应用程序名称:

config('app.name');