Zend Framework:自动加载类库
我在这里定义了一个类库 .../projectname/library/Me/Myclass.php 定义如下:
<?php
class Me_Myclass{
}
?>
我有以下引导程序:
<?php
/**
* Application bootstrap
*
* @uses Zend_Application_Bootstrap_Bootstrap
*/
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
/**
* Bootstrap autoloader for application resources
*
* @return Zend_Application_Module_Autoloader
*/
protected function _initAutoload()
{
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'Default',
'basePath' => dirname(__FILE__),
));
$autoloader->registerNamespace('Me_');
return $autoloader;
}
/**
* Bootstrap the view doctype
*
* @return void
*/
protected function _initDoctype()
{
$this->bootstrap('view');
$view = $this->getResource('view');
$view->doctype('XHTML1_STRICT');
}
/**
* Bootstrap registry and store configuration information
*
* @return void
*/
protected function _initRegistry()
{
$config = new Zend_Config_Ini(APPLICATION_PATH .
'/configs/application.ini', APPLICATION_ENV,
array('allowModifications'=>true));
Zend_Registry::set('configuration', $config);
}
}
在我的控制器中,我尝试像这样实例化该类:
<?php
class SomeController extends Zend_Controller_Action
{
public function indexAction()
{
$classMaker=new Me_Myclass();
}
}
?>
当我导航时直接访问 http:/something.com/projectname/some?id=1 我收到以下错误:
Fatal error: Class 'Me_Myclass' not found in /home/myuser/work/projectname/application/controllers/SomeController.php on line x
有什么想法吗?
可能相关的杂项:
当我使用在应用程序/库下的其他文件夹中定义的类扩展模型时,自动加载器似乎可以工作。
有人建议更改“默认”,我尝试这样做,但它似乎没有解决问题,并且还因破坏使用此命名空间的模型功能而产生额外的负面影响。
I've got a class library in defined here .../projectname/library/Me/Myclass.php defined as follows:
<?php
class Me_Myclass{
}
?>
I've got the following bootstrap:
<?php
/**
* Application bootstrap
*
* @uses Zend_Application_Bootstrap_Bootstrap
*/
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
/**
* Bootstrap autoloader for application resources
*
* @return Zend_Application_Module_Autoloader
*/
protected function _initAutoload()
{
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'Default',
'basePath' => dirname(__FILE__),
));
$autoloader->registerNamespace('Me_');
return $autoloader;
}
/**
* Bootstrap the view doctype
*
* @return void
*/
protected function _initDoctype()
{
$this->bootstrap('view');
$view = $this->getResource('view');
$view->doctype('XHTML1_STRICT');
}
/**
* Bootstrap registry and store configuration information
*
* @return void
*/
protected function _initRegistry()
{
$config = new Zend_Config_Ini(APPLICATION_PATH .
'/configs/application.ini', APPLICATION_ENV,
array('allowModifications'=>true));
Zend_Registry::set('configuration', $config);
}
}
In my controller I try to instantiate the class like this:
<?php
class SomeController extends Zend_Controller_Action
{
public function indexAction()
{
$classMaker=new Me_Myclass();
}
}
?>
When I navigate directly to http:/something.com/projectname/some?id=1 I get the following error:
Fatal error: Class 'Me_Myclass' not found in /home/myuser/work/projectname/application/controllers/SomeController.php on line x
Any ideas?
Potentially Pertinent Miscellany:
The autoloader seems to work when I'm extending models with classes I've defined in other folders under application/library.
Someone suggested changing the 'Default', which I attempted but it didn't appear to fix the problem and had the added negative impact of breaking function of models using this namespace.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的类需要命名为 Me_Myclass:
将您的库文件夹向上移动一个级别,以便您具有文件夹结构:
然后在 Bootstrap 中将以下内容添加到 _initAutoload():
You class needs to be name Me_Myclass:
Move your library folder up a level so that you have the folder structure:
And then in your Bootstrap add the following to the _initAutoload():
您可以在 config.ini 文件中定义自动加载目录,如下所示:
works 100%
you can define the autoload dir in the config.ini file like this:
works 100%
我认为 @smack0007 意味着将 _initAutoload 方法的内容替换为 Zend_Loader_Autoloader::getInstance()->registerNamespace('Me_');所以它看起来像这样:
I think @smack0007 means replace the contents of your _initAutoload method with Zend_Loader_Autoloader::getInstance()->registerNamespace('Me_'); so it looks like this:
不确定这是否是你的问题,但我刚刚花了最后一天半的时间试图找出我自己的类似问题(第一次从 Windows 将其加载到 Linux 上)。事实证明我对图书馆的文件夹名称大小写一无所知。
与(在 *nix 上)不同
类名通常是这样
希望这可以帮助那些同样心不在焉的人。
Not sure if this is your problem, but I just spent the last day and half trying to figure out my own similar problem (first time loading it up on Linux from Windows). Turns out I was blind to my library's folder name case.
is not the same as (on *nix)
Class name is typically this
Hope this helps someone who is similarly absentminded.