formations/developpement/symfony3/ressources/s3-micro/web/index.php

88 lines
2.4 KiB
PHP
Raw Normal View History

<?php
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Routing\RouteCollectionBuilder;
use Symfony\Component\HttpFoundation\Response;
// require Composer's autoloader
require __DIR__.'/../vendor/autoload.php';
class AppKernel extends Kernel
{
// Utilisation du trait "MicroKernel"
use MicroKernelTrait;
// Ajout des bundles
public function registerBundles()
{
return array(
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
);
}
// Configuration des bundles - équivalent d'une config.yml
protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
{
// PHP equivalent of config.yml
$c->loadFromExtension('framework', array(
'secret' => 'S0ME_SECRET',
// 'templating' => [
// 'engine' => [
// 'twig'
// ],
// ],
));
// $c->loadFromExtension('twig', [
// 'default_path' => __DIR__.'/../templates',
// ]);
}
// Configuration des routes
protected function configureRoutes(RouteCollectionBuilder $routes)
{
$routes->add('/random/{limit}', 'kernel:randomAction');
$routes->add('/twig', 'kernel:twigAction');
}
public function twigAction()
{
$container = $this->getContainer();
$twig = $container->get('twig');
return new Response($twig->render('index.html.twig'));
}
// Définition d'une route
public function randomAction($limit)
{
return new JsonResponse(array(
'number' => rand(0, $limit)
));
}
// Configuration des répertoires de cache et logs
// Ces répertoires ne devrait pas être contenus dans la racine "web"
public function getCacheDir()
{
return __DIR__.'/../var/cache/'.$this->getEnvironment();
}
public function getLogDir()
{
return __DIR__.'/../var/logs';
}
}
// Définition de l'environnement
$kernel = new AppKernel('dev', true);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);