guibranco / pancake

🧰 🛠️ Pancake project - toolkit for PHP projects
https://guibranco.github.io/pancake/
MIT License
3 stars 1 forks source link

[FEATURE] Add MVC Capability to Pancake Toolkit #236

Open guibranco opened 3 days ago

guibranco commented 3 days ago

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:

  1. A BaseController with common methods.
  2. An ApiController that inherits from BaseController, but overrides methods to return only JSON responses.
  3. Integration with the DIContainer to register controllers and services.
  4. A Router class to handle route creation and request delegation.
  5. 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:

Additional Requirements:

Acceptance Criteria:

gitauto-ai[bot] commented 3 days ago

Click the checkbox below to generate a PR!

@guibranco, You have 5 requests left in this cycle which refreshes on 2024-11-21 10:07:38+00:00. If you have any questions or concerns, please contact us at info@gitauto.ai.