Slim3 Container good practice?

Simon Müller

Hello im learning PHP and i'am Building a REST API with the Slim3 Framework. I Create Routes Like this:

$container['HomeController'] = function () {
    return new HomeController();
};

$currentContainer = CurrentContainer::getInstance();   
$currentContainer->setContainer($container);

$app->get('/', 'HomeController:index')->setName("index");

My Problem was i had to pass the $container to every Single Controller Class iv'e created, because i need the container context in the Controller for routing etc.

then im build a Singleton Container Class like this:

class CurrentContainer
{

    private static $instance;
    private $container;

    private function __construct()
    {
    }

    private function __clone()
    {
    }

    public static function getInstance()
    {
        if (self::$instance == null) {
        self::$instance = new CurrentContainer();
        }
        return self::$instance;
    }

    public function setContainer($container)
    {
        $this->container = $container;
    }

    /**
     * @return mixed
     */
    public function getContainer()
    {
        return $this->container;
    }
}

so now its possible to create a "MainController" like this:

class Controller
{


    /**
     * @var mixed
     */
    protected $view;

    /**
     * @var
    */
    protected $router;

    public function __construct()
    {
        $container = CurrentContainer::getInstance()->getContainer();
        $this->view = $container->view;
        $this->router = $container->router;
    }


   }

now all of my Controllers extends from the Controller class... my question is now ... its that a good idea or is there a reason to not do it like that? im thankful for every input

tirta keniten

I've built some APIs with Slim Framework, and also tried so many method to get it done (of course in right way). I implemented MVC pattern on Slim Framework. The code example below:

  1. For the controller, I created a base controller that injected with container. So the code:
<?php

namespace App\Controller;

use Slim\Container;

class Controller
{
    protected $container;

    public function __construct(Container $container)
    {
        $this->container = $container;
    }

    public function __get($name)
    {
        return $this->container->get($name);
    }
}
  1. I loaded the base controller on dependencies container.
<?php

// controller
$container['controller'] = function ($c) {
    return new App\Controller\Controller($c);
};
  1. So I can get the container from the controller.
<?php

namespace App\Controller;

use App\Controller\Controller;
use Slim\Http\Request;
use Slim\Http\Response;

class HomeController extends Controller
{
    public function __invoke(Request $request, Response $response, $args)
    {
        return $this->renderer->render($response, 'home');
    }
}

I hope it helps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Inject Provider Service or Container. Good practice

Is it good practice to commit docker container frequently?

Tagged container - is mimicking the container's interface a good practice?

Put a symfony container inside a repository or an entity is a good or bad practice?

Is it a good practice to have an extra docker container for build tasks?

Is it good practice to create separate Docker database container for each user

Is this a good practice?

Is this good practice?

Is it a good practice to *compile* Python3 for Lambda?

When using the angularjs "controllerAs" syntax is it still good practice to have a container object?

Is it good practice if container's size is validated and accessing an element under same conditional statement?

It is a good practice to clone code from gitlab repo and install dependencies in kubernetes Poststart hook of the container?

EntityManager inject good practice

Are convenience functions a good practice?

Python import good practice?

Assert a good practice or not?

Good practice to write to a file

Good practice with Angular Translate

Is that a good practice for working asynchronously?

Is "throws Throwable" good practice

Is this a good coding practice for python?

Good practice with conditional imports

Integration test good practice

Is it good practice to use counters?

Exception Handling - is this good practice?

Is "assert false;" a good practice?

+new Date() - Is this good practice?

Is import static a good practice?

Is this a good practice? "/*/something/*/something//*/"