1

I am attempting to load a CMS Page from my controller. The community here was kind enough to help me with a CMS Static Block - and now I need a pointer in the right direction for a cms Page.

What is the proper syntax to load a CMS Page by identifier or block id and what factory supports this type of loading?

2 Answers 2

5

Found the solution. Documenting for posterity.

...
use Magento\Cms\Model\PageFactory;

protected $pageFactory;

public function __construct(...,PageFactory $pageFactory,...) {
...
$this->pageFactory = $pageFactory;
...
}

public function loadCMSPage() {
...
$page = $this->pageFactory->create()->load(<PAGE URL OR ID HERE>);
...
}
5
  • 1
    Please change $pagefactory to $pageFactory in construct argument, it will not work in Linux server. Commented Oct 23, 2018 at 7:53
  • 1
    @Kevin Hill Awesome. How can I page information per store view I've tried $this->_pageFactory->create()->setStoreId(1)->load(51) but it doesn't work? Commented Mar 1, 2019 at 16:38
  • @Juliano Vargas Have you find the solution to load cms page by store id? I am also looking for the same thing.Kindly share your experience
    – aravind
    Commented May 9, 2019 at 11:17
  • @aravind I haven't worked on it yet. But I have something that I would like to try: Which is create a link between the cms pages E.G : I have 3 store view therefore I need to create 3 help.html page one for each store then add new field in the admin cms form to display all the cms pages from the main store that way I can then select help.html in store 2 and 3 so they are linked when I hit the url:../help.html I get id fetch new column that has link_id and load all cms data by that column if that make sense it's just a theory but idea is to link pages with one another. Commented May 9, 2019 at 13:28
  • @Juliano Vargas thanks for your idea.But i have managed to solve the issue by adding filter to a function which extends searchbuilder. It gets me only the stores where the cms page is active
    – aravind
    Commented May 9, 2019 at 13:40
2

There is a special model for this: GetPageByIdentifierInterface
It is available since 2.3

1
  • really good find, thanks!
    – John
    Commented Aug 20, 2021 at 7:59

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.