Routes

Controllers

Controllers are responsible for returning Centum\Interfaces\Http\ResponseInterface objects.

Controllers must implement Centum\Interfaces\Router\ControllerInterface.

A Controller can be as simple as this:

namespace App\Web\Controllers;

use Centum\Http\Response;
use Centum\Interfaces\Http\ResponseInterface;
use Centum\Interfaces\Router\ControllerInterface;

class LoginController implements ControllerInterface
{
    public function form(): ResponseInterface
    {
        return new Response("this is the login page");
    }
}

Controllers can also take advantage of things like dependency injection, filters, middlewares, and form validation which we’ll learn more about later.

Route Groups

In order to simplify the Route and Router classes, Routes are stored in Group objects.

Centum\Router\Group implements Centum\Interfaces\Router\GroupInterface.

A group can store as many routes as you wish and can be used to organise and group similar routes. A new group can be created with the group() method:

use Centum\Interfaces\Router\RouterInterface;

/** @var RouterInterface $router */

$group = $router->group();

We’ll learn more about Route Groups and how they can be useful in the Middlewares section.

HTTP Methods

When adding a route to the Router, you must specify which HTTP method it matches. RFC 7231 and RFC 5789 specify the following HTTP methods which correlate with a Group method:

HTTP Method Group Method
GET $group->get($uri, $class, $method)
POST $group->post($uri, $class, $method)
HEAD $group->head($uri, $class, $method)
PUT $group->put($uri, $class, $method)
DELETE $group->delete($uri, $class, $method)
TRACE $group->trace($uri, $class, $method)
OPTIONS $group->options($uri, $class, $method)
CONNECT $group->connect($uri, $class, $method)
PATCH $group->patch($uri, $class, $method)

If we wanted to add the login form from the LoginController in the earlier example, we might decide that it should be a GET request and match the /login URL:

use App\Web\Controllers\LoginController;
use Centum\Interfaces\Router\RouterInterface;

/** @var RouterInterface $router */

$group = $router->group();

$group->get("/login", LoginController::class, "form");

Same URL, Different Methods

It’s likely that at some point you’ll want to use the same URL for different things. For example, you might want to show users a login form at /login but also allow the form to submit the login data to /login:

namespace App\Web\Controllers;

use Centum\Http\Response;
use Centum\Interfaces\Http\ResponseInterface;
use Centum\Interfaces\Router\ControllerInterface;

class LoginController implements ControllerInterface
{
    public function form(): ResponseInterface
    {
        return new Response("hello GET");
    }

    public function submit(): ResponseInterface
    {
        return new Response("hello POST");
    }
}

When adding these routes to the Router, you can use the different Group methods to denote which HTTP method they will apply to:

use App\Web\Controllers\LoginController;

$group = $router->group();

$group->get("/login", LoginController::class, "form");
$group->post("/login", LoginController::class, "submit");

GET /login would match form() and POST /login would match submit().

A shorthand exists for this kind of use case which uses the naming convention of form() and submit() inside the Controller:

use App\Web\Controllers\LoginController;

$group = $router->group();

$group->submission("/login", LoginController::class);

Precedence

The Router processes Routes in the order they are added. In this example, GET / would match AController:

$group = $router->group();

$group->get("/", AController::class, "index");
$group->get("/", BController::class, "index");