From a8a448331e3c6c0e6572b5bfc410393c656b6a6d Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Mon, 18 Jun 2018 22:16:03 +0200 Subject: [PATCH] app/library/Services.php: load router settings from config file. --- app/config/routes.yml | 54 ++++++++++++++++++++++++++++++++ app/library/Services.php | 67 ++++++++++------------------------------ 2 files changed, 71 insertions(+), 50 deletions(-) create mode 100644 app/config/routes.yml diff --git a/app/config/routes.yml b/app/config/routes.yml new file mode 100644 index 0000000..d9fbe0b --- /dev/null +++ b/app/config/routes.yml @@ -0,0 +1,54 @@ + +router: + removeExtraSlashes: true + routes: + home-route: + pattern: '/' + path: + controller: index + action: index + about-route: + pattern: '/about' + path: + controller: index + action: about + cb-created: + pattern: '/callback/created/{id}' + path: + controller: callback + action: created + cb-endpoint: + pattern: '/cb/{id}' + path: + controller: callback + action: endpoint + login: + pattern: '/login' + path: + controller: auth + action: index + logout: + pattern: '/logout' + path: + controller: auth + action: logout + oauth: + pattern: '/login/{strategy:([a-z]+)}/:params' + path: + controller: auth + action: oauth + oauth-disconnect: + pattern: '/oauth/{provider:([a-z]+)}/disconnect' + path: 'User::oauthdisconnect' + oauth-disconnect-confirm: + pattern: '/oauth/{provider:([a-z]+)}/disconnect/{confirm}' + path: 'User::oauthdisconnect' + user-settings: + pattern: '/settings' + path: + controller: user + action: settings + activation-link: + path: + controller: user + action: activationlink diff --git a/app/library/Services.php b/app/library/Services.php index b3aa7ab..7d6e761 100644 --- a/app/library/Services.php +++ b/app/library/Services.php @@ -66,6 +66,7 @@ class Services extends DiDefault // will override settings if there a multiple keys. $files = array( 'app.yml', + 'routes.yml', 'local.yml' ); @@ -76,7 +77,8 @@ class Services extends DiDefault foreach ($files as $file) { try { - $config->merge(new Config($basePath . $file)); + $tmp = new Config($basePath . $file); + $config->merge($tmp); } catch(\Phalcon\Config\Exception $e) { // Sometime went wrong. Log here? } @@ -329,61 +331,26 @@ class Services extends DiDefault protected function _initSharedRouter() { + $config = $this->get('config')->router; + // Create the router $router = new Router(); - $router->removeExtraSlashes(true); + $router->removeExtraSlashes($config->get('removeExtraSlashes', false)); - $router->add('/', array( - 'controller' => 'index', - 'action' => 'index' - ))->setName('home-route'); + foreach($config->routes as $name => $def) { - // Route about page to index controller - $router->add('/about', array( - 'controller' => 'index', - 'action' => 'about' - ))->setName('about-route'); + if (!($def instanceof \Phalcon\Config)) { + continue; + } - $router->add('/callback/created/{id}', array( - 'controller' => 'callback', - 'action' => 'created', - ))->setName('cb-created'); - - // Route callback endpoints to callback controller. - $router->add('/cb/{id}', array( - 'controller' => 'callback', - 'action' => 'endpoint', - ))->setName('cb-endpoint'); - - // Login routes. - $router->add('/login', array( - 'controller' => 'auth', - 'action' => 'index', - ))->setName('login'); - - $router->add('/logout', array( - 'controller' => 'auth', - 'action' => 'logout', - ))->setName('logout'); - - $router->add('/login/{strategy:([a-z]+)}/:params', array( - 'controller' => 'auth', - 'action' => 'oauth' - ))->setName('oauth'); - - $router->add('/oauth/{provider:([a-z]+)}/disconnect', 'User::oauthdisconnect')->setName('oauth-disconnect'); - $router->add('/oauth/{provider:([a-z]+)}/disconnect/{confirm}', 'User::oauthdisconnect')->setName('oauth-disconnect-confirm'); - - $router->add('/settings', array( - 'controller' => 'user', - 'action' => 'settings', - ))->setName('user-settings'); - - $router->add('/act/{link}', array( - 'controller' => 'user', - 'action' => 'activationlink', - ))->setName('activation-link'); + $path = $def->get('path', []); + if ($path instanceof \Phalcon\Config) { + $path = $path->toArray(); + } + $router->add($def->get('pattern'), $path) + ->setName($name); + } return $router; }