I'm trying to make a model in MVC here, using some controllers.
Follow my directories:
- Root /
- app /
- Controller /
- Padrao.php
- Controller /
- public /
- index.php
- composer.json Home
- app /
My file index.php (Yes, to leave td mixed, then q is working split into routes.php, etc):
<?php
require 'vendor/autoload.php';
$app = new \Slim\App([
'settings' => [
'displayErrorDetails' => true
]
]);
$container = $app->getContainer();
$container['view'] = function ($container) {
$view = new \Slim\Views\Twig('app/view/', [
'cache' => false
]);
$basePath = rtrim(str_ireplace('index.php', '', $container['request']->getUri()->getBasePath()), '/');
$view->addExtension(new Slim\Views\TwigExtension($container['router'], $basePath));
return $view;
};
$container[App\Controller\Padrao::class] = function ($c) {
return new App\Controller\Padrao($c->get('view'));
};
/* ============
Rotas da aplicação
============ */
$app->get('/test', App\Controller\Padrao::class);
$app->get('/', function ($req, $res) {
return $this->view->render($res, 'inicio.twig');
});
$app->run();
My Class (Padrao.php):
<?php
namespace App\Controller;
use Slim\Views\Twig;
use Slim\Router;
final class Padrao {
{
private $view;
private $logger;
public function __construct(Twig $view, LoggerInterface $logger)
{
$this->view = $view;
$this->logger = $logger;
}
public function __invoke(Request $request, Response $response, $args)
{
$this->logger->info("Home page action dispatched");
$this->view->render($response, 'home.twig');
return $response;
}
}
My composer.json:
"autoload": {
"psr-4": {
"App\": "app/"
}
}
The problem: When I run, and try to access the url / test, it gives the following error:
Class 'App \ Controller \ Padrao' not found