laika-framework

Controllers

Controllers live in lf-app/Controller, namespace App\Controller. A controller is a plain class — no base class to extend.

php laika controller:make UserController --method=index

Basic Controller

namespace App\Controller;

class UserController
{
    public function show($id)
    {
        return "User {$id}";
    }
}

Route parameters and pipeline/filter config args are resolved into method arguments via Laika\Route\Reflection — named-argument style injection, so parameter order in your method signature doesn’t have to match the route definition.

// lf-routes/web.php
Url::get('/users/{id}', 'UserController@show');

// UserController.php — $id is matched by name, not position
public function show($id) { /* ... */ }

Returning a View

Use Laika\Core\App\Template to render a Twig template from template/:

namespace App\Controller;

use Laika\Core\App\Template;

class HomeController
{
    public function index()
    {
        $tpl = new Template();

        $tpl->assign('title', 'Home');
        $tpl->assign('welcome', 'Welcome to Laika PHP MVC Framework!');
        $tpl->assign('provider', ['docurl' => 'https://laikait.com/docs']);

        return $tpl->view('home'); // resolves template/home.twig
    }
}

See Templates for the full templating reference.

Returning Raw Output

A controller can also just return a string (or nothing, if it writes output directly) — the router doesn’t require a Template instance:

public function health()
{
    return json_encode(['status' => 'ok']);
}

Using Models

namespace App\Controller;

use App\Model\UsersModel;

class UserController
{
    public function show($id)
    {
        $users = new UsersModel();
        $user  = $users->where(['id' => $id])->firstOrFail();

        return $user['first_name'];
    }
}

See Models & Database for the query builder.

Using Services

Call into app-level services registered through the service container:

namespace App\Controller;

use Laika\Session\Session;

class DashboardController
{
    public function index()
    {
        $userId = Session::get('user_id');
        // ...
    }
}

Pairing With Pipeline

Pipelines and filters wrap the controller call — see Routing, Pipelines, and Filters.

CLI Reference

Command Description
php laika controller:make <name> [--method=index] Create a controller class
php laika controller:list List registered controller classes
php laika controller:remove <name> Delete a controller class
php laika controller:rename --old=<name> --new=<name> Rename a controller class and its file