PHP名称空间问题与特征

时间:2015-12-08 18:41:23

标签: php laravel namespaces laravel-5.1 traits

我今天遇到了一个最奇怪的问题,那些总是有效的东西已经停止了工作。可能性是,这是我无法看到的那些愚蠢的简单问题之一。

所以这里发生了什么:

我有以下文件结构(我只显示相关文件)

~/project
 v app
   v Models
        User.php
   v Support
     v Traits
        Sluggable.php
   composer.json
   composer.lock

user.php的

<?php

namespace App\Models;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
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 App\Support\Traits\Sluggable;

class User extends Model implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract
{
    //////////////
    //* Traits *//
    //////////////
    use Authenticatable, Authorizable, CanResetPassword, Sluggable;

    ////////////////////////
    //* Model Attributes *//
    ////////////////////////
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'email', 'password'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];

    /////////////////////
    //* Boot Override *//
    /////////////////////
    /**
     * Override the Boot Method to schedule tasks that are Event-Triggered.
     *
     * @return void
     */
    public static function boot()
    {
        // Call the Parent Method
        parent::boot();

        // Intercept the 'Created' Event
        static::creating(function($user)
        {
            // Assign a Validation Token
            $user->validation_token = str_slug(str_random(64));

            // Initialize the Slug for the User
            $user->slug = $user->makeSlug();
        });
    }

    ////////////////////////////
    //* Attributes Overrides *//
    ////////////////////////////
    /**
     * Overrides the $user->name Attibute to update the Sluggable Name.
     *
     * @return void
     */
    public function setNameAttribute($name)
    {
        $this->attributes['name'] = $name;

        $this->attributes['slug'] = $this->makeSlug();
    }
}

Traits.php

<?php

namespace App\Support\Traits;

trait Sluggable
{
    /**
     * The Name of the Attribute that determines the Slug.
     *
     * Use $this->$sluggableAttribute to determine the Sluggable Value.
     *
     * @var string
     */
    protected $sluggableAttribute = 'name';

    /**
     * The Name of the Attribute that contains the Slug.
     *
     * Use $this->$slugAttribute to determine the Slug Value.
     *
     * @var string
     */
    protected $slugAttribute = 'slug';

    /**
     * Generates a Slug based on the Sluggable Attribute.
     *
     * @return string
     */
    public function makeSlug()
    {
        // Intialize the Slug
        $this->$slugAttribute = str_slug($this->$sluggableAttribute);

        // Check for an existing Slug
        $slug = $this->whereRaw("{$this->slugAttribute} RLIKE '^{$this->$sluggableAttribute}(-[0-9]+)?$'")
            ->latest($timestamps ? UPDATED_AT : $this->primaryKey) // Find the Latest Slug
            ->pluck($this->slugAttribute); // Only grab the Slug Attribute

        // See if a Slug was Found
        if($slug)
        {
            // Determine the new Slug Identifier
            $slugID = intval(end(explode('-', $slug))) + 1;

            // Reassign a new Slug
            $this->$slugAttribute .= '-' . $slugID;
        }

        return $this->$slugAttribute;
    }
}

我最终收到错误:

FatalErrorException in User.php line 19:
Trait 'App\Support\Traits\Sluggable' not found

我不明白为什么。

如果重要,我会使用Laravel 5.1作为我的框架。

修改

这是 composer.json 文件,如果重要的话:

{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": ">=5.5.9",
        "laravel/framework": "5.1.*",
        "illuminate/html": "^5.0"
    },
    "require-dev": {
        "fzaninotto/faker": "~1.4",
        "mockery/mockery": "0.9.*",
        "phpunit/phpunit": "~4.0",
        "phpspec/phpspec": "~2.1"
    },
    "autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "App\\": "app/"
        },
        "files": [
            "app/Helpers/route.php"
        ]
    },
    "autoload-dev": {
        "classmap": [
            "tests/TestCase.php"
        ]
    },
    "scripts": {
        "post-install-cmd": [
            "php artisan clear-compiled",
            "php artisan optimize"
        ],
        "pre-update-cmd": [
            "php artisan clear-compiled"
        ],
        "post-update-cmd": [
            "php artisan optimize"
        ],
        "post-root-package-install": [
            "php -r \"copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "php artisan key:generate"
        ]
    },
    "config": {
        "preferred-install": "dist"
    }
}

1 个答案:

答案 0 :(得分:0)

经过进一步的分析,我发现了这个问题,这绝对是我看不太明白的那些愚蠢的简单问题之一。

简单地说, Sluggable.php 文件缺少 .php 扩展名。我想我只是习惯于看到没有我们没注意到的扩展名的文件名。