It looks like you are viewing these slides on GitHub. Please clone the source repository in order to get the included PHP examples to work.
/
show the homepage/news
show all newsitems/news/{id}
show the proper newsitem/news/{id}
via POST
add a comment$ composer require silex/silex:~1.1
Silex\Application
require_once __DIR__ . '/../vendor/autoload.php';
$app = new Silex\Application();
$app->get('/hello/{name}', function($name) use($app) {
return 'Hello ' . $app->escape($name);
});
$app->run();
If robots can't go to heaven, heaven can come to us.
public_html/
— Public files
index.php
— Front Controllercss/
& js/
& img/
& files/
& ….htaccess
(or the like)app/
— Project logic
bootstrap.php
— App Bootstrapapp.php
— Routingvendor/
— (as created by Composer)
composer.json
& composer.lock
Let's take a look at the files in assets/ws2-sws-fiddles-silex/00.basic/
→
/public_html/index.php
is the Front Controller
<?php
require_once __DIR__ . '/../app/app.php';
$app->run();
/app/app.php
contains the actual routing
<?php
require __DIR__ . '/bootstrap.php';
$app->get('/', function(Silex\Application $app) {
return 'ohai';
});
/app/bootstrap.php
creates & configures the $app
<?php
require_once __DIR__ . '/../vendor/autoload.php';
$app = new Silex\Application();
$app['debug'] = true;
Now here's a route with some chest hair.
$app->match('pattern', function(Silex\Application $app) {
// ...
})->method('GET|POST');
$app->get('pattern', function(Silex\Application $app) {
// ...
});
$app->post('pattern', function(Silex\Application $app) {
// ...
});
$app->get('/', function(Silex\Application $app) {
// show something (e.g. return a blob of HTML)
});
$app->get('/about/', function(Silex\Application $app) {
// show something else (e.g. return a blob of HTML)
});
$app->post('/', function(Silex\Application $app) {
// do something (e.g. insert into database, send e-mail)
});
$app->get('/hello/{name}/', function(Silex\Application $app, $name) {
return 'Hello ' . $app->escape($name) . '!';
});
$app->get('/blog/{postId}/{commentId}/', function (Silex\Application $app, $postId, $commentId) {
// ...
});
$app->redirect()
⚑
$app->get('/', function(Silex\Application $app) {
return $app->redirect($app['request']->getBaseUrl() . '/hello');
});
$app->error()
⚑
$app->error(function (\Exception $e, $code) {
if ($code == 404) {
return 'Please visit <code>/hello/<em>name</em></code>';
} else {
return 'Shenanigans! Something went horribly wrong';
}
});
/
$app->get('/hello', …);
will respond to http://localhost/hello
but not to http://localhost/hello/
$app->get('/hello/', …);
will respond to http://localhost/hello
and to http://localhost/hello/
Let's take a look at the files in assets/ws2-sws-fiddles-silex/01.hello/
→
Let's take a look at the files in assets/ws2-sws-fiddles-silex/02.olleh/
→
->host()
$app->match('/', function() {
// app-specific action
})->host('example.com');
$app->match('/', function ($user) {
// user-specific action
})->host('{user}.example.com');
->requireHttp()
and ->requireHttps()
->convert()
$app->get('/user/{id}', function ($id) {
// ...
})->convert('id', function ($id) { return new User($id); });
$app['request']
actually is an instance of Symfony\Component\HttpFoundation\Request
⚑Request
, Silex also uses Symfony's Response
⚑use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
$app->post('/feedback', function(Request $request) {
$message = $request->get('message');
mail('feedback@yoursite.com', '[YourSite] Feedback', $message);
return new Response('Thank you for your feedback!', 201);
});
JsonResponse
or use the $app->json()
shorthand⚑
$app->get('/users/{id}', function(Silex\Application $app, $id) {
return $app->json(getUser($id));
});
BinaryFileResponse
or use the $app->sendFile()
shorthand⚑
$app->get('/files/{path}', function(Silex\Application $app, $path) {
if (!file_exists('/base/path/' . $path)) { return 'Invalid File'; }
return $app->sendFile('/base/path/' . $path);
});
Number 1.0, I hereby petition you
for an emergency sort-and-file!
Let's take a look at the files in assets/ws2-sws-fiddles-silex/03.tweets/
→
use
⚑
$app->get('/tweets', function(Silex\Application $app) use ($tweets) {
$output = '<ul>';
foreach ($tweets as $tweet) {
$output .= '<li><a href="' . $app['request']->getBaseUrl(). '/tweets/'
. $app->escape($tweet['id']) . '">¶</a> '
. $app->escape($tweet['text']) . '</li>';
}
$output .= '</ul>';
return $output;
});
$app->abort()
⚑
$app->get('/tweets/{id}', function(Silex\Application $app, $id) use ($tweets) {
if (!in_array($id, array_column($tweets, 'id'))) {
$app->abort(404, "Tweet $id does not exist");
}
...
})->assert('id', '\d+');
/app/app.php
will quickly become one cluttered messControllerCollection
⚑
ControllerCollection
instance onto a base route
// define controller for a blog
$blog = $app['controllers_factory'];
$blog->get('/', function () {
return 'Blog home page';
});
$blog->get('/{id}', function () {
return 'Blog detail page';
})->assert('id', '\d+');
// Mount the blog controller onto the /blog route
$app->mount('/blog', $blog);
ControllerCollection
ControllerCollection
Let's take a look at the files in assets/ws2-sws-fiddles-silex/04.tweets-organized/
→
/src
composer.json
{
"require": {
"silex/silex": "~1.1"
},
"autoload": {
"psr-0": {
"Ikdoeict": "src/"
}
}
}
composer update
if you've already run composer install
Silex\ControllerProviderInterface
TweetsController
recommended
/src/Namespace/Provider/Controller
connect
function regulates the (sub)routes to other instance functions
/app/app.php
$app->mount('/tweets', new Ikdoeict\Provider\Controller\TweetsController());