Skip to content

Route Group

John Aldrich Bernardo edited this page Feb 12, 2018 · 3 revisions

It's really hard to manage a large scale application having to many routes, right? Calf offers a grouping of routes.

<?php

// Include autoload
require('./vendor/autoload.php');

use \Calf\HTTP\Router as Router;
use \Calf\HTTP\Route as Route;
use \Calf\HTTP\RouteGroup as RouteGroup;
use \Calf\HTTP\Request as Request;
use \Calf\HTTP\Response as Response;

// Create a new instance of Calf Router
$router = new Router();

//  Managing a large group of routes with RouteGroup
$productsGroup = new RouteGroup('products');

$productsGroup->add(
    new Route('get', function(Request $req, Response $res, array $args) {
        return $res->set(['Pen', 'Pineapple', 'Apple']);
    })
);

$productsGroup->add(
    new Route('get/fruits', function(Request $req, Response $res, array $args) {
        return $res->set(['Pineapple', 'Apple']);
    })
);

// Add group to router
$router->addGroup($productsGroup);

// Then let the Router do its work.
$response = $router->dispatch();
$response->render();

Clone this wiki locally