Upgrade to Pro — share decks privately, control downloads, hide ads and more …

爆速フレームワークでREST APIを作った話

爆速フレームワークでREST APIを作った話

CodeCamp LT大会 LT:5-10分
PhalconPHPを使い、REST APIを作った話をしました。
Phalconの特性やメリットなどを中心にお話させていただいてます。

泰 昌平@ShoheiTai

December 18, 2015
Tweet

More Decks by 泰 昌平@ShoheiTai

Other Decks in Programming

Transcript

  1. 機能も充実している ・ORM ・DI ・AutoLoader ・PHQL ・Filter ・ACL ・Forms Builder ・Pagination

    ・Cache ・Security ・Cookies ・Validation ・FlashMessages ・TemplateEngine などなど。。
  2. 実装(Config) // Read the configuration $config = include __DIR__.'/../config/config.php'; //

    config.php return new \Phalcon\Config(array( 'database' => array( 'adapter' => 'Mysql', 'host' => 'localhost', 'username' => 'root', 'password' => '', 'name' => 'test', ), 'application' => array( 'modelsDir' => __DIR__ . '/../models/', 'baseUri' => '/api/', ) ));
  3. 実装(bootstrap) $di = new FactoryDefault(); // Database connection is created

    based in the parameters defined in the configuration file $di->set('db', function() use ($config) { return new Database(array( "host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->name )); }); // Registering an autoloader $loader = new Loader(); $loader->registerDirs( array( $config->application->modelsDir ) )->register();
  4. 実装(API) $app = new Micro($di); // Retrieves all robots $app->get('/api/robots',

    function () use ($app) { }); // Retrieves robots based on primary key $app->get('/api/robots/{id:[0-9]+}', function ($id) use ($app) { }); // Adds a new robot $app->post(('/api/robots', function () use ($app) { }); // Updates robots based on primary key $app->put('/api/robots/{id:[0-9]+}', function ($id) use ($app) { }); // Deletes robots based on primary key $app->delete('/api/robots/{id:[0-9]+}', function ($id) use ($app) { }); $app->handle();
  5. 実装(Model) $app->get('/api/robots', function () use ($app) { $robots = Robots::find(array(

    "conditions" => "type = ?1", "bind" => array(1 => "virtual") )); $data = array(); foreach ($robots as $robot) { $data[] = array( 'id' => $robot->id, 'name' => $robot->name ); } return $app->response->setJsonContent($data); });
  6. Credits Special thanks to all the people who made and

    released these awesome resources for free: ✘ Presentation template by SlidesCarnival ✘ Photographs by Unsplash