php 如何在yii2中获取根目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23155428/
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
How to get root directory in yii2
提问by Tousif Ali
yii2 Question
yii2 问题
My yii2 install in d:\wamp\www\yii2store
我的 yii2 安装在 d:\wamp\www\yii2store
I want to get above path to save images which will be uploaded by me or users.
我想获得以上路径以保存将由我或用户上传的图像。
I have pass all available arguments in Yii::getAlias('@webroot')
(below are the lists of argument which I have used).
我已经传递了所有可用的参数Yii::getAlias('@webroot')
(以下是我使用的参数列表)。
@yii
- framework directory.
@yii
- 框架目录。
@app
- base path of currently running application.
@app
- 当前运行的应用程序的基本路径。
@runtime
- runtime directory.
@runtime
- 运行时目录。
@vendor
- Composer vendor directory.
@vendor
- 作曲家供应商目录。
@webroot
- web root directory of currently running web application.
@webroot
- 当前运行的 web 应用程序的 web 根目录。
@web
- base URL of currently running web application.
@web
- 当前运行的 Web 应用程序的基本 URL。
And also once I will get above path to save images then how can I get path something like this localhost/yiistore2/upload
to be use in img tag src.
而且一旦我将获得以上路径以保存图像,那么我如何获得类似这样的路径localhost/yiistore2/upload
以在 img 标签 src 中使用。
One more thing how can I create my own alias with Yii::setAlias()
and where to create this so that I can load it on every controller.
还有一件事如何创建我自己的别名Yii::setAlias()
以及在哪里创建它,以便我可以将它加载到每个控制器上。
回答by Tousif Ali
Open file
D:\wamp\www\yiistore2\common\config\params-local.php
打开文件
D:\wamp\www\yiistore2\common\config\params-local.php
Paste below code before return
返回前粘贴下面的代码
Yii::setAlias('@anyname', realpath(dirname(__FILE__).'/../../'));
After inserting above code in params-local.php file your file should look like this.
在 params-local.php 文件中插入上述代码后,您的文件应如下所示。
Yii::setAlias('@anyname', realpath(dirname(__FILE__).'/../../'));
return [
];
Now to get path of your root (in my case its D:\wamp\www\yiistore2
) directory you can use below code in any php file.
现在要获取您的根目录(在我的情况下是它D:\wamp\www\yiistore2
)目录的路径,您可以在任何 php 文件中使用以下代码。
echo Yii::getAlias('@anyname');
回答by Latikov Dmitry
Use "getAlias" in Yii2
在 Yii2 中使用“getAlias”
\Yii::getAlias('@webroot')
回答by Dency G B
Try out this,
试试这个,
My installation is at D:\xampp\htdocs\advanced
我的安装在 D:\xampp\htdocs\advanced
\Yii::$app->basePath
will give like D:\xampp\htdocs\advanced\backend
.
\Yii::$app->basePath
会给喜欢D:\xampp\htdocs\advanced\backend
。
\Yii::$app->request->BaseUrl
will give like localhost\advanced\backend\web\
\Yii::$app->request->BaseUrl
会给喜欢 localhost\advanced\backend\web\
You may store the image using \Yii::$app->basePath
and show it using \Yii::$app->request->BaseUrl
您可以使用存储图像\Yii::$app->basePath
并使用\Yii::$app->request->BaseUrl
回答by Scops
Supposing you have a writable "uploads" folder in your application:
假设您的应用程序中有一个可写的“上传”文件夹:
You can define a param like this:
您可以像这样定义一个参数:
Yii::$app->params['uploadPath'] = realpath(Yii::$app->basePath) . '/uploads/';
Then you can simply use the parameter as:
然后您可以简单地将参数用作:
$path1 = Yii::$app->params['uploadPath'] . $filename;
Just depending on if you are using advanced or simple template the base path will be (following the linkprovided by phazei):
仅取决于您使用的是高级模板还是简单模板,基本路径将是(按照phazei 提供的链接):
Simple @app: Your application root directory
Advanced @app: Your application root directory (either frontend or backend or console depending on where you access it from)
简单的@app:您的应用程序根目录
高级@app:您的应用程序根目录(前端或后端或控制台,取决于您从何处访问它)
This way the application will be more portable than using realpath(dirname(__FILE__).'/../../'));
这样,应用程序将比使用更便携 realpath(dirname(__FILE__).'/../../'));
回答by Uchephilz
If you want to get the root directory of your yii2 project use, assuming that the name of your project is project_appyou'll need to use:
如果你想获取你的 yii2 项目使用的根目录,假设你的项目名称是project_app你需要使用:
echo Yii::getAlias('@app');
on windows you'd see "C:\dir\to\project_app"
在 Windows 上你会看到“C:\dir\to\project_app”
on linux you'll get "/var/www/dir/to/your/project_app"
在 linux 上你会得到“/var/www/dir/to/your/project_app”
I was formally using:
我正式使用:
echo Yii::getAlias('@webroot').'/..';
I hope this helps someone
我希望这可以帮助别人
回答by Akshay Naik
To get the base URL you can use this (would return "http:// localhost/yiistore2/upload")
要获取基本 URL,您可以使用它(将返回“http://localhost/yiistore2/upload”)
Yii::app()->baseUrl
The following Code would return just "localhost/yiistore2/upload" without http[s]://
以下代码将仅返回“localhost/yiistore2/upload”而没有 http[s]://
Yii::app()->getBaseUrl(true)
Or you could get the webroot path (would return "d:\wamp\www\yii2store")
或者你可以获得 webroot 路径(将返回“d:\wamp\www\yii2store”)
Yii::getPathOfAlias('webroot')
回答by Chandrashekhar Somwanshi
Open below file C:\xampp\htdocs\project\common\config\params-local.php
打开下面的文件 C:\xampp\htdocs\project\common\config\params-local.php
Before your code:
在您的代码之前:
<?php
return [
];
after your code:
在您的代码之后:
<?php
yii::setAlias('@path1', 'localhost/foodbam/backend/web');
return [
];