本文介绍了每个控制器中App :: uses('AppController','Controller')的用途是什么-cakephp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

烘焙一些控制器时,我注意到在每个控制器的顶部都有一行

When Baking some controllers I noticed that on the top of each controller there is a line

App::uses('AppController', 'Controller');

删除它并没有什么不同,AFA我注意到,没有那一行,一切都很好。自动完成功能在任何情况下均有效,因此不能用于此目的。那么,那是什么意思呢?我的蛋糕版本是2.5.3。

Deleting it does not make any difference, AFA I noticed, everything works fine without that line. Autocomplete works in any case, so it can not be for that purpose. So, what is the meaning of that ? My cake version is 2.5.3.

谢谢

推荐答案

App :: uses是一种静态方法,用于注册要加载的文件(与类相对应)的位置,以便PHP自动加载器能够即时加载。

App::uses is a static method to register the location of a file (that corresponds with a class) to load for the PHP autoloader to be able to load it on-the-fly.

App :: uses('AppController','Controller'); 行声明如果AppController类被引用(在这种情况下为扩展),并且尚未定义AppController类,因此应在 app / Controller 目录中查找文件,并构造实际的像这样的文件名: AppController.php 其中一部分是cakePHP约定,一部分是PHP5。

That App::uses('AppController', 'Controller'); line declares that if the AppController class is referenced (or extended, in this case) and the AppController class isn't yet defined, that it should look for the file within the app/Controller directory and construct the actual filename like so: AppController.php Part of that is cakePHP convention, part PHP5.

删除此行是因为该文件可能是应用程序中其他地方所需的文件(在引导过程中另一个控制器,因此php在内存中已经具有AppController类。

The reason nothing breaks when you remove this line is because that file is probably required somewhere else in the application (another controller, during bootstrap-- so php already has the AppController class in memory.

The之所以将其包含在第一行中,是因为您烘焙的控制器扩展了AppController类,它对要加载的AppController类有严格的依赖关系,并且可能在蛋糕外壳或单元测试环境中,应用程序控制器不会事先自动加载,因此包含必须发生。 pp :: uses就像告诉蛋糕(和PHP),嘿,如果您正在寻找 AppController 类,但您还没有它,应该看看在app / Controller目录中,最后只添加 .php !如果文件已加载,则App :: uses无效。

The reason it's included on the first line is because the controller you baked extends the AppController class. It has a hard dependency on the AppController class being loaded and perhaps in the cake shell or unit testing environments the app controller isn't automatically loaded before hand so the inclusion must occur. App::uses is like telling cake (and PHP) that 'hey, if you're looking for the AppController class and you don't already have it, you should look in app/Controller directory and just add .php on the end!' If the file has already been loaded, App::uses would have no effect.

这篇关于每个控制器中App :: uses('AppController','Controller')的用途是什么-cakephp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 05:08