Repository navigation
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.
<?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.
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.
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.
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.
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.
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.
| 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.
Machinjiri Docs
- Home
- Learn
- Build
- Reference
-
Tutorials
- Routing
- Controllers and Middleware
- Views and Templates
- Database ORM
- Eloquent Database ORM
- Errors and Exceptions
- Forms and Validation
- Http
- Events and Logging
- Services and DI
- Security
- Authentication
- Caching
- File System
- Date and Time
- Debugging
- Queue and Jobs
- Task Scheduling
- Transport (Mail, SMS)
- LDAP
- Notifications
- Webhooks
- UUID Generations
- Terminal
- Helper Function