Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Silex. Микрофреймворк для микроприложений

3,212 views

Published on

Published in: Technology
  • Be the first to comment

Silex. Микрофреймворк для микроприложений

  1. 1. Silex. Микрофреймворк для микроприложений.<br />Судаков Иван,<br />веб-разработчик<br />Ivan.Sudakov@softline.ru<br />
  2. 2. Agenda<br />Теория раз<br />Теория два<br />Теория три <br />Практика<br />
  3. 3.
  4. 4.
  5. 5.
  6. 6.
  7. 7.
  8. 8. cool story bro<br />
  9. 9. what about code?<br />
  10. 10. Микроприложение<br />REST API для микроблога XITTER (sileXtwITTER)<br />получить все xitt’ы;<br />получить xittпо id;<br />добавить xitt.<br />
  11. 11. Init<br /><?php<br />require_once__DIR__ . '/silex.phar';<br />$app = new SilexApplication();<br />// TODO routes<br />$app->run();<br />
  12. 12. Роуты<br /><?php<br />require_once__DIR__ . '/silex.phar';<br />$app = new SilexApplication();<br />$app->get('/list', function() use ($app) {<br />// TODO<br />});<br />$app->get('/show/{xitt}', function($xitt) use ($app) {<br />// TODO<br />});<br />$app->put(‘/add', function() use ($app) {<br />// TODO<br />});<br />$app->run();<br />
  13. 13. Doctrine<br />/**<br /> * Register Doctrine service<br /> * @see http://www.doctrine-project.org/projects/dbal<br /> */<br />$app->register(newSilexExtensionDoctrineExtension(), array(<br />'db.options' =>array(<br /> 'driver' => 'pdo_mysql',<br /> 'host' => 'localhost',<br /> 'user' => 'root',<br /> 'password' => ‘root_password',<br /> 'dbname' => 'xitter'<br /> ),<br />'db.dbal.class_path' => __DIR__ . '/vendor',<br />'db.common.class_path' => __DIR__ . '/vendor',<br />));<br />
  14. 14. Список xitt’ов<br />useSymfonyComponentHttpFoundationResponse;<br />/**<br /> * List of xitts<br /> */<br />$app->get('/list', function() use ($app) {<br />$xitts = $app['db']->fetchAll('SELECT * from xitt');<br />if (empty($xitts)) {<br />return new Response(null, 204);<br /> }<br />return new Response(<br />json_encode($xitts),<br />200,<br />array('Content-Type' => 'application/json')<br />);<br />});<br />
  15. 15. Добавление xitt’а<br />/**<br /> * Add a xitt<br /> */<br />$app->put('/add', function() use ($app) {<br />$xitts = $app['db']->insert(<br /> 'xitt',<br />array(<br />'author’=>$app['request']->get('author'),<br />'body’=>$app['request']->get('body')<br /> )<br /> );<br /> return new Response('Xitt is added succesfully.’, 201);<br />});<br />
  16. 16. Получение xitt’а по id<br />/**<br /> * One xitt<br /> */<br />$app->get('/show/{xitt}', function($xitt) use ($app) {<br /> $xittInfo = $app['db‘]->fetchAssoc(<br />'SELECT * FROM xitt WHERE id = ?‘,<br />array($xitt)<br /> );<br /> if (empty($xittInfo)) {<br /> throw newNotFoundHttpException();<br /> }<br />return new Response(<br />json_encode($xittInfo),<br />200,<br />array('Content-Type' => 'application/json')<br /> );<br />}); <br />
  17. 17. Валидация<br />/**<br /> * One xitt<br /> */<br />$app->get('/show/{xitt}', function($xitt) use ($app) {<br /> $xittInfo = $app['db‘]->fetchAssoc(<br />'SELECT * FROM xitt WHERE id = ?‘,<br />array($xitt)<br /> );<br /> if (empty($xittInfo)) {<br /> throw newNotFoundHttpException();<br /> }<br />return new Response(<br />json_encode($xittInfo),<br />200,<br />array('Content-Type' => 'application/json')<br /> );<br />})->assert('xitt', 'd+'); <br />
  18. 18. Конвертирование<br />$getXittById= function($id) use ($app) {<br />return$app['db‘]->fetchAssoc(<br />'SELECT * FROM xitt WHERE id = ?‘,<br />array($id)<br /> );<br />};<br />/**<br /> * One xitt<br /> */<br />$app->get('/show/{xitt}', function($xitt) use ($app) {<br /> if (empty($xitt)) {<br /> throw newNotFoundHttpException();<br /> }<br />return new Response(<br />json_encode($xitt),<br />200,<br />array('Content-Type' => 'application/json')<br /> );<br />})->assert('xitt', 'd+')->convert('xitt', $getXittById); <br />
  19. 19. Обработка ошибок<br />/**<br /> * Error handler<br /> */<br />useSymfonyComponentHttpKernelExceptionHttpException;<br />useSymfonyComponentHttpKernelExceptionNotFoundHttpException;<br />$app->error(function(Exception $e) {<br />if ($einstanceofNotFoundHttpException) {<br />return new Response(<br />'The requested page could not be found.', 404<br />);<br /> }<br />$code = ($einstanceofHttpException)? $e->getStatusCode() : 500;<br />return new Response(<br />'We are sorry, but something went terribly wrong.', $code<br />);<br />});<br />
  20. 20. Логирование<br />/**<br /> * Register Monolog service<br /> * @see https://github.com/Seldaek/monolog<br /> */<br />$app->register(new SilexExtensionMonologExtension(), array(<br />'monolog.logfile' => __DIR__.'/log',<br />'monolog.class_path' => __DIR__.'/vendor',<br />));<br />/**<br /> * "Before" part of request wrapper<br /> */<br />$app->before(function() use ($app) {<br />$app['monolog']->addDebug(var_export($app['request'], true));<br />});<br />
  21. 21. :)<br />
  22. 22.
  23. 23. Ништяки<br />Quality codebase<br />
  24. 24. Ништяки<br />Quality codebase<br />It’s based on the Symfony2 components<br />
  25. 25. Ништяки<br />Quality codebase<br />It’s based on the Symfony2 components<br />It’s got the coolest, tiniest DI container on the planet<br />
  26. 26. Ништяки<br />Quality codebase<br />It’s based on the Symfony2 components<br />It’s got the coolest, tiniest DI container on the planet<br />The Extension API<br />
  27. 27. Ништяки<br />Quality codebase<br />It’s based on the Symfony2 components<br />It’s got the coolest, tiniest DI container on the planet<br />The Extension API<br />And boy, is it testable!<br />По мотивам http://codenugget.org/5-reasons-why-silex-is-king-of-all-php-micro<br />
  28. 28. Для чего всё это?<br />Простая бизнес-логика<br />REST API<br />Обучение<br />Прототипирование<br />…<br />
  29. 29.
  30. 30. Что почитать?<br />Оф. сайт http://silex-project.org/<br />Реклама http://codenugget.org/5-reasons-why-silex-is-king-of-all-php-micro<br />Гуглогруппаhttp://groups.google.com/group/silex-php/<br />Русскоязычное сообществоhttp://silex-ru.net/<br />
  31. 31. ?<br />

×