如何在yii2中获取根目录 [英] How to get root directory in yii2

查看:1550
本文介绍了如何在yii2中获取根目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

yii2问题



我的yii2安装在 d:\ wamp\www\yii2store code>



我希望获得以上路径以保存将由我或用户上传的图片。



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



@yii - framework目录。

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

@runtime - runtime目录。

$ @vendor - 作曲家供应商目录。



<当前运行的Web应用程序的web根目录。



@web

@webroot - 当前运行的Web应用程序的基本URL。



同样,一旦我将获得保存图像的路径,那么我该如何获得路径这个 localhost / yiistore2 / upload 用于img标签s rc。



还有一件事我该如何使用 Yii :: setAlias()创建自己的别名,以及在哪里创建它,以便我可以将其加载到每个控制器上。

解决方案

打开文件
D :\wamp\www\yiistore2\common\config\params-local.php



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

  Yii :: setAlias('@ anyname',realpath(dirname(__ FILE __)。'/ .. / .. /' )); 

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

  Yii :: setAlias('@ anyname',realpath(dirname(__ FILE __)。'/ .. / .. /')); 

return [
];

现在得到你的根路径(在我的例子中它的 D:\\ \\ wamp \www\yiistore2 )目录下,你可以在任何php文件中使用下面的代码。 > echo Yii :: getAlias('@ anyname');


yii2 Question

My yii2 install in 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 - framework directory.

@app - base path of currently running application.

@runtime - runtime directory.

@vendor - Composer vendor directory.

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

@web - base URL of currently running web application.

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.

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.

解决方案

Open file 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.

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.

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

这篇关于如何在yii2中获取根目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆