Controllers are the entry point for each page request after routing. An action method
is called on the matched controller, and its return value — a ViewModel
or void — is passed to the view layer for rendering.
Controllers extending AbstractController have access to the following
properties, wired automatically from the application:
| Property | Type | Description |
|---|---|---|
$this->view |
ViewInterface |
The application's view variable bag |
$this->http |
HttpInterface |
The application's HTTP container |
$this->request |
RequestInterface |
The current HTTP request (from $this->http->request) |
$this->redirect |
RedirectInterface |
The route redirects helper for Location headers |
Here is the default IndexController that ships with the skeleton.
It extends AbstractController and returns a ViewModel
from its indexAction():
declare(strict_types=1);
namespace Application\Controller;
use JiNexus\Mvc\Controller\AbstractController;
use JiNexus\Mvc\Model\ViewModel;
class IndexController extends AbstractController
{
public function indexAction(): ViewModel
{
return new ViewModel([
'helloWorld' => 'Hello World!',
]);
}
}
Action methods must be suffixed with Action. The MVC pipeline strips
the suffix to determine the action name exposed to the view. For example:
indexAction() → action name indexviewAction() → action name vieweditAction() → action name edit
The matched action name is available on the application as
$app->actionName (a read-only PHP 8.5 property hook).
An action returns a ViewModel to pass variables to the view template.
The constructor accepts an associative array; each key becomes a variable in the
rendered PHTML template:
public function indexAction(): ViewModel
{
return new ViewModel([
'pageTitle' => 'Welcome',
'items' => ['one', 'two', 'three'],
]);
}
If an action returns void (no explicit return), the view is still
rendered with whatever variables were set directly on $this->view.
Use the $this->redirect helper to send a Location header
to a named route. The request is terminated after the header is sent:
public function dashboardAction(): void
{
// 302 redirect to the "home" route
$this->redirect->toRoute('home');
// 301 (permanent) redirect
$this->redirect->toRoute('home', true);
}
If you found a typo or error, please help us improve this document. Create Issue