如果请求的页面是主页,我将如何直接检查.phtml模板?

我想将条件添加到给定容器的类

有帮助吗?

解决方案

如果您检查模板的主页,那么您将不正确地接近Magento模板和布局。

我的方法通常是创建核心/模板类型 block ,并通过 layout 添加到正确的布局句柄

可以在此答案找到更多信息:

https://magento.stackexchange.com//30562/336

对此误解概念的更深层次解释可以在我的博客中找到:

https://blog.philwinke.com/the-最误解的 - 概念 - in-magento /

更深入地潜入Magento布局读取艾伦风暴的本书主题:

http://store.pulsestorm.net/products/no-frills -magento-layout

其他提示

您可以在类构造函数中使用\ magento \ framework \ app \ request \ http的实例。如果您在控制器中,您不需要执行此操作。

您可以像此属性icetagcode

一样访问它
public function __construct(
    ...
    \Magento\Framework\App\Request\Http $request
) {
    ...
    $this->_request = $request;
}
.

然后,您可以检查是否是主页或类别页面或productPage:

if ($this->_request->getFullActionName() == 'cms_index_index') {
    //you are on the homepage
}
if ($this->_request->getFullActionName() == 'catalog_product_view') {
    //you are on the product page
}
if ($this->_request->getFullActionName() == 'catalog_category_view') {
    //you are on the category page
}
. 否则,否则使用对象管理器

直接在phtml文件中使用
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$request = $objectManager->get('\Magento\Framework\App\Request\Http');
.

许可以下: CC-BY-SA归因
scroll top