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/car/< carId > )
• Get all prdocuts with attribute carId as List
Thats my current code, im pretty sure im missing somehting on the routes.xml…
etc/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" before="Magento_Backend" />
<route id="car" frontName="car">
<module name="Streetec_ShopByCar" before="Magento_Backend" />
</route>
</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 StreetecShopByCarControllerShopByCar;
use MagentoFrameworkAppActionAction;
use MagentoFrameworkAppActionContext;
use MagentoFrameworkViewResultPageFactory;
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 = MagentoFrameworkAppObjectManager::getInstance();
$productCollection = $objectManager->create('MagentoCatalogModelResourceModelProductCollection');
$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="MagentoCatalogBlockProductListProduct" 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>