站长我自己新建了一个Facade
namespace App\Facade;
use Illuminate\Support\Facades\Facade;
class Encrypt extends Facade{
protected static function getFacadeAccessor(){
return 'encrypt';
}
}
然后 ServiceProvider 也创建了
public function register()
{
$this->app->bind('encrypt',function (){
return new Encrypt();
});
}
也在app文件里面导入了
'Encrypt' => App\Facade\Encrypt::class,
现在问题来了:
我在路由文件里面调用
Route::get('/',function (){
dd(Encrypt::encrypt('123'));
});
正常打印结果
但是我在UserModel里面用就报错
public function setPasswordAttribute($password){
$this->attributes['password'] = Encrypt::encrypt($password);
// $this->attributes['password'] = app('encrypt')->encrypt($password);
}
用下面的app('encrypt')方式使用就正常
报错如下:
FatalErrorException in User.php line 27:
Class 'App\Encrypt' not found
我项目结构是
app/Encrypt/Encrypt.php
app/Facade/Encrypt.php