An alternative way of defining request logic in a route is by organizing it by utilizing Controllers. In this section
you will learn about basic controllers, resource controllers and restful controllers.
Except for themes and plugins, Devflow controllers should fall under the
Application\Http\Controller namespace in 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 function Codefy\Framework\Helpers\trans;
use function Codefy\Framework\Helpers\view;
final class HomeController
{
public function index(): ResponseInterface
{
return view(
template: 'cmf::home',
data: ['title' => trans('Devflow')]
);
}
}
File: ./Cms/Application/Http/Controller/HomeController.php
Return Response
As much as it is possible, always return a response (highly recommended) in your controllers. In the above
example, the view helper is used which returns an HtmlResponseFactory. For a list of other response
factories, check out Route Response.
Now, we can define a route to the controller's method index. This route example can be defined in
routes/web/web.php
<?php
declare(strict_types=1);
use Application\Http\Controller\HomeController;
return (function(\Qubus\Routing\Psr7Router $router) {
$router->get('/', function(HomeController $controller) {
return $controller->index();
});
});
File: ./routes/web/web.php
This simple example shows you how to create and set up your controllers and views for custom development to extend the functionality of Devflow for you and/or your clients. Please keep in mind:
- Devflow can be used headless
- You can develop regular themes for Devflow with routing
- You can develop page builder themes for Devflow with automatic routing
- You can develop plugins for Devflow with routing