Controllers in DVC
Controllers handle your application logic and connect routes to views. DVC provides a structured way to organize controllers using modules.
Creating a Module
-
Generate module structure:
vendor/bin/dvc make::module example
This creates:
src/ ├── app/ │ └── example/ # Module directory │ └── views/ # Module-specific views └── controller/ └── example.php # Module controller
-
Resulting Controller Structure (
src/controller/example.php
):<?php namespace example; use bravedave\dvc\{ controller as dvcController, ServerRequest }; class controller extends dvcController { protected function _index() { $this->data = (object)[ 'title' => $this->title = config::label, ]; $this->renderBS5([ 'aside' => fn() => $this->load('blank'), 'main' => fn() => $this->load('index') ]); } protected function before() { parent::before(); $this->viewPath[] = __DIR__ . '/views/'; } protected function postHandler() { $request = (new ServerRequest); $action = $request('action'); return match ($action) { default => parent::postHandler() }; } }
Key Features
1. Request Handling
- GET Requests: Automatically routed to
_index()
method - POST Requests: Handled by
postHandler()
2. View Rendering
- Loads view templates from
src/app/example/views/
$this->renderBS5([
'aside' => fn() => $this->load('blank'),
'main' => fn() => $this->load('index')
]);
3. View Structure
Create this file:
<?php
// src/app/example/views/index.php
namespace app\example;
?>
<div class="container">
<h1><?= $title ?></h1>
<p>Welcome to the example module!</p>
</div>
Accessing Your Module
-
Start the development server:
vendor/bin/dvc serve
-
Visit in browser:
http://localhost:8000/example
Advanced Usage
Handling POST Requests
protected function postHandler() {
$request = (new ServerRequest);
return match ($request('action')) {
'save' => $this->saveData($request),
'delete' => $this->deleteRecord($request),
default => parent::postHandler()
};
}
Adding Custom Routes
Extend the _index()
method with additional actions:
public function detail($id) : void {
$this->data = (object)[
'item' => (object)[
'id' => 10,
'description' => 'toy train'
]
];
$this->load('detail');
}
Access via: /example/detail/123