yii2如何获取根目录

2022-01-16 00:00:00 frameworks php yii2

yii2 问题

我的 yii2 安装在 d:wampwwwyii2store

My yii2 install in d:wampwwwyii2store

我想获取上面的路径来保存我或用户将上传的图像.

I want to get above path to save images which will be uploaded by me or users.

我已经在 Yii::getAlias('@webroot') 中传递了所有可用的参数(下面是我使用的参数列表).

I have pass all available arguments in Yii::getAlias('@webroot') (below are the lists of argument which I have used).

@yii - 框架目录.

@app - 当前运行的应用程序的基本路径.

@app - base path of currently running application.

@runtime - 运行时目录.

@vendor - Composer 供应商目录.

@vendor - Composer vendor directory.

@webroot - 当前运行的 web 应用程序的 web 根目录.

@webroot - web root directory of currently running web application.

@web - 当前运行的 Web 应用程序的基本 URL.

@web - base URL of currently running web application.

而且,一旦我将获得保存图像的路径,那么我怎样才能获得类似 localhost/yiistore2/upload 的路径以在 img 标签 src 中使用.

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.

我如何使用 Yii::setAlias() 创建我自己的别名以及在哪里创建它以便我可以在每个控制器上加载它.

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.

推荐答案

打开文件D:wampwwwyiistore2commonconfigparams-local.php

在返回之前粘贴下面的代码

Paste below code before return

Yii::setAlias('@anyname', realpath(dirname(__FILE__).'/../../'));

在 params-local.php 文件中插入上述代码后,您的文件应如下所示.

After inserting above code in params-local.php file your file should look like this.

Yii::setAlias('@anyname', realpath(dirname(__FILE__).'/../../'));

return [
];

现在要获取根目录的路径(在我的例子中是 D:wampwwwyiistore2)目录,您可以在任何 php 文件中使用以下代码.

Now to get path of your root (in my case its D:wampwwwyiistore2) directory you can use below code in any php file.

echo Yii::getAlias('@anyname');

相关文章