gpt4 book ai didi

带有特征的 PHP 命名空间问题

转载 作者:行者123 更新时间:2023-12-04 16:57:35 25 4
gpt4 key购买 nike

我今天遇到了最奇怪的问题,一直有效的东西不知何故停止了工作。很可能,这是我太盲目而无法看到的那些愚蠢的简单问题之一。

所以这是怎么回事:

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

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

用户.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"
}
}

最佳答案

经过进一步分析,我发现了这个问题,并且绝对是我太盲目而无法看到的那些愚蠢的简单问题之一。

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

关于带有特征的 PHP 命名空间问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34163530/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com