2

How to create a new page in magento 2, i don't want to use block. When i click on "rubrique x" it will lead me to my URL /nameoff my page and not /my page.html

3
  • Can you please explain more with example.It's not clarify your requirement properly Commented Aug 14, 2018 at 9:55
  • Go to Admin -> Content -> Pages and Add new page
    – P_U
    Commented Aug 14, 2018 at 10:17
  • You can create page using custom module. Please see my answer, you can also customize it. Commented Aug 14, 2018 at 10:29

1 Answer 1

1

You can create a page using custom module's controller. Please follow following steps to create a page:

app\code\Namespace\Modulename\registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Namespace_Modulename',
    __DIR__
);

app\code\Namespace\Modulename\etc\module.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Namespace_Modulename" setup_version="1.0.0" schema_version="1.0.0">
        <sequence>
            <module name="Magento_Cms"/>
        </sequence>
    </module>
</config>
  1. Create a page

app\code\Namespace\Modulename\Controller\Test\Index.php

<?php    
namespace Namespace\Modulename\Controller\Test;

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

    public function __construct(
        \Magento\Framework\App\Action\Context $context
    ) {
        parent::__construct($context);
    }

    public function execute() {
        $this->_view->loadLayout();
        $this->_view->getLayout()->initMessages();
        $this->_view->renderLayout();
    }   
}

app\code\Namespace\Modulename\etc\frontend\routes.xml

<?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="testroute" frontName="testroute">
            <module name="Namespace_Modulename" />
        </route>
    </router>
</config>

app\code\Namespace\Module_name\view\frontend\layout\testroute_test_index.xml

<?xml version="1.0"?>

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd" layout="2columns-right">
    <body>
        <referenceContainer name="content">
            <block class="Magento\Framework\View\Element\Template" name="testpage" template="test_page.phtml">
            </block>
        </referenceContainer>
    </body>
</page>

app\code\Namespace\Modulename\view\frontend\templates\test_page.phtml

<?php echo "This is my test page"; ?>
  1. Run following commands one by one in command line:

php bin/magento setup:upgrade

php bin/magento clear:flush

php bin/magento clear:cache

  1. Run following url in browser:

http://your_domain_name/testroute/test/index

Hope this solution may help you.

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.