Skip to content

Named Routes

Named routes allow you to generate URLs for specific routes without hardcoding paths. This makes your application more maintainable when routes change.

Use the name() method to assign a name to a route:

<?php
use Krag\Sroute\Router;
use Controllers\ProductController;
$router = new Router();
$router->get('/', HomeController::class, 'index')
->name('home');
$router->get('/products', ProductController::class, 'index')
->name('products.index');
$router->get('/products/{id}', ProductController::class, 'show')
->name('products.show');

Use the route() method to generate URLs from route names:

// Generate: http://example.com/
$url = $router->route('home');
// Generate: http://example.com/products
$url = $router->route('products.index');

Pass parameters as an associative array:

// Generate: http://example.com/products/123
$url = $router->route('products.show', ['id' => 123]);
// Generate: http://example.com/user/john/posts/456
$url = $router->route('user.posts.show', [
'username' => 'john',
'id' => 456
]);
<?php
namespace Controllers;
class OrderController
{
private $router;
public function __construct($router)
{
$this->router = $router;
}
public function store()
{
// Process order
$orderId = $this->createOrder($_POST);
// Redirect to confirmation page using named route
$confirmUrl = $this->router->route('orders.confirmation', ['id' => $orderId]);
header("Location: {$confirmUrl}");
exit;
}
public function confirmation(string $id)
{
$order = $this->findOrder($id);
require __DIR__ . '/../views/orders/confirmation.php';
}
}
$router->post('/checkout', OrderController::class, 'store')
->middleware(['auth', 'csrf'])
->name('checkout.process');
$router->get('/orders/{id}/confirmation', OrderController::class, 'confirmation')
->middleware(['auth'])
->name('orders.confirmation');

Generate links in your views:

<!-- In your PHP view -->
<nav>
<a href="<?= $router->route('home') ?>">Home</a>
<a href="<?= $router->route('products.index') ?>">Products</a>
<a href="<?= $router->route('about') ?>">About</a>
</nav>
<!-- Product listing -->
<?php foreach ($products as $product): ?>
<a href="<?= $router->route('products.show', ['id' => $product->id]) ?>">
<?= htmlspecialchars($product->name) ?>
</a>
<?php endforeach; ?>

Use consistent naming patterns for better organization:

// Resource routes
$router->get('/products', ProductController::class, 'index')
->name('products.index');
$router->get('/products/create', ProductController::class, 'create')
->name('products.create');
$router->post('/products', ProductController::class, 'store')
->name('products.store');
$router->get('/products/{id}', ProductController::class, 'show')
->name('products.show');
$router->get('/products/{id}/edit', ProductController::class, 'edit')
->name('products.edit');
$router->post('/products/{id}', ProductController::class, 'update')
->name('products.update');
$router->post('/products/{id}/delete', ProductController::class, 'destroy')
->name('products.destroy');
  1. Maintainability: Change URLs in one place without updating all references
  2. Type Safety: Catch missing routes during development
  3. Readability: route('products.show') is clearer than /products/123
  4. Refactoring: Easily reorganize your URL structure
  1. Use dot notation: Organize names hierarchically (e.g., admin.users.edit)
  2. Be descriptive: Names should clearly indicate what the route does
  3. Follow conventions: Use standard CRUD names (index, create, store, show, edit, update, destroy)
  4. Avoid duplication: Each route should have a unique name