Skip to content

Instantly share code, notes, and snippets.

@tommy-muehle
Created May 19, 2015 11:23
Show Gist options
  • Save tommy-muehle/3a459b68aeb441cf2d7b to your computer and use it in GitHub Desktop.
Save tommy-muehle/3a459b68aeb441cf2d7b to your computer and use it in GitHub Desktop.
silex Controller
<?php
namespace TM\Website\Controller\Abstracts;
use Silex\Application;
/**
* Class BaseController
*
* @package TM\Website\Controller
*/
abstract class BaseController
{
/**
* @param Application $app
* @param string $template
* @param array $variables
*
* @return string
*/
protected function render(Application $app, $template, array $variables = array())
{
/* @var $twig \Twig_Environment */
$twig = $app['twig'];
return $twig->render($template, $variables);
}
/**
* @param Application $app
* @param string $route
* @param array $parameters
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
protected function redirect(Application $app, $route, array $parameters = array())
{
/* @var $urlGenerator \Symfony\Component\Routing\Generator\UrlGenerator */
$urlGenerator = $app['url_generator'];
$url = $urlGenerator->generate($route, $parameters);
return $app->redirect($url);
}
}
<?php
require_once __DIR__ . '/../vendor/autoload.php';
$app = new \Silex\Application();
$app['charset'] = 'utf8';
$app['environment'] = $app->share(function() {
$env = getenv('APP_ENV');
if ($env !== false) {
return $env;
}
if (false === $env && $_SERVER['USER'] == 'web') {
return 'prod';
}
return 'dev';
});
if ($app['environment'] === 'prod') {
$app['debug'] = false;
$app['config'] = array('file' => __DIR__ . '/config/parameters_prod.yml');
} else {
$app['debug'] = true;
$app['config'] = array('file' => __DIR__ . '/config/parameters_dev.yml');
}
// Provider
$app->register(new \TM\Website\Provider\YamlConfigServiceProvider($app['config']['file']));
$app->register(new Silex\Provider\UrlGeneratorServiceProvider());
$app->register(new Silex\Provider\TwigServiceProvider(), array(
'twig.path' => __DIR__ . '/../resources/views',
'twig.options' => array(
'cache' => __DIR__ . '/cache/twig',
'strict_variables' => false,
)
));
$app->register(new Silex\Provider\DoctrineServiceProvider(), array(
'db.options' => array(
'user' => $app['config']['db_user'],
'host' => $app['config']['db_host'],
'dbname' => $app['config']['db_name'],
'password' => $app['config']['db_pass'],
'charset' => 'utf8'
)
));
$app->register(new TM\Provider\DoctrineORMServiceProvider(), array(
'orm.options' => array(
'proxies_dir' => __DIR__ . '/cache/doctrine/proxies',
'entity_dirs' => array(
array('path' => __DIR__ . '/../src/Entity'),
),
'annotations' => array(
__DIR__ . '/../vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php'
)
)
));
$app->register(new \TM\Provider\SitemapServiceProvider());
/* @var $twig \Twig_Environment */
$twig = $app['twig'];
$twig->addFilter('trans*', new Twig_Filter_Function(function($string) {
return $string;
}));
// Controller
$app->mount('/', new \TM\Website\Controller\DefaultController());
$app->mount('/blog', new \TM\Website\Controller\BlogController());
return $app;
<?php
namespace TM\Website\Controller;
use Silex\Application;
use Silex\ControllerCollection;
use Silex\ControllerProviderInterface;
use TM\Website\Controller\Abstracts\BaseController;
/**
* Class DefaultController
*
* @package TM\Website\Controller
*/
class DefaultController extends BaseController implements ControllerProviderInterface
{
/**
* @param Application $app
*
* @return string
*/
public function indexAction(Application $app)
{
return $this->render($app, 'default/index.html.twig');
}
/**
* @param Application $app
*
* @return string
*/
public function imprintAction(Application $app)
{
return $this->render($app, 'default/imprint.html.twig');
}
/**
* @param Application $app
*
* @return string
*/
public function notFoundAction(Application $app)
{
return $this->render($app, 'default/notFound.html.twig');
}
/**
* Returns routes to connect to the given application.
*
* @param Application $app An Application instance
*
* @return ControllerCollection A ControllerCollection instance
*/
public function connect(Application $app)
{
$defaultController = $app['controllers_factory'];
/* @var $defaultController \Silex\ControllerCollection */
$defaultController->match('/', array($this, 'indexAction'))->bind('default.index');
$defaultController->match('/impressum', array($this, 'imprintAction'))->bind('default.imprint');
$defaultController->match('/404', array($this, 'notFoundAction'))->bind('default.notFound');
return $defaultController;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment