0

I'm trying to create a simple hello world page in the Customer Account Dashboard section of Magento 2.

I have a file structure that looks like this:

|-Vendor
|----Garage
|--------Block
|------------Customer
|----------------Link.php
|--------Controller
|------------Customer
|----------------Index.php
|--------etc
|------------frontend
|----------------routes.xml
|------------module.xml
|--------registration.php
|--------view
|------------frontend
|----------------layout
|--------------------customer_account.xml
|--------------------garage_customer_index.xml
|----------------templates
|--------------------garage.phtml

And my (important) files look this:

customer_account.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">
    <body>
        <referenceBlock name="customer_account_navigation">
            <block class="Vendor\Garage\Block\Customer\Link" name="my-garage">
                <arguments>
                    <argument name="path" xsi:type="string">garage/customer/index</argument>
                    <argument name="label" xsi:type="string">My Garage</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

garage_customer_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">
<update handle="customer_account" />
<body>
    <referenceBlock name="page.main.title">
        <action method="setPageTitle">
            <argument translate="true" name="title" xsi:type="string">My Garage</argument>
        </action>
    </referenceBlock>
    <referenceContainer name="content">
        <block class="Magento\Framework\View\Element\Template" name="garage" template="Vendor_Garage::garage.phtml">
        </block>
    </referenceContainer>
</body>
</page>

Index.php

<?php
    namespace Vendor\Garage\Controller\Index;

    use Magento\Framework\App\Action\Context;
    use Magento\Framework\View\Result\PageFactory;

    class Index extends \Magento\Framework\App\Action\Action
    {
        protected $resultPageFactory;
        protected $session;

        public function __construct(
            \Magento\Framework\App\Action\Context $context,
            \Magento\Customer\Model\Session $customerSession,
            PageFactory $resultPageFactory
        )
        {
            $this->session = $customerSession;
            parent::__construct($context);
            $this->resultPageFactory = $resultPageFactory;
        }

        public function execute()
        {
            if (!$this->session->isLoggedIn()) {
                $resultRedirect = $this->resultRedirectFactory->create();
                $resultRedirect->setPath('customer/account/login');

                return $resultRedirect;
            } else {
                $resultPage = $this->resultPageFactory->create();
                $resultPage->getConfig()->getTitle()->set(__('My Garage'));

                return $resultPage;
            }
        }
    }

I've then proceeded to run:

$ php bin/magento setup:upgrade
$ php bin/magento setup:di:compile
$ php bin/magento setup:static-content:deploy -f # just for good measure
$ php bin/magento cache:flush

I can see my link in the Customer Dashboard section but clicking the link takes me to a 404 page. What am I doing wrong?

P.s if you need to see more file code - lemme know and I'll update question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.