/** * apps以下のrouting設定に沿ってアクションを設定 * @param $method * @param $actions * @throws \Exception */ private function setAction($method, $actions) { foreach ($actions as $pattern => $action) { switch ($method) { case 'get': $this->router->get($pattern, $action); break; case 'post': $this->router->post($pattern, $action); break; case 'put': $this->router->put($pattern, $action); break; case 'delete': $this->router->delete($pattern, $action); break; default: throw new KtrRuntimeException('Invalid routing method setting'); } } }
function router($app, $controllerName, $requests, $lazyLoad = true) { $class_name = $controllerName . 'Controller'; $handler = new $class_name(); $collections = new MicroCollection(); $collections->setHandler($handler, $lazyLoad); foreach ($requests as $action) { switch ($action['method']) { case 'get': $collections->get($action['path'], $action['action']); break; case 'post': $collections->post($action['path'], $action['action']); break; case 'delete': $collections->delete($action['path'], $action['action']); break; case 'put': $collections->put($action['path'], $action['action']); break; } } $app->mount($collections); }
<?php namespace Collections; use Phalcon\Mvc\Micro\Collection as MicroCollection; $mangaChapterCollection = new MicroCollection(); $mangaChapterCollection->setHandler('Controllers\\MangaChapterController'); $mangaChapterCollection->setPrefix('/api/mangas/chapters'); $mangaChapterCollection->get('/{mangaChapterId:[0-9]+}', 'get'); $mangaChapterCollection->post('/', 'add'); $mangaChapterCollection->put('/{mangaChapterId:[0-9]+}', 'update'); $mangaChapterCollection->delete('/{mangaChapterId:[0-9]+}', 'delete'); $mangaChapterCollection->get('/all', 'getAll'); $mangaChapterCollection->get('/list', 'getValidList'); $mangaChapterCollection->get('/list/{statusName:[a-z]+}', 'getList'); return $mangaChapterCollection;
<?php namespace Collections; use Phalcon\Mvc\Micro\Collection as MicroCollection; // Setup Collection $tvSeriesGenreCollection = new MicroCollection(); $tvSeriesGenreCollection->setHandler('Controllers\\TVSeriesGenreController', true); $tvSeriesGenreCollection->setPrefix('/api/tvseries/genres/'); // Define routes $tvSeriesGenreCollection->get('/{tvSeriesGenreId:[0-9]+}', 'get'); $tvSeriesGenreCollection->post('/', 'add'); $tvSeriesGenreCollection->delete('/{tvSeriesGenreId:[0-9]+}', 'delete'); return $tvSeriesGenreCollection;
<?php namespace Collections; use Phalcon\Mvc\Micro\Collection as MicroCollection; $songCollection = new MicroCollection(); $songCollection->setHandler('Controllers\\SongController'); $songCollection->setPrefix('/api/songs'); $songCollection->get('/{songId:[0-9]+}', 'get'); $songCollection->post('/', 'add'); $songCollection->put('/{songId:[0-9]+}', 'update'); $songCollection->delete('/{songId:[0-9]+}', 'delete'); $songCollection->get('/all', 'getAll'); $songCollection->get('/list', 'getValidList'); $songCollection->post('/list/{statusName:[a-z]+}', 'getList'); return $songCollection;
// return $auth; //}; // CoreController if ($app['controllers']['core']) { $core = new MicroCollection(); // Set the handler & prefix $core->setHandler(new CoreController($app)); $core->setPrefix('/'); // Set routers $core->get('/', 'index'); $app->mount($core); } // UsersController if ($app['controllers']['user']) { $users = new MicroCollection(); // Set the handler & prefix $users->setHandler(new UserController($app)); $users->setPrefix('/user'); // Set routers $users->post('/', 'create'); $users->put('/{id}', 'update'); $users->delete('/{id}', 'delete'); $users->get('/', 'userList'); $users->get('/{id}', 'info'); $app->mount($users); } // Not Found $app->notFound(function () use($app) { $app->response->setStatusCode(404, "Not Found")->sendHeaders(); }); $app->handle();
<?php use Phalcon\Mvc\Micro\Collection as MicroCollection; $version = basename(dirname(__FILE__)); $controller_path = 'App\\Controllers\\' . strtoupper($version) . '\\'; /** * Setup collection */ $collection = new MicroCollection(); $collection->setHandler($controller_path . 'FollowersController', true); $collection->setPrefix('/api/' . $version . '/followers'); /** * Define routes */ $collection->get('/{id:[0-9]+}', 'index'); $collection->post('/{id:[0-9]+}', 'create'); $collection->delete('/{id:[0-9]+}', 'delete'); return $collection;
<?php namespace Collections; use Phalcon\Mvc\Micro\Collection as MicroCollection; $animeCollection = new MicroCollection(); $animeCollection->setHandler('Controllers\\AnimeController'); $animeCollection->setPrefix('/api/animes'); $animeCollection->get('/{animeId:[0-9]+}', 'get'); $animeCollection->post('/', 'add'); $animeCollection->put('/{animeId:[0-9]+}', 'update'); $animeCollection->delete('/{animeId:[0-9]+}', 'delete'); $animeCollection->get('/all', 'getAll'); $animeCollection->get('/list', 'getValidList'); $animeCollection->get('/list/{statusName:[a-z]+}', 'getList'); $animeCollection->get('/{animeId:[0-9]+}/seasons', 'getSeasons'); return $animeCollection;
<?php namespace Collections; use Phalcon\Mvc\Micro\Collection as MicroCollection; $celebrityCollection = new MicroCollection(); $celebrityCollection->setHandler('Controllers\\CelebrityController'); $celebrityCollection->setPrefix('/api/celebrities'); $celebrityCollection->get('/{celebrityId:[0-9]+}', 'get'); $celebrityCollection->post('/', 'add'); $celebrityCollection->put('/{celebrityId:[0-9]+}', 'update'); $celebrityCollection->delete('/{celebrityId:[0-9]+}', 'delete'); $celebrityCollection->get('/all', 'getAll'); $celebrityCollection->get('/list', 'getValidList'); $celebrityCollection->get('/list/{statusName:[a-z]+}', 'getList'); $celebrityCollection->get('/{celebrityId:[0-9]+}/movies', 'getMovies'); $celebrityCollection->get('/{celebrityId:[0-9]+}/productions', 'getProductions'); return $celebrityCollection;
<?php namespace Collections; use Phalcon\Mvc\Micro\Collection as MicroCollection; // Setup Collection $songGenreCollection = new MicroCollection(); $songGenreCollection->setHandler('Controllers\\SongGenreController', true); $songGenreCollection->setPrefix('/api/songs/genres/'); // Define routes $songGenreCollection->get('/{songGenreId:[0-9]+}', 'get'); $songGenreCollection->post('/', 'add'); $songGenreCollection->delete('/{songGenreId:[0-9]+}', 'delete'); return $songGenreCollection;
<?php namespace Collections; use Phalcon\Mvc\Micro\Collection as MicroCollection; $tvSeriesCollection = new MicroCollection(); $tvSeriesCollection->setHandler('Controllers\\TVSeriesController'); $tvSeriesCollection->setPrefix('/api/tvseries'); $tvSeriesCollection->get('/{tvseriesId:[0-9]+}', 'get'); $tvSeriesCollection->post('/', 'add'); $tvSeriesCollection->put('/{tvseriesId:[0-9]+}', 'update'); $tvSeriesCollection->delete('/{tvseriesId:[0-9]+}', 'delete'); $tvSeriesCollection->get('/all', 'getAll'); $tvSeriesCollection->get('/list', 'getValidList'); $tvSeriesCollection->get('/list/{statusName:[a-z]+}', 'getList'); $tvSeriesCollection->get('/{tvseriesId:[0-9]+}/seasons', 'getSeasons'); return $tvSeriesCollection;
$projetoAnexos->delete('/', 'delete'); $app->mount($projetoAnexos); /*******************************************************/ $download = new MicroCollection(); $download->setHandler(new wsGerProj\Controllers\DownloadController()); $download->setPrefix('/download'); $download->get('/{id}', 'download'); $app->mount($download); /*******************************************************/ $tarefa = new MicroCollection(); $tarefa->setHandler(new A\TarefasController()); $tarefa->setPrefix('/admin/tarefas'); $tarefa->get('/', 'index'); $tarefa->get('/{id}', 'show'); $tarefa->post('/', 'create'); $tarefa->put('/{id}', 'update'); $tarefa->delete('/{id}', 'delete'); $tarefa->get('/tipos', 'getTipos'); $tarefa->get('/status', 'getStatus'); $tarefa->get('/tipos/{id}', 'getTipoID'); $tarefa->get('/status/{id}', 'getStatusID'); $app->mount($tarefa); /*******************************************************/ $tarefaAnexos = new MicroCollection(); $tarefaAnexos->setHandler(new A\TarefaAnexosController()); $tarefaAnexos->setPrefix('/admin/tarefa_anexos'); $tarefaAnexos->post('/', 'create'); $tarefaAnexos->get('/{id}', 'show'); $tarefaAnexos->delete('/', 'delete'); $app->mount($tarefaAnexos); /*******************************************************/
<?php namespace Collections; use Phalcon\Mvc\Micro\Collection as MicroCollection; $mangaCollection = new MicroCollection(); $mangaCollection->setHandler('Controllers\\MangaController'); $mangaCollection->setPrefix('/api/mangas'); $mangaCollection->get('/{mangaId:[0-9]+}', 'get'); $mangaCollection->post('/', 'add'); $mangaCollection->put('/{mangaId:[0-9]+}', 'update'); $mangaCollection->delete('/{mangaId:[0-9]+}', 'delete'); $mangaCollection->get('/all', 'getAll'); $mangaCollection->get('/list', 'getValidList'); $mangaCollection->get('/list/{statusName:w+}', 'getList'); $mangaCollection->get('/{mangaId:[0-9]+}/chapters/all', 'getChapters'); return $mangaCollection;
<?php namespace Collections; use Phalcon\Mvc\Micro\Collection as MicroCollection; $seasonCollection = new MicroCollection(); $seasonCollection->setHandler('Controllers\\SeasonController'); $seasonCollection->setPrefix('/api/seasons'); $seasonCollection->get('/{seasonId:[0-9]+}', 'get'); $seasonCollection->post('/', 'add'); $seasonCollection->put('/{seasonId:[0-9]+}', 'update'); $seasonCollection->delete('/{seasonId:[0-9]+}', 'delete'); $seasonCollection->get('/all', 'getAll'); $seasonCollection->get('/list', 'getValidList'); $seasonCollection->get('/list/{statusName:[a-z]+}', 'getList'); $seasonCollection->get('/{seasonId:[0-9]+}/episodes/all', 'getEpisodes'); return $seasonCollection;
<?php namespace Collections; use Phalcon\Mvc\Micro\Collection as MicroCollection; // Setup Collection $movieGenreCollection = new MicroCollection(); $movieGenreCollection->setHandler('Controllers\\MovieGenreController', true); $movieGenreCollection->setPrefix('/api/movies/genres/'); // Define routes $movieGenreCollection->get('/{movieGenreId:[0-9]+}', 'get'); $movieGenreCollection->post('/', 'add'); $movieGenreCollection->delete('/{movieGenreId:[0-9]+}', 'delete'); return $movieGenreCollection;
<?php namespace Collections; use Phalcon\Mvc\Micro\Collection as MicroCollection; // Setup Collection $gamePlatformCollection = new MicroCollection(); $gamePlatformCollection->setHandler('Controllers\\GamePlatformController', true); $gamePlatformCollection->setPrefix('/api/games/platforms'); // Define routes $gamePlatformCollection->get('/{gamePlatformId:[0-9]+}', 'get'); $gamePlatformCollection->post('/', 'add'); $gamePlatformCollection->delete('/{gamePlatformId:[0-9]+}', 'delete'); return $gamePlatformCollection;
<?php namespace Collections; use Phalcon\Mvc\Micro\Collection as MicroCollection; $productionCollection = new MicroCollection(); $productionCollection->setHandler('Controllers\\ProductionController'); $productionCollection->setPrefix('/api/productions'); $productionCollection->get('/{productionId:[0-9]+}', 'get'); $productionCollection->post('/', 'add'); $productionCollection->put('/{productionId:[0-9]+}', 'update'); $productionCollection->delete('/{productionId:[0-9]+}', 'delete'); $productionCollection->get('/all', 'getAll'); $productionCollection->get('/list', 'getValidList'); $productionCollection->get('/list/{statusName:[a-z]+}', 'getList'); $productionCollection->get('/{productionId:[0-9]+}/songs/all', 'getSongs'); return $productionCollection;
<?php namespace Collections; use Phalcon\Mvc\Micro\Collection as MicroCollection; // Setup Collection $fictionalCharacterTypeCollection = new MicroCollection(); $fictionalCharacterTypeCollection->setHandler('Controllers\\FictionalCharacterTypeController', true); $fictionalCharacterTypeCollection->setPrefix('/api/fictionalcharactertypes'); // Define routes $fictionalCharacterTypeCollection->get('/{fictionalCharacterTypeId:[0-9]+}', 'get'); $fictionalCharacterTypeCollection->post('/', 'add'); $fictionalCharacterTypeCollection->delete('/{fictionalCharacterTypeId:[0-9]+}', 'delete'); $fictionalCharacterTypeCollection->get('/all', 'getAll'); return $fictionalCharacterTypeCollection;
* @var $exampleCollection */ $exampleCollection = new \Phalcon\Mvc\Micro\Collection(); $exampleCollection->setLazy(true)->setPrefix('/v1/example')->setHandler('\\PhalconRest\\Controllers\\ExampleController'); // Set Access-Control-Allow headers. $exampleCollection->options('/', 'optionsBase'); $exampleCollection->options('/{id}', 'optionsOne'); // First paramter is the route, which with the collection prefix here would be GET /example/ // Second paramter is the function name of the Controller. $exampleCollection->get('/', 'get'); // This is exactly the same execution as GET, but the Response has no body. $exampleCollection->head('/', 'get'); // $id will be passed as a parameter to the Controller's specified function $exampleCollection->get('/{id:[0-9]+}', 'getOne'); $exampleCollection->head('/{id:[0-9]+}', 'getOne'); $exampleCollection->post('/', 'post'); $exampleCollection->delete('/{id:[0-9]+}', 'delete'); $exampleCollection->put('/{id:[0-9]+}', 'put'); $exampleCollection->patch('/{id:[0-9]+}', 'patch'); $app->mount($exampleCollection); /** * After a route is run, usually when its Controller returns a final value, * the application runs the following function which actually sends the response to the client. * * The default behavior is to send the Controller's returned value to the client as JSON. * However, by parsing the request querystring's 'type' paramter, it is easy to install * different response type handlers. Below is an alternate csv handler. */ $app->after(function () use($app) { // OPTIONS have no body, send the headers, exit if ($app->request->getMethod() == 'OPTIONS') {
<?php namespace Collections; use Phalcon\Mvc\Micro\Collection as MicroCollection; // Setup Collection $mangaPublicCollection = new MicroCollection(); $mangaPublicCollection->setHandler('Controllers\\MangaPublicController', true); $mangaPublicCollection->setPrefix('/api/mangas/publics'); // Define routes $mangaPublicCollection->get('/{mangaPublicId:[0-9]+}', 'get'); $mangaPublicCollection->post('/', 'add'); $mangaPublicCollection->delete('/{mangaPublicId:[0-9]+}', 'delete'); return $mangaPublicCollection;
* Expose the /v1/emails end point */ $emails = new MicroCollection(); $emails->setHandler('EmailController', true); $emails->setPrefix('/v1/emails'); $emails->post('/', 'addEmailAddress'); $emails->put('/{code}/{email}', 'updateEmailAddress'); $emails->get('/{code}/{email}', 'getEmailAddress'); $app->mount($emails); /** * Expose the /v1/orders end point */ $orders = new MicroCollection(); $orders->setHandler('OrderController', true); $orders->setPrefix('/v1/orders'); $orders->post('/', 'addOrder'); $orders->post('/quote', 'getQuote'); $app->mount($orders); /** * CORS Headers to allow the web app to communicate with the web service */ $app->response->setHeader('Access-Control-Allow-Origin', '*'); /** * Not found handler */ $app->notFound(function () use($app) { $app->response->setStatusCode(404, HttpStatusCodes::getMessage(404))->sendHeaders(); $app->response->setContentType('application/json'); $app->response->setJsonContent(['status' => 'error', 'message' => ResponseMessages::METHOD_NOT_IMPLEMENTED, 'code' => 'METHOD_NOT_IMPLEMENTED']); $app->response->send(); });
<?php namespace Collections; use Phalcon\Mvc\Micro\Collection as MicroCollection; // Setup Collection $broadcastTypeCollection = new MicroCollection(); $broadcastTypeCollection->setHandler('Controllers\\BroadcastTypeController', true); $broadcastTypeCollection->setPrefix('/api/broadcasttypes'); // Define routes $broadcastTypeCollection->get('/{broadcastTypeId:[0-9]+}', 'get'); $broadcastTypeCollection->post('/', 'add'); $broadcastTypeCollection->delete('/{broadcastTypeId:[0-9]+}', 'delete'); return $broadcastTypeCollection;
<?php use Phalcon\Mvc\Micro\Collection as MicroCollection; $version = basename(dirname(__FILE__)); $controller_path = 'App\\Controllers\\' . strtoupper($version) . '\\'; /** * Setup collection */ $collection = new MicroCollection(); $collection->setHandler($controller_path . 'EventsController', true); $collection->setPrefix('/api/' . $version . '/events'); /** * Define routes */ $collection->get('/', 'index'); $collection->post('/', 'create'); $collection->put('/{id:[0-9]+}', 'update'); $collection->delete('/{id:[0-9]+}', 'delete'); return $collection;
<?php namespace Collections; use Phalcon\Mvc\Micro\Collection as MicroCollection; $lyricsCollection = new MicroCollection(); $lyricsCollection->setHandler('Controllers\\LyricsController'); $lyricsCollection->setPrefix('/api/lyrics'); $lyricsCollection->get('/{lyricsId:[0-9]+}', 'get'); $lyricsCollection->post('/', 'add'); $lyricsCollection->put('/{lyricsId:[0-9]+}', 'update'); $lyricsCollection->delete('/{lyricsId:[0-9]+}', 'delete'); $lyricsCollection->get('/all', 'getAll'); $lyricsCollection->get('/list', 'getValidList'); $lyricsCollection->get('/list/{statusName:w+}', 'getList'); return $lyricsCollection;
<?php use Phalcon\Mvc\Micro\Collection as MicroCollection; $version = basename(dirname(__DIR__)); $controller_path = 'App\\Controllers\\' . strtoupper($version) . '\\OAuth\\'; /** * Setup collection */ $collection = new MicroCollection(); $collection->setHandler($controller_path . 'OAuthController', true); $collection->setPrefix('/api/' . $version . '/oauth'); /** * Define routes */ $collection->post('/token', 'getToken'); return $collection;
<?php namespace Collections; use Phalcon\Mvc\Micro\Collection as MicroCollection; // Setup Collection $gameGenreCollection = new MicroCollection(); $gameGenreCollection->setHandler('Controllers\\GameGenreController', true); $gameGenreCollection->setPrefix('/api/games/genres'); // Define routes $gameGenreCollection->get('/{gameGenreId:[0-9]+}', 'get'); $gameGenreCollection->post('/', 'add'); $gameGenreCollection->delete('/{gameGenreId:[0-9]+}', 'delete'); return $gameGenreCollection;
<?php namespace Collections; use Phalcon\Mvc\Micro\Collection as MicroCollection; // Setup Collection $mangaGenreCollection = new MicroCollection(); $mangaGenreCollection->setHandler('Controllers\\MangaGenreController', true); $mangaGenreCollection->setPrefix('/api/mangas/genres/'); // Define routes $mangaGenreCollection->get('/{mangaGenreId:[0-9]+}', 'get'); $mangaGenreCollection->post('/', 'add'); $mangaGenreCollection->delete('/{mangaGenreId:[0-9]+}', 'delete'); return $mangaGenreCollection;
* Error handler */ $app->error(function (\Exception $e) use($app) { $app->response->setStatusCode($e->getCode()); $app->response->setContent($e->getMessage()); return $app->response; }); /** * Options handler */ $app->options('/{catch:(.*)}', function () use($app) { return $app->response->setStatusCode(200, "OK"); }); /** * API documentations */ $documentation = new MicroCollection(); $documentation->setHandler('PHPWebDevelopers\\Api\\Store\\Controller\\DocumentationsController', true); $documentation->setPrefix('/documentations'); $documentation->get('/', 'indexAction'); $app->mount($documentation); /** * Store Routes */ $store = new MicroCollection(); $store->setHandler('PHPWebDevelopers\\Api\\Store\\Controller\\StoresController', true); $store->setPrefix('/stores'); $store->get('/', 'listAction'); $store->post('/', 'addAction'); $store->get('/{store}', 'findAction'); $app->mount($store);