CakePHP - 包括来自应用程序目录外的目录的类 [英] CakePHP - Include class from a directory outside the app directory

查看:202
本文介绍了CakePHP - 包括来自应用程序目录外的目录的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的应用程式位于:

  / var / www / websites / my_website / app / 

类位于:

  / var / www / websites / libs / CakePHP / MyClass.php 



在我的bootstrap中,我很难弄清楚如何添加路径从该目录加载类:

  App :: build(array('Lib'=& www / websites / lib /'))); 

App :: uses('MyClass','CakePHP');

$ myClass = new MyClass();


解决方案

载入不应在引导程序中执行,



AppController 的 beforeFilter 此外,还有一个非蛋糕库的保留位置,即 app / Vendor 目录。您可以将所有类放在那里,然后轻松加载团队:

  App :: uses('MyClass' '); 

如果真的需要在替代路径中,则需要指定并调用完整路径。并确保使用相同的名称。现在,您指定 Lib ,但调用 CakePHP ,就像这是一个构建本身(它不是)。这将不工作。它应该看起来像这样:

  App :: build(array('Lib'=& www / websites / lib'))); 
App :: uses('MyClass','Lib / CakePHP'); //定义这里的子目录

同时检查关于加载供应商文件的文档,它有很多例子。


I am trying to include a miscellaneous library class from outside my app (it gets used by different apps).

My app is located at:

/var/www/websites/my_website/app/

And the class is located at:

/var/www/websites/libs/CakePHP/MyClass.php

In my bootstrap I'm struggling to figure out how to add the path for loading the classes from that directory:

App::build(array('Lib' => array('/var/www/websites/lib/')));

App::uses('MyClass', 'CakePHP');

$myClass = new MyClass();

解决方案

Loading shouldn't be done in your bootstrap, but in your AppController's beforeFilter method instead.

Also, there is a reserved place for non-Cake libraries, being the app/Vendor directory. You can place all your classes in there and then load team easily with:

App::uses('MyClass', 'Vendor');

If it really needs to be in an alternative path, you need to specify and call the full path instead. And make sure to use the same names. Right now, you're specifying Lib, yet calling CakePHP as if that was a build by itself (which it's not). This won't work. It should look like this instead:

App::build(array('Lib' => array('/var/www/websites/lib')));
App::uses('MyClass', 'Lib/CakePHP'); // Define the subdirectory here

Also check the documentation on loading vendor files, it has quite some examples.

这篇关于CakePHP - 包括来自应用程序目录外的目录的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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