在 laravel Controller 中使用多个 Trait
use multiple Trait in laravel Controller
在 Controller 中使用多个 Trait 时出现问题
特质
trait A
{
public function name()
{
return 'A';
}
}
trait B
{
public function name()
{
return 'B';
}
}
trait C
{
public function name()
{
return 'C';
}
}
trait D
{
public function name()
{
return 'D';
}
}
在 CR 控制器中
class CRController extends Controller
{
use A {
name AS A_name;
}
use B {
name AS B_name;
}
use C {
name AS C_name;
}
use D {
name AS D_name;
}
}
错误日志
[2021-10-11 14:19:40] local.ERROR: Trait method CREATES_IS_NAME_METHOD has not been applied, because there are collisions with other trait methods on App\Http\Controllers\Panel\CRController {"exception":"[object] (Symfony\Component\Debug\Exception\FatalErrorException(code: 64): Trait method CREATES_IS_NAME_METHOD has not been applied, because there are collisions with other trait methods on App\Http\Controllers\PNAME\CRController at /var/www/html/app/Http/Controllers/PNAME/CRController.php:26)
[stacktrace]
#0 {main}
"}
$ php -v
PHP 7.2.24 (cli) (built: Oct 22 2019 08:28:36) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
执行命令...
作曲家dump-autoload
作曲家 dump-autoload -o
php artisan config:clear
php artisan view:clear
php artisan route:clear
php artisan cache:clear
不工作!
这叫做Conflict Resolution。简而言之,您需要 select 一个默认的 name
方法。
To resolve naming conflicts between Traits used in the same class, the insteadof operator needs to be used to choose exactly one of the conflicting methods.
您所需要的只是使用 A::name 而不是其他的。并为其他特征方法定义另一个名称。
class CRController extends Controller
{
use A {
A::name insteadof B,C,D;
A::name AS A_name;
}
use B {
B::name AS B_name;
}
use C {
C::name AS C_name;
}
use D {
D::name AS D_name;
}
}
在 app 目录中创建新的文件夹 traits app/traits 然后为 tait 创建新的 php 文件,TestTrait.php 在 Controller 文件中使用上面的 trait
演示();}?>
在 Controller 中使用多个 Trait 时出现问题
特质
trait A
{
public function name()
{
return 'A';
}
}
trait B
{
public function name()
{
return 'B';
}
}
trait C
{
public function name()
{
return 'C';
}
}
trait D
{
public function name()
{
return 'D';
}
}
在 CR 控制器中
class CRController extends Controller
{
use A {
name AS A_name;
}
use B {
name AS B_name;
}
use C {
name AS C_name;
}
use D {
name AS D_name;
}
}
错误日志
[2021-10-11 14:19:40] local.ERROR: Trait method CREATES_IS_NAME_METHOD has not been applied, because there are collisions with other trait methods on App\Http\Controllers\Panel\CRController {"exception":"[object] (Symfony\Component\Debug\Exception\FatalErrorException(code: 64): Trait method CREATES_IS_NAME_METHOD has not been applied, because there are collisions with other trait methods on App\Http\Controllers\PNAME\CRController at /var/www/html/app/Http/Controllers/PNAME/CRController.php:26)
[stacktrace]
#0 {main}
"}
$ php -v
PHP 7.2.24 (cli) (built: Oct 22 2019 08:28:36) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
执行命令...
作曲家dump-autoload
作曲家 dump-autoload -o
php artisan config:clear
php artisan view:clear
php artisan route:clear
php artisan cache:clear
不工作!
这叫做Conflict Resolution。简而言之,您需要 select 一个默认的 name
方法。
To resolve naming conflicts between Traits used in the same class, the insteadof operator needs to be used to choose exactly one of the conflicting methods.
您所需要的只是使用 A::name 而不是其他的。并为其他特征方法定义另一个名称。
class CRController extends Controller
{
use A {
A::name insteadof B,C,D;
A::name AS A_name;
}
use B {
B::name AS B_name;
}
use C {
C::name AS C_name;
}
use D {
D::name AS D_name;
}
}
在 app 目录中创建新的文件夹 traits app/traits 然后为 tait 创建新的 php 文件,TestTrait.php 在 Controller 文件中使用上面的 trait
演示();}?>