Framework Docs Controllers

Controllers

Controllers are the glue between routes and views. Every front-end controller extends the abstract Controller base class, which sets up the view engine and provides dependency-injection via the App container.

Namespace: Wojo\Core\Controller

Controller (abstract base)

Method Signature Description
__construct() __construct(?string $viewKey = null, string $viewDir = 'front') Resolves the View instance from the container, optionally loads a view file
setView() setView(string $viewKey, string $dir = 'front'): void Switch to a different view file at runtime

Anatomy of a Controller

PHP
namespace Wojo\Controller\Front;

use Wojo\Core\Controller;
use Wojo\Core\Url;
use Wojo\Database\Database;

class PageController extends Controller
{
    public function __construct(private readonly Database $db)
    {
        parent::__construct('view.front');  // loads view/front/themes/master/
    }

    public function about(): void
    {
        $tpl = $this->view;

        // Assign variables to the template
        $tpl->pageTitle       = 'About Us';
        $tpl->metaDescription = 'Learn more about our company.';

        // Select the template file (view/front/themes/master/about.tpl.php)
        $tpl->template = 'about';
    }
}

Dependency Injection

Constructor parameters are resolved automatically from the App container. Declare type-hinted parameters in controller methods — the router injects them before calling the method.

PHP
// URL segment {id} is injected as int
public function show(int $id): void
{
    $tpl->post      = $this->db->select('posts')->where('id', $id)->first();
    $tpl->template  = 'post-detail';
}

// Multiple injected params
public function edit(int $id, string $slug): void { ... }

SEO Meta Pattern

A private helper method for setting OG/meta tags keeps each public action lean and readable.

PHP
private function setupPageMeta(
    string $title,
    string $description,
    string $keywords,
    string $slug
): void {
    $tpl->pageTitle       = $title;
    $tpl->metaDescription = $description;
    $tpl->metaKeywords    = $keywords;
    $tpl->canonical       = Url::url('/pages/' . $slug);
    $tpl->ogTitle         = $title;
    $tpl->ogDescription   = $description;
}

public function privacy(): void
{
    $this->setupPageMeta(
        title:       'Privacy Policy',
        description: 'How we collect and use your data.',
        keywords:    'privacy, GDPR',
        slug:        'privacy'
    );
    global $tpl;
    $tpl->template = 'privacy';
}