/** * apps以下のrouting設定に沿ってルーティングをする */ public function routing() { $nameSpace = APPS_MAIN_CONF['base_namespace'] . '\\Controllers\\'; foreach ($this->conf as $controller => $settings) { $class = $nameSpace . $controller . 'Controller'; $this->init(); $this->router->setHandler($class, true); $this->router->setPrefix($settings['prefix']); foreach ($settings['actions'] as $method => $actions) { $this->setAction($method, $actions); } $this->app->mount($this->router); } }
protected function registerRouters() { $should_login = []; // basic $this->get('/', function () { header('Location: /movie.html'); }); $this->NotFound(function () { $this->response->setStatusCode(404, "Not Found")->sendHeaders(); echo json(['error' => 'param error.']); }); // user $user = new Collection(); $re = $user->setHandler('api\\controller\\UserController', true); $user->setPrefix('/api/users'); $user->get('/status', 'status')->post('/token', 'login')->get('/logout', 'logout')->get('/active', 'active')->post('', 'create'); $this->mount($user); // photo $photo = new Collection(); $photo->setHandler('api\\controller\\PhotoController', true); $photo->setPrefix('/api/photos'); $photo->get('', 'get')->post('', 'create')->post('/upload', 'upload')->post('/import', 'import'); $this->mount($photo); $this->should_login = $should_login; }
public function stitch($routeHandler) { $collection = new MicroCollection(); $collection->setHandler($routeHandler->handler(), true); $collection->setPrefix($routeHandler->prefix()); foreach ($routeHandler->routes() as $route) { $httpMethods = $route->types(); foreach ($httpMethods as $method) { $collection->{strtolower($method)}($route->path(), $route->method()); } } $this->app->mount($collection); }
public function bootstrap(PhalconApp $app) { $collection = new MicroCollection(); $collection->setHandler($this); $prefix = static::getPrefix(); if ($prefix !== false) { $collection->setPrefix($prefix); } foreach ($this->actions() as $className) { $reflection = new \ReflectionClass($className); if ($reflection->implementsInterface(ActionInterface::class)) { $hasMiddleware = $reflection->implementsInterface(MiddlewareSetInterface::class); $this->implementAction($app, $className, $hasMiddleware); } } $app->mount($collection); }
/** * @param string $prefix path * @param string $controller class name * @param bool $lazyLoad default true * @param array $map method * @return self */ public function resource($prefix, $controller, $lazyLoad = true, $map = []) { $collection = new Micro\Collection(); $collection->setHandler($controller, $lazyLoad); $collection->setPrefix("/" . trim($prefix, '/')); foreach (get_class_methods($controller) as $action) { if (isset(self::$resourceMaps[$action])) { $method = strtolower(self::$resourceMaps[$action]['method']); $pattern = self::$resourceMaps[$action]['pattern']; call_user_func_array([$collection, $method], [$pattern, $action]); } } if (!empty($map)) { // TODO: add mapping } $this->mount($collection); return $this; }
public function __construct($prefix) { parent::setPrefix($prefix); $this->initialize(); }
<?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 . 'MusicTagsController', true); $collection->setPrefix('/api/' . $version . '/musictags'); /** * Define routes */ $collection->get('/', 'index'); $collection->post('/', 'create'); return $collection;
<?php use Phalcon\Mvc\Micro\Collection; return call_user_func(function () { $productsCollection = new Collection(); $productsCollection->setPrefix('/v1')->setHandler('\\Resapi\\Controllers\\ProductsController')->setLazy(true); $productsCollection->get('/', 'get'); return $productsCollection; });
<?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; $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;
<?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;
// 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 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;
<?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 . 'HashTagsController', true); $collection->setPrefix('/api/' . $version . '/hashtags'); /** * Define routes */ $collection->get('/', 'index'); $collection->post('/', 'create'); return $collection;
<?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; // 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 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(__FILE__)); $controller_path = 'App\\Controllers\\' . strtoupper($version) . '\\'; /** * Setup collection */ $collection = new MicroCollection(); $collection->setHandler($controller_path . 'DocsController', true); $collection->setPrefix('/api/' . $version . '/docs'); /** * Define routes */ $collection->get('/', 'index'); return $collection;
<?php namespace Collections; use Phalcon\Mvc\Micro\Collection as MicroCollection; $episodeCollection = new MicroCollection(); $episodeCollection->setHandler('Controllers\\EpisodeController'); $episodeCollection->setPrefix('/api/episodes'); $episodeCollection->get('/{episodeId:[0-9]+}', 'get'); $episodeCollection->post('/', 'add'); $episodeCollection->put('/{episodeId:[0-9]+}', 'update'); $episodeCollection->delete('/{episodeId:[0-9]+}', 'delete'); $episodeCollection->get('/all', 'getAll'); $episodeCollection->get('/list', 'getValidList'); $episodeCollection->post('/list/{statusName:[a-z]+}', 'getList'); return $episodeCollection;
<?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 . 'PhotosController', true); $collection->setPrefix('/api/' . $version . '/photos'); /** * 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; $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; // 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;
/** * 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; $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 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;
*/ $di = new FactoryDefault(); $app = new Micro($di); // 日志 $di->setShared('logger', function () { return new \Phalcon\Logger\Adapter\File(APP_PATH . '/logs/' . date('Ymd') . '.log'); }); // api 规则路由 $apiRegister = (require_once APP_PATH . '/config/register.php'); // 注册命名空间 $loader = new \Phalcon\Loader(); $loader->registerNamespaces($apiRegister['namespace'])->register(); foreach ($apiRegister['list'] as $class => $conf) { $index = new Micro\Collection(); $index->setHandler(new $class()); $index->setPrefix($conf['prefix']); foreach ($conf['router'] as $router) { $index->{$router}[0]($router[1], $router[2]); } $app->mount($index); } // 设置数据库连接 $di->set('db', function () use($config) { $config = $config->get('database')->toArray(); $dbAdapter = '\\Phalcon\\Db\\Adapter\\Pdo\\' . $config['adapter']; $connection = new $dbAdapter($config); return $connection; }); // 设置配置 $di->set('config', function () use($config) { return $config;
<?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;
/** * @return array * @throws \Exception */ public function getCollections() { /** @var Config $collectionConfig */ $collectionConfig = $this->getDI()->get('collectionConfig'); $collections = []; if (!$collectionConfig) { return []; } foreach ($collectionConfig->getRequiredArray('versions') as $version => $entitys) { foreach ($entitys as $entityName => $entity) { $collection = new PhalconCollection(); $collection->setPrefix(sprintf('/%s/%s', strtolower($version), strtolower($entityName))); $collection->setHandler(sprintf('\\%s\\%s\\Controllers\\%s\\%sController', $collectionConfig->getRequiredString('namespace'), $version, $entityName, $entityName)); $collection->setLazy(true); foreach ($entity as $requestMethod => $actions) { foreach ($actions as $actionName => $action) { $validMethod = in_array(strtoupper($requestMethod), RequestMethodEnum::getConstants()); if (!$validMethod) { throw new \Exception("Invalid request method in the config file: '{$requestMethod}'"); } $requestMethod = strtolower($requestMethod); $collection->{$requestMethod}($action, $actionName); } } $collections[] = $collection; } } return $collections; }
<?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;
<?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;