duancuan6466 2015-12-08 18:41
浏览 65

PHP名称空间问题与特征

I'm having the strangest problem today, where something that always works has somehow stopped working. Odds are, it's one of those stupid simple issues that I'm too blind to see.

So here's what's going on:

I have the following file structure (I'm only showing relevant files)

~/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;
    }
}

And I end up getting the error:

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

And I have no idea why.


If it matters, I'm using Laravel 5.1 as my Framework.

Edit:

Here's the composer.json file, in case that matters:

{
    "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条回答 默认 最新

  • douyu2817 2015-12-08 19:19
    关注

    After further analysis, I found the issue, and was definitely one of those stupid simple issues that I'm was too blind to see.

    Simply put, the Sluggable.php file lacked the .php extension. I guess I'm just so used to see file names without their extensions that I didn't notice.

    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制