未找到Laravel包装特质

eCo*_*Evo 3 php laravel composer-php

我正在开发一个包并在我的PSR-4结构中添加了一个"Traits"目录,这反映在包的composer.json中.

{
    "name": "my-private-repo/prefs",
    "description": "Preferences package.",
    "type": "package",
    "keywords": [
        "prefs",
        "preferences"
    ],
    "require": {
        "php": ">=5.5.9",
        "illuminate/support": "5.2.*",
        "laravelcollective/html": "5.2.*",
        "anahkiasen/former": "~4"
    },
    "autoload": {
        "classmap": [
            "src/controllers",
            "src/models"
        ],
        "psr-4": {
            "MyPrivateRepo\\Prefs\\": "src/"
        },
        "files": [
            "src/Prefs/helpers.php"
        ]
    },
    "minimum-stability": "dev",
    "prefer-stable": true
}
Run Code Online (Sandbox Code Playgroud)

src/目录内是这种结构:

Prefs/
    Traits/
        HasPrefs.php
    Prefs.php
    PrefsServiceProvider.php
    helpers.php
Run Code Online (Sandbox Code Playgroud)

这是以下内容HasPrefs.php:

namespace MyPrivateRepo\Prefs\Traits;

use MyPrivateRepo\Prefs\Prefs;

trait HasPrefs
{
    public function prefs($key = null, $value = null)
    {
        //...do pref related stuff here...
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经加载了私有项目,Prefs直接调用类时一切都很好.然后我决定测试HasPrefs为我的User模型添加特征:

namespace App;

use Cartalyst\Sentinel\Users\EloquentUser;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use MyPrivateRepo\Prefs\Traits\HasPrefs;

class User extends EloquentUser implements
    AuthenticatableContract,
    AuthorizableContract,
    CanResetPasswordContract
{
    use SoftDeletes,
        Authenticatable,
        Authorizable,
        HasPrefs,
        CanResetPassword;

    //...do lots of user related stuff here...
}
Run Code Online (Sandbox Code Playgroud)

现在,每次我尝试做任何事情,我都会遇到这个错误:

[Symfony\Component\Debug\Exception\FatalErrorException]
Trait 'MyPrivateRepo\Prefs\Traits\HasPrefs' not found
Run Code Online (Sandbox Code Playgroud)

然后我做了composer update但是当它到达运行的脚本部分时得到了与上面相同的错误php artisan optimize.

我注释掉了HasPrefsin User和re-ran 的引用,这些引用artisan optimize没有问题.

取消注释HasPrefs并且所有工作都按预期工作,没有错误......直到下次我需要composer update再次运行项目.然后,我回到必须HasPrefs再次注释掉引用并手动运行artisan optimize.

考虑到这是一件需要部署到生产服务器,我不能手动编辑使用这个特质,每次有一个每个文件composer updatecomposer install通过自动部署触发.

我尝试过以下方法来解决这个问题,但没有成功:

  • 工匠清除缓存
  • 作曲家自我更新
  • 作曲家清除&&作曲家更新-o
  • 再次评论出HasPrefs:artisan clear-compiled && artisan optimize
  • rm composer.lock && composer update -o
  • rm -rf vendor/*&& composer install

composer update如果以上任何一个暂时解决问题,只需要一次打电话就可以重新打破它.

任何想法在这里有什么不对?

Fil*_*ski 5

正如你在这里看到的:

"psr-4": {
    "MyPrivateRepo\\Prefs\\": "src/"
},
Run Code Online (Sandbox Code Playgroud)

您将srcdir 指向MyPrivateRepo\Prefs名称空间.所以当你想在名为Traits你的命名空间的dir中使用这个特性应该是这样的:

MyPrivateRepo\Prefs\Prefs\Traits
Run Code Online (Sandbox Code Playgroud)

因为你有一个Prefs,所以你use在这个特征中的陈述将如下所示:

 use MyPrivateRepo\Prefs\Prefs\Prefs;
Run Code Online (Sandbox Code Playgroud)

我的建议是改变作曲家的条目:

"psr-4": {
    "MyPrivateRepo\\": "src/"
},
Run Code Online (Sandbox Code Playgroud)