A controller is a simple class responsible for handling HTTP requests and returning responses. Controllers should stay slim: they only coordinate — they call services, prepare data, and render views.
Business logic, data access, and complex processing belong in the model layer.
\Krystal\Controller\AbstractController Controller/ subdirectory inside a module Typical folder structure
module
- Site
- Controller
- Main.php
As a best practice, controllers must be slim. That means, they only have to know how to call methods and pass resulting values to view. They should never process data, and contain complicated logic.
<?php
namespace Site\Controller;
use Krystal\Controller\AbstractController;
class Main extends AbstractController
{
public function indexAction()
{
return 'Welcome to Krystal!';
}
}
Routes are defined in the module's getRoutes() method (in Module.php).
<?php
namespace Site;
use Krystal\Application\Module\AbstractModule;
class Module extends AbstractModule
{
public function getRoutes()
{
return [
'/' => [
'controller' => 'Main@indexAction'
],
'/about' => [
'controller' => 'Main@aboutAction'
]
];
}
}
Format: Module:Controller@Method
Set in config/app.php under the router component:
'router' => [
'default' => 'Site:Main@notFoundAction',
],
When no route matches, Main::notFoundAction() will be called.
Use (:var) to capture URI segments. They are passed as action method arguments in order.
Define
'/user/(:var)' => [
'controller' => 'User@profileAction'
],
Use
public function profileAction($username)
{
// $username contains the value from /user/var
}
Multiple parameters:
Define
'/post/(:var)/comment/(:var)' => 'Post@showCommentAction'
Matches
showCommentAction($postId, $commentId)
Return false from an action to invoke the default (not found) handler:
public function viewAction($id)
{
$post = $this->getModuleService('PostManager')->getById($id);
if (!$post) {
return false; // → triggers 404 (default action)
}
return $this->view->render('post/view', ['post' => $post]);
}
These methods are inherited from AbstractController:
| Method | Description | Example / Usage |
|---|---|---|
redirectToRoute($route) |
Performs HTTP redirect to the specified route | $this->redirectToRoute('Site:Main@dashboard'); |
forward($route, array $args = []) |
Calls another controller's action internally and returns its response | $sidebar = $this->forward('Site:Sidebar@latestNews', ['limit' => 5]); |
getWithAssetPath($path, $module = null) |
Generates full public URL to an asset (CSS/JS/image) in current or specified module | $css = $this->getWithAssetPath('/css/style.css'); |
getWithViewPath($path, $module = null, $theme = null) |
Returns filesystem path to a view file | $path = $this->getWithViewPath('partial/header.phtml', 'Site', 'default'); |