5

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.

2
  • 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

12
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;
        parent::__construct($context);
    }

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

        return $resultPage;
    }
}
0

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.