search
HomeBackend DevelopmentPHP TutorialHow to configure automatic loading of new directories in Yii2

目录结构类似这样的:
/

<code>frontend/
common/
backend/
helpers/
    Foo.php
vendor/
</code>

我在项目根目录新建了个目录helpers,里面建立了个类文件Foo.php,namespace helpers;
但是在use helpers\Foo;的时候,提示helpers\Foo没有找到。
请问这是什么原因呢?是不是Yii和autoload没有把一级目录加进自动加载里面?
要怎么解决这个问题?

目前我是把helpers放到common里面

@光灵小鼠 @八宝粥 helpers放一级目录这种设计确实不好,多谢指点。

回复内容:

目录结构类似这样的:
/

<code>frontend/
common/
backend/
helpers/
    Foo.php
vendor/
</code>

我在项目根目录新建了个目录helpers,里面建立了个类文件Foo.php,namespace helpers;
但是在use helpers\Foo;的时候,提示helpers\Foo没有找到。
请问这是什么原因呢?是不是Yii和autoload没有把一级目录加进自动加载里面?
要怎么解决这个问题?

目前我是把helpers放到common里面

@光灵小鼠 @八宝粥 helpers放一级目录这种设计确实不好,多谢指点。

两种途径:

  1. 利用yii2本身的方法,即上面有人提到的setAlias

  2. 利用composer, 然后不要忘记composer dump-autoload

<code>"psr-4": {
    "helpers\\": "helpers/"
}</code>

我也没看到helpersFoo在哪
格式如下

<code>namespace helpers </code>
<code>use userCenter\regist;</code>

在common/config/bootstrap.php文件内配置:

<code><?php Yii::setAlias('common', dirname(__DIR__));
Yii::setAlias('frontend', dirname(dirname(__DIR__)) . '/frontend');
Yii::setAlias('backend', dirname(dirname(__DIR__)) . '/backend');
Yii::setAlias('console', dirname(dirname(__DIR__)) . '/console');
Yii::setAlias('helpers', dirname(dirname(__DIR__)) . '/helpers');</code></code>

这种是要配置的。
根目录下小写开头的目录都需要特别的设置,也就是说helpers, frontend, common这些和命名空间不是直接对应的,但是它们之下的文件是对应的。自己看一下文档就了解了。

setAlias()

如果没记错,Yii2 也是利用 composer 实现 psr-4 映射,可参阅 composer 文档。

你用的是Yii2 advanced模版
这个模版有三个应用
backend 代表后台应用
console 代表控制器应用
frontend 代表前台应用
common 存放三个应用公用的类

= = 你在根目录增加helper?表示你要增加一个helper应用?

在common下面增加一个helper文件夹还差不多

@__FresHmaN 讲的不错。

但是我不建议你放在根目录,建议你放在 common 里面

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php怎么将url的参数转化成数组php怎么将url的参数转化成数组Apr 21, 2022 pm 08:50 PM

转化方法:1、使用“mb_substr($url,stripos($url,"?")+1)”获取url的参数部分;2、使用“parse_str("参数部分",$arr)”将参数解析到变量中,并传入指定数组中,变量名转为键名,变量值转为键值。

yii2 怎么去掉jqueryyii2 怎么去掉jqueryFeb 17, 2023 am 09:55 AM

yii2去掉jquery的方法:1、编辑AppAsset.php文件,注释掉变量$depends里的“yii\web\YiiAsset”值;2、编辑main.php文件,在字段“components”下面添加配置为“'yii\web\JqueryAsset' => ['js' => [],'sourcePath' => null,],”即可去掉jquery脚本。

php怎么去除首位数字php怎么去除首位数字Apr 20, 2022 pm 03:23 PM

去除方法:1、使用substr_replace()函数将首位数字替换为空字符串即可,语法“substr_replace($num,"",0,1)”;2、用substr截取从第二位数字开始的全部字符即可,语法“substr($num,1)”。

php有操作时间的方法吗php有操作时间的方法吗Apr 20, 2022 pm 04:24 PM

php有操作时间的方法。php中提供了丰富的日期时间处理方法:1、date(),格式化本地日期和时间;2、mktime(),返回日期的时间戳;3、idate(),格式化本地时间为整数;4、strtotime(),将时间字符串转为时间戳等等。

php怎么去掉右边几个字符php怎么去掉右边几个字符Apr 21, 2022 pm 07:45 PM

去掉方法:1、用substr_replace()删除右边n位置开始的全部字符,语法“substr_replace($str,"",-n)”,参数“n”为需要去除的字符个数;2、用substr(),语法“substr($str,0,-n)”。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version