Skip to content

Views and Responses

Precious Lyson edited this page Nov 11, 2026 · 1 revision

Views and Responses

Machinjiri includes a view renderer and HTTP request/response abstractions. The application skeleton determines where views are stored and how the renderer is configured.

Render a view from a controller

<?php

namespace App\Controllers;

use Mlangeni\Machinjiri\Core\Artisans\Base\AbstractController;
use Mlangeni\Machinjiri\Core\Http\HttpRequest;
use Mlangeni\Machinjiri\Core\Http\HttpResponse;

class HomeController extends AbstractController
{
    public function index(HttpRequest $request, HttpResponse $response): string
    {
        return $this->view('home', [
            'title' => 'Welcome',
            'message' => 'A view can receive application data.',
        ]);
    }
}

The base controller's view() method returns rendered HTML as a string. In the starter app described in the README, the view is resources/views/home.view.php; view lookup may be configured differently in other generated applications.

Escape output by default

In a Machinjiri view, use {{ ... }} to print an HTML-escaped value:

<h1>{{ $title }}</h1>
<p>{{ $message }}</p>

Keep untrusted data escaped. Do not concatenate request input into raw HTML or use an unescaped-output mechanism unless the content has been safely sanitized for its exact output context.

Return JSON

Controllers extending AbstractController can use its json() helper:

public function show(HttpRequest $request, HttpResponse $response): HttpResponse
{
    return $this->json([
        'status' => 'ok',
        'data' => ['name' => 'Example'],
    ]);
}

Pass an HTTP status code when appropriate:

return $this->json(['error' => 'Not found'], 404);

The helper constructs an HttpResponse, sets the status, and sets a JSON body. Use the response APIs and conventions in your generated application for headers and response sending.

Redirects

The base controller also provides a redirect helper:

$this->redirect('/login');

It sends a redirect response (302 by default). For other status codes, the helper accepts a second argument. Validate and constrain redirect destinations when they can be influenced by user input to prevent open redirects.

Layouts and partials

The framework package includes view components and CLI commands for creating views, layouts, and partials (view:make, view:make-layout, and view:make-partial). Exact directive syntax and file placement depend on the view configuration generated for your app; inspect its sample views and renderer configuration before adopting a template convention.

Forms

Machinjiri includes form request, validation, error-bag, file-upload, and CSRF-token components. A controller can validate incoming fields using the validation helpers exposed by AbstractController; rules supported by your installed version should be checked in its FormValidator and RuleBuilder APIs. Always validate on the server even when forms also use browser-side validation.

Choosing a response type

Endpoint Typical result
HTML page Rendered view string
JSON API HttpResponse with JSON body and suitable status
Redirect after form submit Redirect response
Invalid input Validation errors or a 4xx response

Keep presentation out of data-access code and keep the response format consistent for each endpoint.

More

Clone this wiki locally