We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
当使用 Show\Field 组件进行链式方法调用时,若方法名与 Illuminate\Support\Str 门面的宏方法同名,会导致错误的方法调用路由。系统会尝试调用不存在的 Str 门面方法而不是字段对象的方法,从而抛出异常。
Show\Field
Illuminate\Support\Str
Str
错误示例:
// 控制器代码 $show->field('path') ->link('#') ->setElementClass('custom-class'); // 抛出异常: // BadMethodCallException: Method Illuminate\Support\Str::setElementClass does not exist
protected function detail($id) { return Show::make($id, Model::class, function (Show $show) { $show->field('path') ->link('#') ->setElementClass('custom-class'); }); }
访问该模型的详情页
观察控制台错误输出:
BadMethodCallException: Method Illuminate\Support\Str::setElementClass does not exist
应该成功调用 Dcat\Admin\Show\Field::setElementClass() 方法,为字段元素添加指定 CSS 类。
Dcat\Admin\Show\Field::setElementClass()
系统错误地尝试调用 Illuminate\Support\Str::setElementClass() 方法,导致抛出异常。
Illuminate\Support\Str::setElementClass()
通过调试跟踪发现以下关键点:
方法调用路由机制 当字段值被转换为字符串时(在 toHtml() 方法中),会触发 __call 魔术方法的路由判断
toHtml()
__call
源码定位
src/Show/Field.php:688 (__call())
src/Show/Field.php:729 (toHtml())
冲突根源 当方法名同时存在于以下两个位置时会发生路由错误:
Field
setElementClass
Field::macro()
通过自定义视图显式传递参数:
$show->field('path') ->link(admin_route(...)) ->view('admin.show.link-field', [ 'class' => 'field-path', 'href' => admin_route(...), 'value' => $model->name ]);
对应视图文件:
<!-- resources/views/admin/show/link-field.blade.php --> <a href="{{ $href }}" class="{{ $class }}">{{ $value }}</a>
方法名冲突检测 在 Field::__call() 方法中添加优先级判断:
Field::__call()
if (method_exists($this, $method)) { return $this->$method(...$parameters); } if (Str::hasMacro($method)) { // ...当前处理逻辑 }
别名方法注册 为常用方法注册保护性别名:
Field::macro('elementClass', function ($class) { return $this->setHtmlAttribute('class', $class); });
文档补充 在 Show Field 文档 中添加方法名冲突警告说明
关联仓库: https://github.com/jqhph/dcat-admin
The text was updated successfully, but these errors were encountered:
No branches or pull requests
链式方法调用时与 Str 门面方法名冲突导致 BadMethodCallException
问题描述
当使用
Show\Field
组件进行链式方法调用时,若方法名与Illuminate\Support\Str
门面的宏方法同名,会导致错误的方法调用路由。系统会尝试调用不存在的Str
门面方法而不是字段对象的方法,从而抛出异常。错误示例:
复现步骤
访问该模型的详情页
观察控制台错误输出:
预期行为
应该成功调用
Dcat\Admin\Show\Field::setElementClass()
方法,为字段元素添加指定 CSS 类。实际行为
系统错误地尝试调用
Illuminate\Support\Str::setElementClass()
方法,导致抛出异常。环境信息
问题分析
通过调试跟踪发现以下关键点:
方法调用路由机制
当字段值被转换为字符串时(在
toHtml()
方法中),会触发__call
魔术方法的路由判断源码定位
src/Show/Field.php:688 (__call())
src/Show/Field.php:729 (toHtml())
冲突根源
当方法名同时存在于以下两个位置时会发生路由错误:
Field
组件方法 (如setElementClass
)Str
门面宏方法 (通过Field::macro()
注册)临时解决方案
通过自定义视图显式传递参数:
对应视图文件:
建议改进方案
方法名冲突检测
在
Field::__call()
方法中添加优先级判断:别名方法注册
为常用方法注册保护性别名:
文档补充
在 Show Field 文档 中添加方法名冲突警告说明
关联仓库:
https://github.com/jqhph/dcat-admin
The text was updated successfully, but these errors were encountered: