Magento2 페이지가 홈페이지 인 경우 템플릿 체크인
-
29-09-2020 - |
문제
요청한 페이지가 홈페이지 인 경우 .phtml 템플리트에서 직접 확인해야합니까?
주어진 컨테이너에 조건부를 추가하고 싶습니다
해결책
어떤 페이지가 템플릿에서 홈페이지인지 확인하는 경우 Magento Templating 및 Layout에 잘못 접근하고 있습니다.
내 접근 방식은 일반적으로 코어 / 템플릿 유형의 블록 을 만들고 레이아웃 을 통해 레이아웃 핸들에 추가하는 것입니다. .
자세한 정보는이 답변에서 찾을 수 있습니다 :
https://magento.stackexchange.com/a/30562/336
이 오해에 대한 더 깊은 설명은 내 블로그에서 찾을 수 있습니다 :
https://blog.philwinkle.com/the- 대부분 오해 된 개념 - in-magento /
Magento 레이아웃으로의 다이빙을 훨씬 더 깊게 읽으십시오. 앨런 폭풍의 주제 :
http://store.pulsestorm.net/products/no-frills. -magento 레이아웃
다른 팁
클래스 생성자에서 \ magento \ framework \ app \ request \ http의 인스턴스를 사용할 수 있습니다.컨트롤러에 있으면 할 필요가 없습니다.
이 $request = $this->getRequest()
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');
. 제휴하지 않습니다 magento.stackexchange