Named Routes
Named routes allow you to generate URLs for specific routes without hardcoding paths. This makes your application more maintainable when routes change.
Naming Routes
Section titled “Naming Routes”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');Generating URLs
Section titled “Generating URLs”Use the route() method to generate URLs from route names:
Simple Routes
Section titled “Simple Routes”// Generate: http://example.com/$url = $router->route('home');
// Generate: http://example.com/products$url = $router->route('products.index');Routes with Parameters
Section titled “Routes with Parameters”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]);Real-World Example
Section titled “Real-World Example”<?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'; }}Route Definitions
Section titled “Route Definitions”$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');Using in Views
Section titled “Using in Views”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; ?>Naming Conventions
Section titled “Naming Conventions”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');Benefits
Section titled “Benefits”- Maintainability: Change URLs in one place without updating all references
- Type Safety: Catch missing routes during development
- Readability:
route('products.show')is clearer than/products/123 - Refactoring: Easily reorganize your URL structure
Best Practices
Section titled “Best Practices”- Use dot notation: Organize names hierarchically (e.g.,
admin.users.edit) - Be descriptive: Names should clearly indicate what the route does
- Follow conventions: Use standard CRUD names (index, create, store, show, edit, update, destroy)
- Avoid duplication: Each route should have a unique name