英文:
Shopware 6 Plugin - SEO Friendly Pagination: , null is returned from app.request.get('properties')
问题
我目前正在开发一个名为“SEO友好分页”的Shopware 6插件,旨在使产品类别中的分页URL对搜索引擎爬虫可访问。为实现此目标,我扩展了分页模板,以包括每个分页中的不可见href元素。这些href元素使用反映当前页面上下文的SEO友好URL生成,包括排序顺序和筛选器。
我的自定义插件pagination.html.twig中的分页模板如下所示:
{# 扩展 storefront 中的 'pagination.html.twig' 模板 #}
{% sw_extends '@Storefront/storefront/component/pagination.html.twig' %}
{# 定义 'component_pagination' 块 #}
{% block component_pagination %}
{# 从上下文中访问 'TanmarSeoPagination' 数据 #}
{% set TanmarSeoPaginationData = context.context.extensions.tanmarNgSeoPagination %}
{# 检查 TanmarSeoPagination 是否处于活动状态 #}
{% if TanmarSeoPaginationData.active %}
{# 检查页头中是否定义了活动导航ID #}
{% if page.header.navigation.active.id is defined %}
{% set currentNavigationId = page.header.navigation.active.id %}
{% endif %}
{# 检查搜索过滤器中是否定义了导航ID #}
{% if searchResult.currentFilters.navigationId is defined %}
{% set currentNavigationId = searchResult.currentFilters.navigationId %}
{% endif %}
{% for i in 1..totalPages %}
{# 设置一个空的SEO参数对象 #}
{% set seoParams = {} %}
{# 检查并设置请求中的 'order' 参数 #}
{% set order = app.request.get('order') %}
{% if order %}
{% set seoParams = seoParams|merge({ order: order }) %}
{% endif %}
{# 计算下一页页码 #}
{% set pageNumber = currentPage + 1 %}
{% if i != currentPage %}
{% if pageNumber %}
{% set seoParams = seoParams|merge({ p: i }) %}
{% endif %}
{# 检查并设置请求中的 'properties' 参数 #}
{% set properties = app.request.get('properties') %}
{{ dump(properties) }}
{% if properties %}
{% set seoParams = seoParams|merge({ properties: app.request.get('properties') ~ seoParams }) %}
{% endif %}
{# 在使用之前将SEO参数正确编码为字符串 #}
{% set seoParams = '?' ~ seoParams|url_encode %}
{# 检查是否定义了currentNavigationId并生成SEO友好的URL #}
{% if currentNavigationId is defined %}
<a href="{{ seoUrl('frontend.navigation.page', { navigationId: currentNavigationId }) ~ seoParams }}"></a>
{% endif %}
{% endif %}
{% endfor %}
{% endif %}
{# 调用父分页块 #}
{{ parent() }}
{% endblock %}
请问有人可以帮助我确定可能导致返回null的原因以及如何修复吗?有没有办法像我访问订单一样访问属性?
提前感谢您的帮助!
最好的问候,
Lodhi
英文:
I'm currently developing a Shopware 6 plugin called "SEO Friendly Pagination" that aims to make paginated URLs in product categories accessible to search engine crawlers. To achieve this, I have extended the pagination template to include invisible href elements for each page in the pagination. These href elements are generated with SEO-friendly URLs that reflect the current context of the page, including sorting order and filters.
The pagination template in my custom plugin pagination.html.twig looks like this:
{# Extend the 'pagination.html.twig' template from the storefront #}
{% sw_extends '@Storefront/storefront/component/pagination.html.twig' %}
{# Define the 'component_pagination' block #}
{% block component_pagination %}
{# Access the 'TanmarSeoPagination' data from the context #}
{% set TanmarSeoPaginationData = context.context.extensions.tanmarNgSeoPagination %}
{# Check if TanmarSeoPagination is active #}
{% if TanmarSeoPaginationData.active %}
{# Check if active navigation ID is defined in page header #}
{% if page.header.navigation.active.id is defined %}
{% set currentNavigationId = page.header.navigation.active.id %}
{% endif %}
{# Check if navigation ID is defined in search filters #}
{% if searchResult.currentFilters.navigationId is defined %}
{% set currentNavigationId = searchResult.currentFilters.navigationId %}
{% endif %}
{% for i in 1..totalPages %}
{# Set up an empty SEO parameters object #}
{% set seoParams = {} %}
{# Check and set 'order' parameter from request #}
{% set order = app.request.get('order') %}
{% if order %}
{% set seoParams = seoParams|merge({ order: order }) %}
{% endif %}
{# Calculate the next page number #}
{% set pageNumber = currentPage + 1 %}
{% if i != currentPage %}
{% if pageNumber %}
{% set seoParams = seoParams|merge({ p: i }) %}
{% endif %}
{# Check and set 'properties' parameter from request #}
{% set properties = app.request.get('properties') %}
{{ dump(properties) }}
{% if properties %}
{% set seoParams = seoParams|merge({ properties: app.request.get('properties') ~ seoParams }) %}
{% endif %}
{# Properly encode the SEO parameters into a string before using it #}
{% set seoParams = '?' ~ seoParams|url_encode %}
{# Check if currentNavigationId is defined and generate SEO-friendly URL #}
{% if currentNavigationId is defined %}
<a href="{{ seoUrl('frontend.navigation.page', { navigationId: currentNavigationId }) ~ seoParams }}"></a>
{% endif %}
{% endif %}
{% endfor %}
{% endif %}
{# Call the parent pagination block #}
{{ parent() }}
{% endblock %}
Could someone please help me identify what might be causing this to return null and how I can fix it? Is there anyway I can access the properties the way I am accessing the order?
Thank you in advance for your assistance!
Best regards,
Lodhi
答案1
得分: 2
这部分可以翻译如下:
这段代码在本地运行时没有问题:
{% set properties = app.request.get('properties') %}
{{ dump(properties) }}
{# 01896e38492873aca93edde1c6a6b3a4|01896e38492873aca93edde1c76853fe #}
但是出现问题的是这一行:
{% set seoParams = seoParams|merge({ properties: app.request.get('properties') ~ seoParams }) %}
这导致了模板渲染错误。因为您已经在seoParams
中进行了合并,所以在这一点上不需要再连接它们,因为您稍后会对它们进行URL编码。移除连接操作 ~ seoParams
使一切按照我的预期工作。
英文:
This works for me locally without issue:
{% set properties = app.request.get('properties') %}
{{ dump(properties) }}
{# 01896e38492873aca93edde1c6a6b3a4|01896e38492873aca93edde1c76853fe #}
What didn't work was this line:
{% set seoParams = seoParams|merge({ properties: app.request.get('properties') ~ seoParams }) %}
That caused an error rendering the template. You're already merging into seoParams
so you really don't need to concatenate them at that point, since you're URL encoding them later on. Removing the concatenation ~ seoParams
made everything work as expected for me.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论