Simon Müller
Simon Müller

Reputation: 451

Slim3 Container good practice?

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

Upvotes: 0

Views: 1512

Answers (1)

tirta keniten
tirta keniten

Reputation: 427

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.

Upvotes: 2

Related Questions