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>