BlogControllerProvider.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace demo\Controllers;
  3. use Silex\Api\ControllerProviderInterface;
  4. use Silex\Application;
  5. use Silex\ControllerCollection;
  6. class BlogControllerProvider implements ControllerProviderInterface {
  7. const MAGIC = 'xyzzy';
  8. /**
  9. * Returns routes to connect to the given application.
  10. *
  11. * @param Application $app An Application instance
  12. *
  13. * @return ControllerCollection A ControllerCollection instance
  14. */
  15. public function connect(Application $app): ControllerCollection {
  16. // This is a factory service, producing a new instance every time.
  17. $collection = $app['controllers_factory'];
  18. $blog = BlogController::class;
  19. $collection->get('/', "$blog::index")
  20. ->bind('blog_list');
  21. $collection->get('/{id}', "$blog::index2")
  22. ->assert('id', static::MAGIC)
  23. // Serve '(mount)/xyzzy' when asked for '(mount)'.
  24. ->value('id', static::MAGIC)
  25. ->bind('blog_mount');
  26. $collection->get('/json', "$blog::json");
  27. $collection->get('/json-view', "$blog::jsonView");
  28. $collection->get('/{id}', "$blog::fifiAction")
  29. ->assert('id', '\d+')
  30. // See Symfony expression language.
  31. ->when("request.headers.get('User-Agent') matches '/firefox/i'");
  32. $collection->get('/{id}', "$blog::show")
  33. ->assert('id', '\d+')
  34. // Serve '(mount)/1' when asked for '(mount)/'.
  35. ->value('id', 1)
  36. ->bind('blog_post');
  37. return $collection;
  38. }
  39. }