0

How we can call the CMS page using controller. Is it possible to call the CMS page from controller. If it is possible how we can call the CMS page using controller.

Thanks.

3 Answers 3

0

There is already an answer for this, Refer to link Link to Click. But I think you want to create a controller a load page using controller route id. So you have to do something like which I did below-

Create controller like-

<?php
/**
 * Copyright ©  All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);

namespace Gag\Front\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{

    protected $resultPageFactory;

    /**
     * Constructor
     *
     * @param \Magento\Framework\App\Action\Context  $context
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    ) {
        $this->resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }

    /**
     * Execute view action
     *
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {
        return $this->resultPageFactory->create();
    }
}

Create a routes.xml like-

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="frontcms" frontName="frontcms">
            <module name="Gag_Front"/>
        </route>
    </router>
</config>

Create a block class which will be passed in .phtml file like-

<?php
/**
 * Copyright ©  All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);

namespace Gag\Front\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{

    protected $resultPageFactory;

    /**
     * Constructor
     *
     * @param \Magento\Framework\App\Action\Context  $context
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    ) {
        $this->resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }

    /**
     * Execute view action
     *
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {
        return $this->resultPageFactory->create();
    }
}

In your .phtml file load the CMS page like-

<?php 

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$page = $objectManager->get('\Magento\Cms\Model\Page')->load('enable-cookies');
echo $objectManager->get('\Magento\Cms\Model\Template\FilterProvider')->getBlockFilter()->filter($page->getContent());

NOTE:- I have called object manager in .phtml file which is not right. You should use Factories or some better ways

3
  • Hi @Gagan thanks for your responce. i didn't get about block class
    – Mani
    Jun 3 at 12:45
  • I have updated the answer. Please see through the link, may be that can help but if you want using route id then you have to create module.
    – Gagan
    Jun 3 at 12:46
  • 1
    Thank you @Gagan
    – Mani
    Jun 3 at 12:58
0

Simple way is use PageHelper

<?php
declare(strict_types=1);

namespace Acme\StackExchange\Controller\Result;

use Magento\Cms\Helper\Page as PageHelper;
use Magento\Cms\Model\Page as PageModel;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\Controller\ResultInterface;

class Page implements HttpGetActionInterface
{
    /**
     * @var PageModel
     */
    protected $page;

    /**
     * @var PageHelper
     */
    protected $pageHelper;

    public function __construct(
        PageModel $page,
        PageHelper $pageHelper
    ) {
        $this->page       = $page;
        $this->pageHelper = $pageHelper;
    }

    /**
     * @return ResultInterface
     */
    public function execute()
    {
        return $this->pageHelper->prepareResultPage($this, 'enable-cookies');
    }
}
0
Add below code in your controller : 

$this->loadLayout();    
$block = $this->getLayout()->createBlock('block_type','name',array('template' => 'phtml_name'));
$this->getLayout()->getBlock('content')->append($block);
$this->renderLayout();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.