12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- <?php
- namespace demo\Controllers;
- use Silex\Application;
- use Symfony\Component\HttpFoundation\Response;
- /**
- * Class BlogController demonstrates most basic controller types.
- *
- * Available automatic arguments on controllers: Application, Request.
- *
- * @package demo\Controllers
- */
- class BlogController {
- const POSTS = [
- 1 => [
- 'date' => '2011-03-29',
- 'author' => 'igorw',
- 'title' => 'Using Silex',
- 'body' => '...It takes time on version changes...',
- ],
- 2 => [
- 'date' => '2015-03-29',
- 'author' => 'igorw',
- 'title' => 'Using Silex 2',
- 'body' => '...Especialy S1 to S2...',
- ],
- ];
- // Default: http://blog, not http://blog/
- public function fifiAction(Application $app, $id) {
- return "<h1>Fifi action {$id} </h1>\n";
- }
- public function index() {
- $output = "<ul>\n";
- foreach (static::POSTS as $post) {
- $output .= "<li>" . $post ['title'] . "</li>\n";
- }
- $output .= "</ul>\n";
- return $output;
- }
- // Default: http://blog, not http://blog/
- public function show(Application $app, $id) {
- if (!isset (static::POSTS[$id])) {
- // Will trigger the error() (or built-in) error handler.1
- $app->abort(Response::HTTP_NOT_FOUND, "Post $id does not exist.");
- }
- $post = static::POSTS[$id];
- return "<h1> {$post['title']}</h1>" . "<p> {$post['body']} </p>";
- }
- // Encode response manually.
- public function json(Application $app) {
- return $app->json(static::POSTS);
- }
- // Rely on the view handler.
- public function jsonView() {
- return static::POSTS;
- }
- }
|