Description:
I would like to add MVC (Model-View-Controller) capability to the Pancake toolkit. This will enhance the toolkit's ability to handle both web and API requests, allowing structured routing, view rendering, and controller-based logic. The implementation should include:
- A BaseController with common methods.
- An ApiController that inherits from BaseController, but overrides methods to return only JSON responses.
- Integration with the DIContainer to register controllers and services.
- A Router class to handle route creation and request delegation.
- Support for a template engine, which can be either the default or a Mustache/Twig-like engine for resolving views.
Below are code examples to illustrate the core components.
1. BaseController and ApiController
<?php
namespace GuiBranco\PocMvc\Src\Core;
class BaseController
{
protected $templateEngine;
public function __construct($templateEngine)
{
$this->templateEngine = $templateEngine;
}
public function render($view, $data = [])
{
echo $this->templateEngine->render($view, $data);
}
public function redirect($url)
{
header("Location: $url");
exit();
}
}
class ApiController extends BaseController
{
public function render($view, $data = [])
{
header('Content-Type: application/json');
echo json_encode($data);
exit();
}
}
2. DI Container Registration Example
<?php
$container = new DIContainer();
// Register Template Engine
$container->registerSingleton('templateEngine', function() {
// Use a simple default engine or a Mustache/Twig-like engine
return new Mustache_Engine();
});
// Register BaseController
$container->registerTransient('BaseController', function($container) {
return new \GuiBranco\PocMvc\Src\Core\BaseController($container->resolve('templateEngine'));
});
// Register ApiController
$container->registerTransient('ApiController', function($container) {
return new \GuiBranco\PocMvc\Src\Core\ApiController($container->resolve('templateEngine'));
});
3. Router Class Example
<?php
namespace GuiBranco\PocMvc\Src\Core;
class Router
{
private $routes = [];
public function add($method, $route, $controller, $action)
{
$this->routes[] = ['method' => $method, 'route' => $route, 'controller' => $controller, 'action' => $action];
}
public function dispatch($requestMethod, $requestUri, $container)
{
foreach ($this->routes as $route) {
if ($requestMethod == $route['method'] && $requestUri == $route['route']) {
$controller = $container->resolve($route['controller']);
$action = $route['action'];
return $controller->$action();
}
}
// Default 404 handling
http_response_code(404);
echo "Page not found";
}
}
4. Example of Routing and Handling Requests
<?php
$router = new \GuiBranco\PocMvc\Src\Core\Router();
// Add a web route (renders HTML view)
$router->add('GET', '/home', 'BaseController', 'renderHome');
// Add an API route (returns JSON response)
$router->add('GET', '/api/data', 'ApiController', 'renderData');
// Simulate a request
$router->dispatch($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI'], $container);
Task Requirements:
- Implement BaseController and ApiController.
- Create a Router class for defining routes and dispatching requests.
- Integrate with the DIContainer for controller and service registration.
- Allow for template engine registration with a default engine or a Twig/Mustache-like engine.
Additional Requirements:
- Provide unit tests for controllers, routing, and DI integration.
- Include integration tests to ensure the MVC structure functions as expected.
Acceptance Criteria:
Description:
I would like to add MVC (Model-View-Controller) capability to the Pancake toolkit. This will enhance the toolkit's ability to handle both web and API requests, allowing structured routing, view rendering, and controller-based logic. The implementation should include:
Below are code examples to illustrate the core components.
1. BaseController and ApiController
2. DI Container Registration Example
3. Router Class Example
4. Example of Routing and Handling Requests
Task Requirements:
Additional Requirements:
Acceptance Criteria: