Brian Graham
Brian is a Sr. Software Developer. He's spoken at TrueNorthPHP, FullStackTO, GTAPHP, and Coder Camp Hamilton.
Out-of-the-box, silex provides everything you need to start HTTP.
<?php // web/index.php
// Dependency Namespaces are loaded via
// Composer according to PSR
require_once __DIR__.'/../vendor/autoload.php';
// Gets an instance of a Silex web app
$app = new Silex\Application();
// TODO: Add all the application logic here.
// Handles the request
// All equest object configuration can be set
// up here (eg, trusted proxies, hosts, formats, etc...)
$app->run();
Out-of-the-box, silex provides everything you need to start HTTP.
<?php // web/index.php
require_once __DIR__.'/../vendor/autoload.php';
$app = new Silex\Application();
// Register a route to your app to
// execute a closure as the controller.
$app->get('/silex-is-cool', function () {
// FIXME: A template in my controller!
// We'll fix this in a few slides.
$output = 'Thanks for visiting!';
return $output;
});
$app->run();
<?php // web/index.php
require_once __DIR__.'/../vendor/autoload.php';
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
$app = new Silex\Application();
// Register a route to your app to
// execute a closure as the controller.
$app->get('/silex-is-{adjective}',
function (Silex\Application $app, $adjective) {
if ($adjective === "cilex") {
$app->abort(400, "Client error.");
}
$output = sprintf(
'Thanks for visiting! You think Silex is %s!',
$adjective
);
return Response($output);
}
);
$app->POST('/some-endpoint', function (Request $request) {
return Response($request->get('someData'));
});
// see also: $app->{POST|PUT|GET|PATCH|MATCH}
$app->run();
<?php // web/index.php
require_once __DIR__.'/../vendor/autoload.php';
$nameFudger= function ($name) {
return str_rot13($name);
};
$app->get('/user/{name}', function ($name) {
return $name;
})
->convert('name', $nameFudger)
->assert('name', 'some|sort|of|regex');
// Other cool things: default values, named routes,
// enforce HTTPS, pre-controller, post-controller,
// Send/Receive JSON, send files, send streams...
// Inject a controller provider to get controllers
// out of the index file!
$app->run();
Take full advantage of powerful PHP components already built for you.
<?php // web/index.php
// ...
// Register provider into the service container (Pimple)
$app->register(new Silex\Provider\TwigServiceProvider(), array(
'twig.path' => __DIR__.'/views',
));
$app->get('/hello/{name}', function ($name) use ($app) {
// Use the twig service to render a template
return $app['twig']->render('hello.html.twig', array(
'name' => $name,
));
});
// ...
$ composer require twig/twig
$ composer require symfony/twig-bridge
{# web/views/hello.html.twig #}
Hello {{ name }}!
$app->error(function (\Exception $e, $code) use ($app) {
if ($app['debug']) {
return;
}
switch ($code) {
case 404:
$message = 'The requested page could not be found.';
break;
default:
$message = 'We are sorry, but something went terribly wrong.';
}
});
In less than an hour you've already seen "the big stuff".
If you want to know everything else...
$ wget ftp://ftp.trailing-edge.com/pub/rsx11freewarev2/rsx81b/374001/jargon.txt
$ grep -A 2 '^GROK' jargon.txt
GROK [from the novel "Stranger in a Strange Land", by Robert Heinlein,
where it is a Martian word meaning roughly "to be one with"] v. To
understand, usually in a global sense.
<?php // app.php
require_once __DIR__.'/vendor/autoload.php';
// Get a new instance of Cilex
$app = new \Cilex\Application('Cilex');
// TODO: All the things
// Start parsing the commands and route it to things etc...
$app->run();
$ php run.php
Cilex version
Usage:
command [options] [arguments]
Options:
--help (-h) Display this help message
--quiet (-q) Do not output any message
--verbose (-v|vv|vvv) Increase the verbosity of messages: 1 for normal output,
2 for more verbose output and 3 for debug
--version (-V) Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
--no-interaction (-n) Do not ask any interactive question
Available commands:
help Displays help for a command
list Lists commands
<?php // run.php
require_once __DIR__.'/vendor/autoload.php';
// Get a new instance of Cilex
$app = new \Cilex\Application('Cilex');
// Registers a command into Cilex. This is similar to adding routes.
$app->command(
// look at this class if confused about commands
new \Cilex\Command\GreetCommand()
);
// Start parsing the commands and route it to things etc...
$app->run();
$ php run.php
Cilex version
Usage:
command [options] [arguments]
Options:
--help (-h) Display this help message
--quiet (-q) Do not output any message
--verbose (-v|vv|vvv) Increase the verbosity of messages: 1 for normal output,
2 for more verbose output and 3 for debug
--version (-V) Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
--no-interaction (-n) Do not ask any interactive question
Available commands:
help Displays help for a command
list Lists commands
demo
demo:greet Greet someone
$ php run.php demo:greet --help
Usage:
demo:greet [--yell] [name]
Arguments:
name Who do you want to greet?
Options:
--yell If set, the task will yell in uppercase letters
--help (-h) Display this help message
--quiet (-q) Do not output any message
--verbose (-v|vv|vvv) Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
--version (-V) Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
--no-interaction (-n) Do not ask any interactive question
$ php run.php demo:greet
Hello
$ php run.php demo:greet --yell achievers
HELLO ACHIEVERS
<?php // ../vendors/Cilex/Cilex/Src/Cilex/Command/GreetCommand.php
// namespaces and use...
class GreetCommand extends Command {
protected function configure() {
$this
->setName('demo:greet')
->setDescription('Greet someone')
->addArgument('name', InputArgument::OPTIONAL, 'Who do you want to greet?')
->addOption('yell', null, InputOption::VALUE_NONE, 'If set, the task will yell in uppercase letters');
}
protected function execute(InputInterface $input, OutputInterface $output) {
$name = $input->getArgument('name');
$text = 'Hello';
if ($name) {
$text .= ' '.$name;
}
if ($input->getOption('yell')) {
$text = strtoupper($text);
}
$output->writeln($text);
}
}
<?php // ... blah blah this is a class...
protected function execute(
InputInterface $input,
OutputInterface $output
) {
$twig = $this->getApplication()->getService('twig');
$text = $twig->render('hello.txt.twig', [
'name' => $input->getArgument('name'),
'yell' => $input->getOption('yell'),
]);
$output->writeln($text);
}
// inside run.php
// The silex provider is probably compatible
$app->register(new Silex\Provider\TwigServiceProvider(), [
'twig.path' => __DIR__.'/views',
]);
$ composer require twig/twig
$ composer require symfony/twig-bridge
{# views/hello.txt.twig #}
Hello {{ yell ? name|capitalize : name }}!
* Option I like the best for demonstrating these frameworks
** Option I like best
By Brian Graham
Silex is a concise, extensible and testable web micro-framework based on common Symfony components. Cilex is it's command-line twin. Be dazzled and amazed at how easy it is to go from rapid prototyping to large apps, and even provide the same business logic to both frameworks.
Brian is a Sr. Software Developer. He's spoken at TrueNorthPHP, FullStackTO, GTAPHP, and Coder Camp Hamilton.