In magento1 we use this code to set meta description and meta keywords.

$this->getLayout()->getBlock('head')->setDescription('test description');
$this->getLayout()->getBlock('head')->setKeywords('test keywords'); 

I know this will do that in blocks

$this->pageConfig->setDescription('test description');
$this->pageConfig->setKeywords('test keywords'); 

What is controller code replacement in Magento2.

  • Answer Works Fine ?
    – Jackson
    Jan 23, 2017 at 10:00
  • @AnkitShah i am sure of that but didn't test yet. Jan 23, 2017 at 10:03

1 Answer 1

namespace Custom\Module\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Registry;
use Magento\Framework\View\Result\PageFactory;

class Index extends Action {

    protected $resultPageFactory;

    public function __construct(
    Context $context, PageFactory $resultPageFactory) {
        $this->resultPageFactory = $resultPageFactory;

     * @return \Magento\Framework\Controller\Result\Forward|\Magento\Framework\View\Result\Page
    public function execute() {
        $resultPage = $this->resultPageFactory->create();
        $resultPage->getConfig()->setKeywords("Key Words");

        return $resultPage;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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