Everyone knows that Silex is a great microframework for APIs and small sites, but what do you do when you want to build a large site, or your little tiny site has grown up? Silex has many different ways to let you build larger, complex websites that might still be too small for Symfony, but have outgrown the single page app it once was. We’ll look at what Silex offers us, and different ways we can structure our site.
13. 13
And you add more pages
<?php
require_once __DIR__ . '/../vendor/autoload.php';
$app = new SilexApplication();
$app->get('/', function() use ($app) {/*...*/});
$app->get('/about, function() use ($app) {/*...*/});
$app->get('/contact', function() use ($app) {/*...*/});
$app->get('/services', function() use ($app) {/*...*/});
$app->run();
33. 33
For Services
● Implement
SilexServiceProviderInterface
● Implement boot() and
register()
34. 34
For Controllers
● Implement
SilexControllerProviderInterface
● Implement connect()
35. 35
Sample Controller
use SilexApplication;
use SilexControllerProviderInterface;
class BlogController implements ControllerProviderInterface {
public function connect(Application $app) {
$controllers = $app['controllers_factory'];
$controllers->get('/', array($this, 'index'));
$controllers->get('/inline', function() use ($app) {
/* Logic Here */
});
}
protected function index(Application $app) {
/* Logic Here */
}
}
$app->mount('/blog', new BlogController());
36. 36
Controllers as Services
● Ships with Silex
● Allows attaching objects to
routes
● Favors dependency injection
● Framework Agnostic
37. 37
Create our Object
// src/MyApp/Controller/IndexController.php
namespace MyAppController;
use SilexApplication;
use SymfonyComponentHttpFoundationRequest;
class IndexController
{
public function indexAction(Application $app)
{
return $app['twig']->render('Index/index.html.twig');
}
public function aboutAction(Application $app) { /* … */ }
public function servicesAction(Application $app) { /* … */ }
public function contactAction(Application $app) { /* … */ }
}