Usando o Silex Bootstrap (http://github.com/lucasmezencio/silex-bootstrap).
config.ini
[production] | |
db.driver = pdo_mysql | |
db.dbname = dumb | |
db.host = localhost | |
db.user = root | |
db.password = root | |
mail.host = localhost | |
mail.port = 123 | |
mail.username = username | |
mail.password = password | |
mail.encryption = ssl | |
mail.auth_mode = login | |
[staging] | |
db.driver = pdo_mysql | |
db.dbname = dumb | |
db.host = localhost | |
db.user = root | |
db.password = root | |
mail.host = localhost | |
mail.port = 123 | |
mail.username = username | |
mail.password = password | |
mail.encryption = ssl | |
mail.auth_mode = login | |
[development] | |
db.driver = pdo_mysql | |
db.dbname = dumb | |
db.host = localhost | |
db.user = root | |
db.password = root | |
mail.host = localhost | |
mail.port = 123 | |
mail.username = username | |
mail.password = password | |
mail.encryption = ssl | |
mail.auth_mode = login |
bootstrap.php
<?php | |
require_once __DIR__.'/../vendor/autoload.php'; | |
use Silex\Provider\TwigServiceProvider; | |
use Silex\Provider\SwiftmailerServiceProvider; | |
$env = getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'; | |
$ini_config = parse_ini_file(__DIR__.'/config.ini', TRUE); | |
$config = $ini_config[$env]; | |
$app = new Silex\Application(); | |
$app->register(new TwigServiceProvider(), array( | |
'twig.path' => __DIR__.'/templates', | |
'twig.options' => array('cache' => __DIR__.'/../cache'), | |
)); | |
$app->register(new SwiftmailerServiceProvider(), array( | |
'swiftmailer.options' => array( | |
'host' => $config['mail.host'] | |
'port' => $config['mail.port'] | |
'username' => $config['mail.username'] | |
'password' => $config['mail.password'] | |
'encryption' => $config['mail.encryption'] | |
'auth_mode' => $config['mail.auth_mode'] | |
) | |
)); | |
return $app; |
app.php
<?php | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpFoundation\Response; | |
$app = require __DIR__.'/bootstrap.php'; | |
$app->match('/contact/', function(Request $request) use ($app) { | |
if ('POST' == $request->getMethod()) { | |
$data = array( | |
'nome' => $request->get('name'), | |
'email' => $request->get('email') | |
); | |
$message = $app['mailer'] | |
->createMessage() | |
->setSubject('Subject') | |
->setFrom(array($request->get('email'))) | |
->setTo(array('email@example.com')) | |
->setBody($app['twig']->render('email.html.twig', $data), 'text/html'); | |
$app['mailer']->send($message); | |
if ($request->isXmlHttpRequest()) { | |
return $app->json(array( | |
'status' => true | |
)); | |
} | |
} | |
return $app['twig']->render('contact.html.twig'); | |
})->bind('contact'); | |
return $app; |
index.php
<?php | |
$app = require __DIR__.'/../src/app.php'; | |
$app->run(); |