英文:
Route Custom binding Laravel overwrite
问题
因为我们使用中间件API,所以我们修复了一个自定义路由绑定器,该绑定器针对我们的中间件API而不是通用数据库进行查找。
问题是现在。当有多个参数时,它只会检索最后一个绑定。它会覆盖先前的调用。
绑定器
public function syncBinding(): void
{
if (array_key_exists(mb_strtolower(class_basename($this)), self::$bindable)) {
return;
}
$routeBinding = Request::getRouteResolver()->call($this);
$parameters = $routeBinding->parameters();
$bindingFields = $routeBinding->bindingFields();
$bind = mb_strtolower(class_basename($this));
if (array_key_exists($bind, self::$bindable) || ! array_key_exists($bind, $parameters)) {
return;
}
$column = null;
if (array_key_exists($bind, $bindingFields)) {
$column = $bindingFields[$bind];
}
self::$bindable[$bind] = true;
$this->items = $this->resolveBinding($this, $parameters[$bind], $column);
}
items属性保存了绑定,但一旦有多个参数就会被覆盖。
如何修复这个问题?
英文:
Because we work with a middleware API, we fixed a custom route binder that looks against our middleware API instead of the general database by Eloquent.
Problem is now. When there is more then 1 parameter it only retrieve the last binding. It overrides the previous call.
Binder
public function syncBinding(): void
{
if (array_key_exists(mb_strtolower(class_basename($this)), self::$bindable)) {
return;
}
$routeBinding = Request::getRouteResolver()->call($this);
$parameters = $routeBinding->parameters();
$bindingFields = $routeBinding->bindingFields();
$bind = mb_strtolower(class_basename($this));
if (array_key_exists($bind, self::$bindable) || ! array_key_exists($bind, $parameters)) {
return;
}
$column = null;
if (array_key_exists($bind, $bindingFields)) {
$column = $bindingFields[$bind];
}
self::$bindable[$bind] = true;
$this->items = $this->resolveBinding($this, $parameters[$bind], $column);
}
The items property is holding the binding, but it will be overridden once there are more then 1 parameters.
How can I fix this?
答案1
得分: 2
根据您的代码,问题出现在每次调用syncBinding
函数时都会覆盖$this->items
属性。如果有多个参数,每次调用都会覆盖上一次的结果。因此,该属性只保留最后一个参数处理的结果。
一个解决方法是将$this->items
从保存单个项目更改为保存项目数组。每次调用syncBinding
时,而不是覆盖$this->items
的当前值,您可以将新值推送到数组中。您可以尝试以下解决方案。可能会出现一些错误,您可以进行调试和修复。但总体思路应该是这样的:
public function syncBinding(): void
{
if (array_key_exists(mb_strtolower(class_basename($this)), self::$bindable)) {
return;
}
$routeBinding = Request::getRouteResolver()->call($this);
$parameters = $routeBinding->parameters();
$bindingFields = $routeBinding->bindingFields();
$bind = mb_strtolower(class_basename($this));
if (array_key_exists($bind, self::$bindable) || ! array_key_exists($bind, $parameters)) {
return;
}
$column = null;
if (array_key_exists($bind, $bindingFields)) {
$column = $bindingFields[$bind];
}
self::$bindable[$bind] = true;
if (!is_array($this->items)) {
$this->items = [];
}
array_push($this->items, $this->resolveBinding($this, $parameters[$bind], $column));
}
请注意,通过这些更改,$this->items
现在将是包含每个参数的解析绑定的数组。因此,您可能需要调整使用$this->items
的其余代码,以考虑这个更改。
英文:
Based on your code, the issue occurs because the $this->items
property is overwritten each time the syncBinding
function is called. If there are multiple parameters, each call overwrites the previous result. Therefore, the property only retains the result from the last parameter processed.
One solution could be to change $this->items
from holding a single item to holding an array of items. Each time syncBinding
is called, instead of overwriting the current value of $this->items
, you would push the new value onto the array. You can try following solution. Maybe some errors can occur and you can debug and fix it. But general idea should be like this:
public function syncBinding(): void
{
if (array_key_exists(mb_strtolower(class_basename($this)), self::$bindable)) {
return;
}
$routeBinding = Request::getRouteResolver()->call($this);
$parameters = $routeBinding->parameters();
$bindingFields = $routeBinding->bindingFields();
$bind = mb_strtolower(class_basename($this));
if (array_key_exists($bind, self::$bindable) || ! array_key_exists($bind, $parameters)) {
return;
}
$column = null;
if (array_key_exists($bind, $bindingFields)) {
$column = $bindingFields[$bind];
}
self::$bindable[$bind] = true;
if (!is_array($this->items)) {
$this->items = [];
}
array_push($this->items, $this->resolveBinding($this, $parameters[$bind], $column));
}
Please note that with these changes, $this->items
will now be an array containing all the resolved bindings for each parameter. So, you may need to adjust the rest of your code that makes use of $this->items
to account for this change.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论