Hi I am new to Slim 4.
I am doing a course through Udemy on Slim 3 framework which has the below code in public/index.php I am given an upgrade path to Slim 4 in the course but it does not work.
My question: I see that Slim 4 framework has hardly any setup code in public/index.php.
How would I convert the below from my course to work in Slim 4 framework.
Note: I have read and understood the comment made in the Slim 4 framework about ActionControllers and the fact that they do 1 thing and do it well. I understand that the below example from the Udemy course I took is more focused on Controllers (MVC). I am still trying to wrap my head around the fact that an ActionController can and should only do 1 thing. I.e. what about all the functions that we normally create in an MVC Laravel Controller to do Crud?
Essentially I need to fire up the following in a Controller and SecondController and print a view.
BUT I need to use the DI container in Slim 4 framework container.php to do the work that was being done in index.php below. I.e. create the container and app etc.
<?php
namespace App\Controller;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Container\ContainerInterface;
abstract class Controller
{
protected $ci;
public function __construct(ContainerInterface $ci)
{
$this->ci = $ci;
}
protected function render(Response $response, $template, $data = [])
{
$html = $this->ci->get('templating')->render($template, $data);
$response->getBody()->write($html);
return $response;
}
}
which is then extended in SecondController.php per below.
<?php
namespace App\Controller;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
class SecondController extends Controller
{
public function hello(Request $request, Response $response)
{
return $this->render($response, 'second.twig', ['name' => 'Chris']);
}
}
I have tried to set up the code below in the config area of the Slim 4 framework but am not having much luck.
I get this error:
Error 500 (Internal Server Error)
Message: No entry or class found for ‘templating’
File: C:\xampp\htdocs\slim4odanpfour\vendor\php-di\php-di\src\Container.php, Line: 133
Below is the index.php code that needs to be simplified / logic that needs to be moved out of index.php. It is this that is stumping me. Hope you can help me.
<?php
use Slim\Factory\AppFactory;
use DI\Container;
require __DIR__ . '/../vendor/autoload.php';
$container = new Container();
$container->set('templating', function () {
return new Mustache_Engine([
'loader' => new Mustache_Loader_FilesystemLoader(
__DIR__ . '/../templates',
['extension' => '']
)
]);
});
AppFactory::setContainer($container);
$app = AppFactory::create();
$app->get('/', '\App\Controller\FirstController:homepage');
$app->get('/hello', '\App\Controller\SecondController:hello');
$app->run();