จะตรวจสอบ IsHomePage ใน Magento 2 ได้อย่างไร เราอยู่ในหน้าแรกหรือไม่


9

ฉันต้องการตรวจสอบหน้าปัจจุบันคือหน้าแรกหน้าหมวดหมู่หน้าผลิตภัณฑ์ & หน้า cms ​​ใน magento 2


คุณต้องการตรวจสอบสิ่งนี้ในบริบทใด ควบคุม? บล็อก?
Marius

Magento 1 เวอร์ชั่นที่เราใช้ function Mage :: getBlockSingleton ('page / html_header') -> getIsHomePage (); ฉันต้องการแสดงข้อมูลหน้าเหมือนหน้าแรก, หน้าหมวดหมู่, หน้าผลิตภัณฑ์ & หน้า cms ​​ฯลฯ
MagikVishal

1
ฉันรู้ว่า แต่เพื่อตอบคำถามนี้ฉันต้องการทราบว่าคุณต้องการใช้ใน magento2 ที่ไหน ไม่มีคลาส mage ระดับโลกใน m2
Marius

คำตอบ:


20

คุณสามารถลองทำสิ่งนี้: ใส่อินสแตนซ์ของตัว\Magento\Framework\App\Request\Httpสร้างคลาสของคุณ หากคุณอยู่ในคอนโทรลเลอร์คุณไม่จำเป็นต้องทำ คุณสามารถเข้าถึงแบบนี้ได้แล้ว$request = $this->getRequest()

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

จากนั้นคุณสามารถตรวจสอบว่าเป็นหน้าแรกเช่นนี้:

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
}

@marius - ฉันจะตรวจสอบสิ่งเดียวกันในไฟล์ phtml ได้อย่างไร
Manashvi Birla

2
เขียนวิธีในบล็อกที่ส่งคืน$this->_request->getFullActionName()โฆษณาใช้ในไฟล์ phtml
Marius

คุณสามารถทำอย่างละเอียด? เราจะทำอย่างนั้นได้อย่างไร?
สูงสุด

@ Max คุณต้องการรายละเอียดเพิ่มเติมเพิ่มเติมอีกไหม? ฉันคิดว่าคำตอบนั้นชัดเจน? บอกสิ่งที่คุณต้องการรู้
Marius

ขออภัยฉันอ่านใหม่และชัดเจน ขอบคุณ.
สูงสุด

7

จากภายในไฟล์ HTML นี้ใช้งานได้สำหรับฉัน:

if ($this->getRequest()->getFullActionName() == 'cms_index_index') {
    //you are on the homepage
}
if ($this->getRequest()->getFullActionName() == 'catalog_product_view') {
    //you are on the product page
}
if ($this->getRequest()->getFullActionName() == 'catalog_category_view') {
    //you are on the category page
}

มันใช้งานได้สำหรับฉัน
sandip

3

ลองอันนี้:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$request = $objectManager->get('Magento\Framework\App\Action\Context')->getRequest();
if ($request->getFullActionName() == 'cms_index_index') {
    // is homepage
}

2
อย่าใช้ตัวจัดการวัตถุ
Marius

สิ่งนี้ใช้ได้สำหรับฉัน ทำไมไม่ใช้ Object Manager?
TheBlackBenzKid

เป็นวิธีปฏิบัติที่ไม่ถูกต้องที่จะใช้ Object Manager โดยตรง หมอวีโอไอพี dev doc กล่าวว่า "วีโอไอพีห้ามใช้ ObjectManager โดยตรงในรหัสของคุณเพราะมันซ่อนการพึ่งพาที่แท้จริงของชั้นเรียนไว้"
Makwana Ketan

0

เพราะรูปแบบการออกแบบพึ่งพาการฉีด คุณสร้างโมดูลเพื่อร้องขอทรัพยากรตามความต้องการ ตัวจัดการวัตถุขัดกับกระบวนทัศน์นั้น อย่างไรก็ตามมันใช้งานได้ดี แต่ก็เหมือนกับการใช้ Mage อีกครั้ง - ช้า


อ๊ะควรจะแสดงความคิดเห็นข้างต้นขของฉัน
Chris Anderson

0

ลองรหัสด้านล่าง:

protected $_logo;   

public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\Theme\Block\Html\Header\Logo $logo,
    array $data = []
)
{       
    $this->_logo = $logo;
    parent::__construct($context, $data);
}

public function isHomePage()
{   
    return $this->_logo->isHomePage();
}

ใช้ Object Manager

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$logo = $objectManager->get('Magento\Theme\Block\Html\Header\Logo');
var_dump($logo->isHomePage());
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.