未找到 Laravel 自定义特征
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37511893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Custom Trait Not Found
提问by Watercayman
I'm new to traits, but thought I'd give it a try. But, it doesn't seem to load.
我是特征的新手,但我想我会尝试一下。但是,它似乎没有加载。
I've created a trait within a folder under the Laravel app directory: app\Helpers called CheckPermsAgainstObjectTrait.php
我在 Laravel 应用程序目录下的文件夹中创建了一个特征: app\Helpers 名为 CheckPermsAgainstObjectTrait.php
Here is the trait code:
这是特征代码:
<?php
namespace App\Helpers;
trait CheckPermsAgainstObjectTrait {
function something{}
}
I try to use it in a controller as such:
我尝试在控制器中使用它:
<?php
namespace App\Http\Controllers;
use this&that;
use App\Helpers\CheckPermsAgainstObjectTrait;
class PolicyController extends Controller{
use CheckPermsAgainstObjectTrait;
}
Classes in that directory load fine. PHPStorm sees the trait fine. I've clear compiled aritsan and dumped autoload. I'm guessing there is something that Laravel doesn't like with the namespacing? I would hope I don't need to do any manual loading in composer -- but I'm having trouble finding any documentation to give me a hint as to what I'm screwing up.
该目录中的类加载良好。PHPStorm 认为这个特性很好。我已经清除了编译的 aritsan 并转储了自动加载。我猜 Laravel 不喜欢命名空间?我希望我不需要在作曲家中进行任何手动加载——但是我找不到任何文档来提示我搞砸了什么。
The error:
错误:
FatalErrorException in PolicyController.php line 15:
Trait 'App\Helpers\CheckPermsAgainstObjectTrait' not found
Any thoughts?
有什么想法吗?
回答by huuuk
Did you dump autoload files?
您是否转储了自动加载文件?
composer dump-autoload
回答by Zack Morris
The answer for me was that I had the wrong namespace at the top of my trait file due to working from a Laravel trait as an example. If your trait is in App/Traits/MyTrait.php
then make sure your namespace is:
我的答案是,由于以 Laravel trait 为例,我的 trait 文件顶部有错误的命名空间。如果您的特征在,App/Traits/MyTrait.php
那么请确保您的命名空间是:
namespace App\Traits;
trait MyTrait
{
// ..
}
Then from the file that's including the trait:
然后从包含特征的文件中:
use App\Traits\MyTrait;
class MyClass
{
use MyTrait;
// ..
}
No need to mess with config/app.php
, composer.json
or autoloading.
无需弄乱config/app.php
,composer.json
或自动加载。
回答by Quisk
I had a very similar problem where I was getting the error Trait 'Tests\CreatesApplication' not found in '.../my_project/tests/TestCase.php' on line 9
while running tests with phpunit
because I manually updated my Laravel project and must have missed some changes. This may also happen to anyone creating a trait or class in a unique namespace.
我有一个非常相似的问题,我Trait 'Tests\CreatesApplication' not found in '.../my_project/tests/TestCase.php' on line 9
在运行测试时遇到错误,phpunit
因为我手动更新了我的 Laravel 项目并且一定错过了一些更改。这也可能发生在任何在唯一命名空间中创建特征或类的人身上。
Anyways, @Devon 's comment on the original question pointed me in the right direction.
无论如何,@Devon 对原始问题的评论为我指明了正确的方向。
I was missing the required autoloader configuration in my composer.json
file, so I checked what it should be for my version of Laravel (5.4 in this case) from the Laravel github repository.
我的文件中缺少所需的自动加载器配置composer.json
,因此我从Laravel github 存储库检查了我的 Laravel 版本(在本例中为 5.4)应该是什么。
In this case, I was not calling from a namespace within App\
(which was already in composer.json
.)
在这种情况下,我不是从内部的命名空间App\
(已经在composer.json
.)
Here's what I was missing from composer.json
:
这是我所缺少的composer.json
:
{
...,
"autoload-dev": {
"psr-4": {
"Tests\": "tests/"
}
},
...
}
Thanks goes to Devon! Hope this keeps someone else from pulling their hair out.
感谢德文郡!希望这可以防止其他人拔头发。