1

Im currently working my first small custom module and followed some threads and docs to get to the point where im currently at. Goal of the module is the following:
• Create a custom route ( shopbycar/shopbycar/car )
• Get all prdocuts with attribute carId as List

Thats my current code, im pretty sure im missing somehting on the routes.xml...

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="shopbycar" frontName="shopbycar">
            <module name="Streetec_ShopByCar"/>
        </route>
    </router>
</config>

etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Streetec_ShopByCar" setup_version="1.0.0">
    </module>
</config>

Controller/ShopByCar/Car.php

<?php

namespace Streetec\ShopByCar\Controller\ShopByCar;

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

class Car extends Action
{
    protected $resultPageFactory;

    public function __construct(Context $context, PageFactory $resultPageFactory)
    {
        $this->resultPageFactory = $resultPageFactory;
        parent::__constuct($context);
    }

    public function execute()
    {
        $carId = $this->getRequest()->getParam('car_id');
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
        $productCollection->addAttributeToSelect('*');
        $productCollection->addFieldToFilter('car_id', $carId);
        $resultPage = $this->resultPageFactory->create();
        $resultPage->getConfig()->getTitle->set(__('Products by Car'));
        return $resultPage;
    }
}

view/frontend/layout/shopbycar_car_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-left">
    <head>
        <title>Products by Car</title>
    </head>
    <body>
        <referenceContainer name="content">
            <block class="Magento\Catalog\Block\Product\ListProduct" name="car.products.list" template="Streetec_ShopByCar::product/list.phtml">
                <arguments>
                    <argument name="is_car_page" xsi:type="boolean">true</argument>
                </arguments>
            </block>
        </referenceContainer>
    </body>
</page>
1
  • Based on Magento standards we would require 1 route in each module/extension. Can we have the reason behind creating 2 routes in a single module?
    – Mehul Shah
    Commented May 16, 2023 at 10:36

1 Answer 1

1

Why do you have two routes nested ? Is there a reason ? I think this is what is messing with your code; you should be creating etc/frontend/routes.xml to get a frontend route and you don't have to put a nested car route.

Just create your controller in the following location Vendor/Controller/Car/Index.php

The pattern is the following for a path : routename/controllerFirstFolder/ControllerClassName

So mapping /shopbycar/car?var is actually the same thing as mapping /shopbycar/car/index?var

which is why you need a route id shopbycar a controller namespace with car and a classname with index.

Index class name or package name can be omitted from url

THe url /shopbycar alone would match Vendor\Controller\Index\Index.php

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.