This extension contains a new page that can be accessed at the following url:
<magento2-url>/newpage
To demonstrate how to create a custom webpage in Magento.
View-related files in Magento are based on "application area." For instance, if the user browsing the website is in the "frontend" area - such as a customer purchasing a product - pages in the admin area cannot be loaded. The view folder contains subdirectories named by application area, which contain files only relevant to that area. This extension only deals places content on the frontend, so it only has a frontend directory.
The route config file maps the frontname "newpage" to this extension, so the application looks in the extension to find the layout file describing the page.
There are three routing parameters used to determine which layout file must be loaded.
The general form of the url is <magento2-url>/<extension>/<action path>/<action name>/
.
Because the page is accessed at <magento2-url>/newpage
, the routing parameters are:
- extension: "newpage"
- action path: "index"
- action name: "index"
The index values are provided as defaults when none are explicitly provided.
The application thus looks in <extension-root>/view/frontend/layout
for a file called newpage_index_index.xml
.
The layout file defines two things: a title (in the head/title element) and a block class to fill the page with content.
There can be custom or specialized block classes for sophisticated extensions, but here we use the most basic block class, Magento\Framework\View\Element\Template, to handle plain html content.
The block's template defines exactly what that content will be. The layout file defines which module contains the
template, and what its name is. Based on that, the application will look in <extension-root>/view/frontend/templates
for main.phtml
. The block uses its toHtml()
method to process the template file and generate output html.
This module is intended to be installed using composer.
After the code is marshalled by composer, enable the module by adding it the list of enabled modules in the config or, if that file does not exist, installing Magento.
After including this component and enabling it, you can verify it is installed by going the backend at:
STORES -> Configuration -> ADVANCED/Advanced -> Disable Modules Output
Once there check that the module name shows up in the list to confirm that it was installed correctly.
Unit tests are found in the Test/Unit directory.
Magento Core team