An alternative way of defining request logic in a route is by organizing it by utilizing Controllers. In this article you will learn about basic controllers. As a defined default in Devflow, all custom controllers go into the Cms/Application/Http/Controller directory.

Basic Controller

Here is a simple example of a basic controller with an index method which responds to an incoming request.

<?php

declare(strict_types=1);

namespace Application\Http\Controller;

use Psr\Http\Message\ResponseInterface;
use Qubus\View\Native\Exception\InvalidTemplateNameException;
use Qubus\View\Native\Exception\ViewException;

use function Codefy\Framework\Helpers\view;

final class FrontendController
{
    /**
     * @throws ViewException
     * @throws InvalidTemplateNameException
     */
    public function index(): ResponseInterface
    {
        return view('cmf::index', ['title' => 'Devflow Site']);
    }
}

Now, we can define a route to the controller's method index with a custom route class:

<?php

declare(strict_types=1);

namespace Application\Http\Route;

use Application\Http\Controller\FrontendController;
use Qubus\Routing\Psr7Router;

final class MyFrontendRoute
{
    public function handle(Psr7Router $router): void
    {
        $router->get('/', function(FrontendController $controller) {
            return $controller->index();
        });
    }
}

Note

Check out the routing page on where routes are stored and registered.

When an incoming request matches a route's uri, the index method in the FrontendController will be invoked.

This is just a brief introduction to basic controllers. Check out CodefyPHP's for resource controllers, RESTful controllers, and controller middleware.

Themes

An alternative to creating and storing controllers, routes, and views under the Cms directory, you can instead create themes.