Initial PHP slim project structure

This commit is contained in:
2022-11-19 13:51:05 -05:00
parent 63dcfcdde5
commit 4a44aef764
13 changed files with 1546 additions and 8 deletions

View File

@ -0,0 +1,19 @@
<?php
namespace BitGoblin\Goliath\Controllers;
use Psr\Container\ContainerInterface;
class Controller {
protected $container;
public function __construct(ContainerInterface $container) {
$this->container = $container;
}
public function get(string $name) {
return $this->container->get($name);
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace BitGoblin\Goliath\Controllers;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Views\Twig;
class HomeController extends Controller {
public function getIndex(Request $request, Response $response): Response {
$view = Twig::fromRequest($request);
return $view->render($response, 'index.twig');
}
}

33
src/app.php Normal file
View File

@ -0,0 +1,33 @@
<?php
use DI\Container;
use Slim\Factory\AppFactory;
use Slim\Views\Twig;
use Slim\Views\TwigMiddleware;
require __DIR__ . '/../vendor/autoload.php';
// Load app configuration
//$config = Config::load(__DIR__ . '/../conf/defaults.json');
// Create new container object and add our config object to it
$container = new Container();
//$container->set('config', function () use ($config) {
// return $config;
//});
// Set container to create App with on AppFactory
AppFactory::setContainer($container);
$app = AppFactory::create();
// Add Error Handling Middleware
$app->addErrorMiddleware(true, false, false);
// Create Twig
$twig = Twig::create(__DIR__ . '/../views', ['cache' => false]);
// Add Twig-View Middleware
$app->add(TwigMiddleware::create($app, $twig));
// load in route handlers
require_once __DIR__ . '/routes.php';

8
src/routes.php Normal file
View File

@ -0,0 +1,8 @@
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Views\Twig;
// index GET route - this page should welcome the user and direct them to the available actions
$app->get('/', '\\BitGoblin\\Goliath\\Controllers\\HomeController:getIndex')->setName('index');