Sylius:通过特定路径在 RequestStack 中只有一个不完整的对象
Sylius: Only one incomplete object in RequestStack through particular route
我是 Symfony 初学者,我正在为电子商务系统 Sylius 编写一个插件:到目前为止的每条路线都在 RequestStack 中为我提供了两个对象,并且我能够通过一个在任何地方获得一个非常特殊的变量我通过 $requestStack->getMasterRequest() 检索。
在特定路由中突然无法检索变量(在配置路由的 yaml 文件中设置)并且 $requestStack 不包含两个对象,只有一个。
抱歉,我知道这个解释很糟糕,希望有人能理解。
我尝试了 $request = Request::createFromGlobals() 并检索了 Container 而不是直接检索 RequestStack,但最终结果相同。
# This is the route I am trying to overwrite because I want to give it an extra parameter from the request:
sylius_shop_product_show:
path: /products/{slug}
methods: [GET]
defaults:
_controller: sylius.controller.product:showAction
_sylius:
template: "@SyliusShop/Product/show.html.twig"
repository:
method: findOneByChannelAndSlug_andVarFromRequest
arguments:
- "expr:service('sylius.context.channel').getChannel()"
- "expr:service('sylius.context.locale').getLocaleCode()"
- $slug
- "expr:service('request_stack')"
//This is the function which should be used, it is part of a Repository
public function findOneByChannelAndSlug_andVarFromRequest(ChannelInterface $channel, string $locale, string $slug, RequestStack $requestStack): ?ProductInterface
{
我不明白为什么我突然无法阻止我可以通过所有其他途径访问的变量。因为它是一个存储库?请求还没有完全处理吗?
尝试在名称前输入带有 $
符号的变量名。例如,如果您的变量名称是 myVar
:
sylius_shop_product_show:
path: /products/{slug}
methods: [GET]
defaults:
_controller: sylius.controller.product:showAction
_sylius:
template: "@SyliusShop/Product/show.html.twig"
repository:
method: findOneByChannelAndSlug_andVarFromRequest
arguments:
- "expr:service('sylius.context.channel').getChannel()"
- "expr:service('sylius.context.locale').getLocaleCode()"
- $slug
- $myVar
它是如何工作的:https://github.com/Sylius/SyliusResourceBundle/blob/1.4/src/Bundle/Controller/ParametersParser.php#L55 注意:$request->get()
方法是从 GET、POST 和 Symfony 属性获取变量的别名。
我昨天自己解决了这个问题。问题是我没有在路由描述的正确范围内声明 sylius_shop_product_show ,它也声明了我试图通过的变量,因此 Symfony 逻辑上无法解析它并使其可用。好吧,现在看到解决方案是非常合理的,但在我是 Symfony(以及 Sylius)初学者之前,我很难理解哪里出了问题。
所以当我执行 php bin/console debug:router 时我明白了这个问题,它列出了所有可用的路由,当我发现路由 sylius_shop_product_show 实际上没有创建时我正在寻找的变量甚至在路由路径中可用的路由。感谢所有试图帮助我的人!
我是 Symfony 初学者,我正在为电子商务系统 Sylius 编写一个插件:到目前为止的每条路线都在 RequestStack 中为我提供了两个对象,并且我能够通过一个在任何地方获得一个非常特殊的变量我通过 $requestStack->getMasterRequest() 检索。
在特定路由中突然无法检索变量(在配置路由的 yaml 文件中设置)并且 $requestStack 不包含两个对象,只有一个。
抱歉,我知道这个解释很糟糕,希望有人能理解。
我尝试了 $request = Request::createFromGlobals() 并检索了 Container 而不是直接检索 RequestStack,但最终结果相同。
# This is the route I am trying to overwrite because I want to give it an extra parameter from the request:
sylius_shop_product_show:
path: /products/{slug}
methods: [GET]
defaults:
_controller: sylius.controller.product:showAction
_sylius:
template: "@SyliusShop/Product/show.html.twig"
repository:
method: findOneByChannelAndSlug_andVarFromRequest
arguments:
- "expr:service('sylius.context.channel').getChannel()"
- "expr:service('sylius.context.locale').getLocaleCode()"
- $slug
- "expr:service('request_stack')"
//This is the function which should be used, it is part of a Repository
public function findOneByChannelAndSlug_andVarFromRequest(ChannelInterface $channel, string $locale, string $slug, RequestStack $requestStack): ?ProductInterface
{
我不明白为什么我突然无法阻止我可以通过所有其他途径访问的变量。因为它是一个存储库?请求还没有完全处理吗?
尝试在名称前输入带有 $
符号的变量名。例如,如果您的变量名称是 myVar
:
sylius_shop_product_show:
path: /products/{slug}
methods: [GET]
defaults:
_controller: sylius.controller.product:showAction
_sylius:
template: "@SyliusShop/Product/show.html.twig"
repository:
method: findOneByChannelAndSlug_andVarFromRequest
arguments:
- "expr:service('sylius.context.channel').getChannel()"
- "expr:service('sylius.context.locale').getLocaleCode()"
- $slug
- $myVar
它是如何工作的:https://github.com/Sylius/SyliusResourceBundle/blob/1.4/src/Bundle/Controller/ParametersParser.php#L55 注意:$request->get()
方法是从 GET、POST 和 Symfony 属性获取变量的别名。
我昨天自己解决了这个问题。问题是我没有在路由描述的正确范围内声明 sylius_shop_product_show ,它也声明了我试图通过的变量,因此 Symfony 逻辑上无法解析它并使其可用。好吧,现在看到解决方案是非常合理的,但在我是 Symfony(以及 Sylius)初学者之前,我很难理解哪里出了问题。
所以当我执行 php bin/console debug:router 时我明白了这个问题,它列出了所有可用的路由,当我发现路由 sylius_shop_product_show 实际上没有创建时我正在寻找的变量甚至在路由路径中可用的路由。感谢所有试图帮助我的人!