-
-
Save umpirsky/3ab3f4535b9e7dd98457 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include __DIR__ . '/../vendor/silex/autoload.php'; | |
use Symfony\Component\HttpFoundation\Response; | |
use Symfony\Component\HttpKernel\Exception\HttpException; | |
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | |
$app = new Silex\Application(); | |
// Register extensions | |
$app->register(new Silex\Provider\TwigServiceProvider(), array( | |
'twig.path' => __DIR__ . '/../views', | |
'twig.class_path' => __DIR__ . '/../vendor/twig/lib', | |
)); | |
$app->register(new Silex\Provider\UrlGeneratorServiceProvider()); | |
$app->register(new Silex\Provider\FormServiceProvider(), array( | |
'form.class_path' => __DIR__ . '/../vendor/symfony/src' | |
)); | |
$app->register(new Silex\Provider\TranslationServiceProvider(), array( | |
'locale_fallback' => 'en', | |
'translation.class_path' => __DIR__ . '/../vendor/symfony/src', | |
'translator.messages' => array() | |
)); | |
$app->register(new Silex\Provider\SymfonyBridgesServiceProvider(), array( | |
'symfony_bridges.class_path' => __DIR__ . '/../vendor/symfony/src' | |
)); | |
$app['autoloader']->registerNamespace('Knp\Menu', __DIR__ . '/../vendor/KnpMenu/src'); | |
$app['knp_menu.default_renderer'] = 'twig'; | |
$app['knp_menu.menus'] = array('main' => 'main_menu'); | |
$app->register(new \Knp\Menu\Silex\KnpMenuServiceProvider()); | |
// Add layout | |
$app->before(function () use ($app) { | |
$app['twig']->addGlobal('layout', $app['twig']->loadTemplate('layout.twig')); | |
}); | |
// Register error handlers | |
$app->error( | |
function (\Exception $e) use ($app) { | |
if ($e instanceof NotFoundHttpException) { | |
return $app['twig']->render('error.twig', array('code' => 404)); | |
} | |
$code = ($e instanceof HttpException) ? $e->getStatusCode() : 500; | |
return $app['twig']->render('error.twig', array('code' => $code)); | |
} | |
); | |
// Add pages | |
$pages = array( | |
'/' => 'home', | |
'/jezici' => 'languages', | |
'/nemacki' => 'german', | |
'/engleski' => 'english', | |
'/o-name' => 'about', | |
'/slike' => 'pictures', | |
); | |
foreach ($pages as $route => $view) { | |
$app->get($route, function () use ($app, $view) { | |
return $app['twig']->render($view . '.twig'); | |
})->bind($view); | |
} | |
$app->match('/kontakt/{sent}', function ($sent) use ($app, $view) { | |
$form = $app['form.factory'] | |
->createBuilder('form') | |
->add('name', 'text', array('label' => 'Ime:')) | |
->add('email', 'email', array('label' => 'E-pošta:')) | |
->add('message', 'textarea', array('label' => 'Poruka:')) | |
->getForm(); | |
if ('POST' == $app['request']->getMethod()) { | |
$form->bindRequest($app['request']); | |
if ($form->isValid()) { | |
$data = $form->getData(); | |
require_once __DIR__ . '/../vendor/swiftmailer/lib/swift_required.php'; | |
\Swift_Mailer::newInstance(\Swift_MailTransport::newInstance()) | |
->send(\Swift_Message::newInstance() | |
->setSubject(sprintf('Kontakt sa %s', $_SERVER['SERVER_NAME'])) | |
->setFrom(array($data['email'])) | |
->setTo(array('umpirsky@gmail.com')) | |
->setBody($data['message']) | |
); | |
return $app->redirect($app['url_generator']->generate( | |
'contact', | |
array('sent' => true) | |
)); | |
} | |
} | |
return $app['twig']->render('contact.twig', array( | |
'form' => $form->createView(), | |
'sent' => $sent | |
)); | |
}) | |
->convert('sent', function ($sent) { return (bool) $sent; }) | |
->value('sent', false) | |
->bind('contact'); | |
// Menu | |
$app['main_menu'] = function($app) { | |
$menu = $app['knp_menu.factory']->createItem('root'); | |
$menu->setCurrentUri($app['request']->getRequestUri()); | |
$menu->addChild('Početna', array('route' => 'home')); | |
$languages = $menu->addChild('Jezici', array('route' => 'languages')); | |
$languages->addChild('Nemački', array('route' => 'german')); | |
$languages->addChild('Engleski', array('route' => 'english')); | |
$menu->addChild('O nama', array('route' => 'about')); | |
$menu->addChild('Slike', array('route' => 'pictures')); | |
$menu->addChild('Kontakt', array('route' => 'contact')); | |
return $menu; | |
}; | |
// Run | |
$app->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment