Creating routes.xml
File Location
Create etc/frontend/routes.xml in your module:
app/code/Amazon/Prep/etc/frontend/routes.xml
Complete Configuration
<?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="amazon_prep" frontName="amazon_prep">
<module name="Amazon_Prep"/>
</route>
</router>
</config>
Front Name Rules
- Use lowercase with underscores:
amazon_prep - Should be unique across all modules
- Becomes the first segment of your URLs
- Convention: match the module name in snake_case
URL Structure
With front name amazon_prep:
amazon_prep/index/index → Controller/Index/Index.php
amazon_prep/product/view → Controller/Product/View.php
amazon_prep/warranty/list → Controller/Warranty/List.php
Multiple Routes Per Module
<router id="standard">
<route id="amazon_prep" frontName="amazon_prep">
<module name="Amazon_Prep"/>
</route>
<route id="amazon_prep_api" frontName="amazon_prep_api">
<module name="Amazon_Prep"/>
</route>
</router>
This gives you two URL prefixes:
amazon_prep/*for frontend pagesamazon_prep_api/*for API-style endpoints
Creating Controller Actions
Index Action (Default Page)
<?php
namespace Amazon\Prep\Controller\Index;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\Result\Page;
use Magento\Framework\Result\PageFactory;
class Index implements HttpGetActionInterface
{
public function __construct(
private PageFactory $resultPageFactory
) {}
public function execute(): Page
{
$page = $this->resultPageFactory->create();
$page->getConfig()->getTitle()->set(__('Amazon Prep Dashboard'));
return $page;
}
}
URL: amazon_prep/index/index
View Action
<?php
namespace Amazon\Prep\Controller\Product;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\Result\Page;
use Magento\Framework\Result\PageFactory;
class View implements HttpGetActionInterface
{
public function __construct(
private PageFactory $resultPageFactory
) {}
public function execute(): Page
{
$productId = $this->getRequest()->getParam('id');
$page = $this->resultPageFactory->create();
$page->getConfig()->getTitle()->set(__('Product View'));
// Pass data to block via layout
$page->getLayout()->getBlock('product.view')
->setData('product_id', $productId);
return $page;
}
}
URL: amazon_prep/product/view/id/5
Save Action (POST)
<?php
namespace Amazon\Prep\Controller\Index;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\Controller\Result\RedirectFactory;
use Magento\Framework\Message\ManagerInterface;
class Save implements HttpPostActionInterface
{
public function __construct(
private RedirectFactory $resultRedirectFactory,
private ManagerInterface $messageManager
) {}
public function execute()
{
try {
$name = $this->getRequest()->getParam('name');
// Save logic here
$this->messageManager->addSuccessMessage(__('Saved successfully.'));
} catch (\Exception $e) {
$this->messageManager->errorMessage($e->getMessage());
}
return $this->resultRedirectFactory->create()
->setPath('amazon_prep/index/index');
}
}
URL: amazon_prep/index/save (POST only)
Testing Routes
Browser Testing
- Clear cache first:
bin/magento cache:clean
- Open browser and navigate to:
http://your-magento.com/amazon_prep/index/index
- Check if page loads without 404 error.
CLI Testing with curl
# Test GET request
curl -v http://localhost/amazon_prep/index/index
# Test with parameters
curl -v "http://localhost/amazon_prep/product/view/id/5"
# Test POST request
curl -X POST http://localhost/amazon_prep/index/save \
-d "name=Test+Product"
Debug Route Matching
Enable route logging:
// In app/etc/env.php, "add":
'system' => [
'debug' => [
'log_router_enabled' => true,
],
],
Then check var/log/system.log:
[2024-01-15 10:30:00] main.DEBUG: Matched route "amazon_prep" []
Check Compiled Routes
bin/magento setup:di:compile
grep -r "amazon_prep" var/di/
Common Route Issues
404 Not Found
# Check if module is enabled
bin/magento module:status Amazon_Prep
# Check routes.xml exists
dir app/code/Amazon/Prep/etc/frontend/routes.xml
# Check XML is valid (no syntax errors)
php -l app/code/Amazon/Prep/etc/frontend/routes.xml
# Clear cache
bin/magento cache:clean
Controller Not Found
# Error: Class Amazon\Prep\Controller\Index\Index does not exist
# Check namespace matches directory structure
# File: app/code/Amazon/Prep/Controller/Index/Index.php
# Namespace: Amazon\Prep\Controller\Index
Wrong HTTP Method
# Error: Requested HTTP method is not allowed
# Check action implements correct interface:
# HttpGetActionInterface for GET
# HttpPostActionInterface for POST
Quiz
1. What is the URL for a controller at Controller/Product/View.php with front name 'amazon_prep'?
2. Which interface should a POST-only controller action implement?
3. What command clears route-related cache?
Flashcards
Question
Where does routes.xml go for frontend?
Click to reveal answer
Answer
Vendor/Module/etc/frontend/routes.xml
Question
What is a front name?
Click to reveal answer
Answer
The URL prefix that maps to your module's routes (e.g., amazon_prep)
Question
How do URL segments map to controllers?
Click to reveal answer
Answer
frontName/controller/action → Controller/ControllerName/Action.php
Question
What debug setting enables route logging?
Click to reveal answer
Answer
log_router_enabled = true in app/etc/env.php
Question
How do you test a route?
Click to reveal answer
Answer
Navigate to the URL in browser or use curl -v
Revision Notes
Key Takeaways
- 1. routes.xml goes in etc/frontend/ for frontend routes
- 2. Front name becomes the URL prefix for your module
- 3. URL segments after front name map to Controller directory structure
- 4. Always clear cache after adding or changing routes
- 5. Use route logging to debug routing issues
Interview Tips
- • Explain how URL segments map to controller files
- • Know how to debug a 404 route error
- • Be ready to create a complete route with controller action
- • Understand front name conventions and uniqueness
Cheat Sheet
Route: <route id="amazon_prep" frontName="amazon_prep">
URL: amazon_prep/product/view/id/5
Maps to: Controller/Product/View.php
Debug: log_router_enabled in env.php
Test: curl -v http://site/amazon_prep/index/index
Clear: bin/magento cache:clean