模型中的laravel 4特征自动加载 [英] laravel 4 trait autoloading in models
问题描述
好的,我现在已经挣扎了2个多小时...
OK, I am struggling for over 2 hours now...
这一定是哦,上帝,那是显而易见的"时期之一,在那您太累了,看不到解决方案...
It must be one of the "oh god, it was that obvious" times where you are so tired and you cannot see the solution...
我想扩展口才模型以包含特征.
I want to extend the eloquentmodel to include a trait.
所以我的课看起来像
<?php
class Note extends Eloquent{
use \Admin\GreatTrait;
...
}
,然后创建了app/Traits/Admin
目录结构,并在其中创建了名为GreatTrait.php
的文件,其内容如下:
and I created the app/Traits/Admin
directory structure and there I created the file named GreatTrait.php
with the following contents:
<?php namespace Admin;
trait GreatTrait{
...
}
当然,我编辑了start/global.php
,将目录包含在ClassLoader中,就像这样
and of cource I edited the start/global.php
to include the dir to the ClassLoader like so
ClassLoader::addDirectories(array(
app_path().'/commands',
... more ...
app_path().'/traits',
app_path().'/traits/Admin',
));
然后出现以下错误...
and I get the following error...
Symfony \ Component \ Debug \ Exception \ FatalErrorException
Trait 'Admin\GreatTrait' not found
有什么建议吗?
推荐答案
在您的代码之后,我进行了以下更改:
Following your code, i made it work with next changes:
-
在composer.json中包含以下内容:
Include in composer.json this:
"autoload": {
"classmap": [
"app/traits"
]
}
在类声明后包含在模型中(仅特征名称,不包含 目录):
Include in model after class declaration (just trait name, without directory):
php
class Note extends Eloquent {
use \GreatTrait;
...
}
?>
这篇关于模型中的laravel 4特征自动加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!