phpslim

Slim php container setup


I am fairly new to slim php, i want to learn about containers but with not much luck, i tried so hard to get it done but still i get nothing.

--api
   --src
     --Controllers
       --HomeController.php
     --public
       --.htaccess
       --index.php
  .htaccess
   

index.php

use DI\Container;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
use api\Controllers\HomeController;
use Psr\Container\ContainerInterface;

require __DIR__ . '/../vendor/autoload.php';

$container = new Container();

AppFactory::setContainer($container);
$app = AppFactory::create();
$app->setBasePath('/api');

$container->set('HomeController', function (ContainerInterface $container) {
    $view = $container->get('view');
    
    return new HomeController($view);
});


$app->get('/', \HomeController::class . ':home');

$app->run();

HomeController.php


namespace api\Controllers;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;


class HomeController
{
   private $container;

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

   public function home(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
   {
        $response->getBody()->write('hello');
        return $response;
   }

}

i tried every youtube video tutorial, read every documentation. with no understanding what so ever, i just need a basic container to work so i can understand it in person and tweak with it, thank you in advance for your patience.


Solution

  • In order to get things going, I suggest you these steps:

    1. Create a settings.php file at the root of your project. Something like:
    <?php
    
    return [
        'settings' => [
            'slim' => [
                'displayErrorDetails' => true,
                'logErrors' => true,
                'logErrorDetails' => true,
            ],
        ],
        // The rest of your container settings go here
    ];
    
    
    1. Create a bootstrap.php file at the root of your project that contains the app container and you can configure that here. An example would be something like:
    require_once __DIR__ . '/vendor/autoload.php';
    
    $container = new Container(require __DIR__ . '/settings.php');
    

    And you can add more singletons to your container:

    $container->set(DefaultController::class, function (Container $c) {
        return new DefaultController($c->get(EntityManager::class));
    });
    

    So that you can use your EntityManager in your controllers. Remember that you have to set that and add it to your container as well. With whatever configurations that you want.

    1. Now you can create an index.php file in the public folder, and create your app:
    require __DIR__ . '/../bootstrap.php';
    
    AppFactory::setContainer($container);
    
    $app = AppFactory::create();
    
    1. Note that you have to configure your namespaced autoload in your composer.json like this:
        ...
        "autoload": {
            "psr-4": {
                "Slim\\": "Slim",
                "App\\": "src/",
                "Test\\": "tests/"
            }
        },
        ...
    
    1. Create your controllers and be careful with the namespaces that you use. They are a little tricky.