首页 > 解决方案 > PHP,Laravel,未找到特征

问题描述

更新

经过太多次试验和失败后,我注意到这个错误有点误导。我回到旧版本之一,抓取旧文件并一一引入新版本。事实证明,当我更改 Publishings.php 时引发了错误,这与错误消息中提到的不同。

更新 2

这两个特征 Posts.php 和 Publishings.php 相互使用。我认为这会产生某种冲突。当我断开链接时,一切都恢复了。

原帖

我遇到了我遇到过的最奇怪的错误。我希望有人能帮帮忙。我有一堆特质,而且都互相使用。

/app
    /Traits
        /Products.php
        /Publishings.php
        /Posts.php
        /...

Publishings.php 用于 5 个控制器和 2 个特征并且工作正常。但只有一个特征,它给出了这个错误:

Symfony\Component\ErrorHandler\Error\FatalError
Trait "App\Traits\Publishings" not found

引发错误的特征是 Posts.php:

<?php

namespace App\Traits;

use App\Models\Post;
use App\Models\Publishing;
use App\Traits\Pictures;
use App\Traits\Publishings;
use App\Traits\Videos;
use Carbon\Carbon;

trait Posts
{

    use Pictures, Publishings, Videos;

    public function...

}

错误来自第 12 行。该行的内容是“trait Posts”。

HomeController 也使用 Publishings 特征,当我删除使用 App\Traits\Publishings 时,它给了我未定义的函数错误。所以 HomeController 可以找到 Publishings。

我多次尝试清除所有缓存。没有帮助。此外,此应用程序的副本正在共享主机中运行,没有任何问题。我昨天重新安装了 Windows 10 的新副本,我的文件夹结构有点奇怪。即使我看不到任何关系,但我想知道我的问题是否与此有关。

C:\Coding\Projects // this is the document root instead of htdocs
C:\Coding\Platforms\Xampp

我没有主意了。任何帮助将不胜感激。提前致谢。

编辑:

这是 Publishings.php 的命名空间

<?php

namespace App\Traits;

use Illuminate\Support\Str;
use Carbon\Carbon;
use App\Models\Publishing;
use App\Traits\Pictures;
use App\Traits\Paginate;
use App\Traits\Videos;
use App\Traits\Specs;
use App\Traits\Posts;

trait Publishings
{
    use Posts, Pictures, Paginate, Specs, Videos;

}

标签: phplaravel

解决方案


文件夹结构无关紧要。如果您在命名空间中打错字,只要您在其他文件中使用相同的错字,它仍然可以工作。仔细检查命名空间。

你说:

HomeController 也使用 Publishings 特征,当我删除使用 App\Trait\Publishings 时,它给了我未定义的函数错误。所以 HomeController 可以找到 Publishings。

App\Trait\Publishings不一样App\Traits\Publishings!!!


推荐阅读