Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Add MVC Capability to Pancake Toolkit #236

Open
4 tasks
guibranco opened this issue Oct 21, 2024 · 2 comments · May be fixed by #252
Open
4 tasks

[FEATURE] Add MVC Capability to Pancake Toolkit #236

guibranco opened this issue Oct 21, 2024 · 2 comments · May be fixed by #252
Labels
♻️ code quality Code quality-related tasks or issues 📝 documentation Tasks related to writing or updating documentation enhancement New feature or request gitauto GitAuto label to trigger the app in a issue. good first issue Good for newcomers hacktoberfest Participation in the Hacktoberfest event help wanted Extra attention is needed 🧪 tests Tasks related to testing 🕕 very high effort A task that can be completed in a few weeks 🛠 WIP Work in progress

Comments

@guibranco
Copy link
Owner

guibranco commented Oct 21, 2024

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:

  • 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:

  • The MVC components are implemented and registered in the DI container.
  • Routing, controller rendering, and template engine integration work as expected.
  • Unit and integration tests are provided.
  • Documentation is updated with usage examples.
@guibranco guibranco added 📝 documentation Tasks related to writing or updating documentation enhancement New feature or request help wanted Extra attention is needed good first issue Good for newcomers hacktoberfest Participation in the Hacktoberfest event 🧪 tests Tasks related to testing ♻️ code quality Code quality-related tasks or issues 🕕 very high effort A task that can be completed in a few weeks labels Oct 21, 2024
Copy link
Contributor

gitauto-ai bot commented Oct 21, 2024

Click the checkbox below to generate a PR!

  • Generate 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 [email protected].

@gitauto-ai gitauto-ai bot added the gitauto GitAuto label to trigger the app in a issue. label Oct 25, 2024
Copy link
Contributor

gitauto-ai bot commented Oct 25, 2024

Hey, I'm a bit lost here! Not sure which file I should be fixing. Could you give me a bit more to go on? Maybe add some details to the issue or drop a comment with some extra hints? Thanks!

Have feedback or need help?
Feel free to email [email protected].

@gitauto-ai gitauto-ai bot linked a pull request Oct 25, 2024 that will close this issue
@gstraccini gstraccini bot added the 🛠 WIP Work in progress label Oct 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
♻️ code quality Code quality-related tasks or issues 📝 documentation Tasks related to writing or updating documentation enhancement New feature or request gitauto GitAuto label to trigger the app in a issue. good first issue Good for newcomers hacktoberfest Participation in the Hacktoberfest event help wanted Extra attention is needed 🧪 tests Tasks related to testing 🕕 very high effort A task that can be completed in a few weeks 🛠 WIP Work in progress
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant