Is it possible to dynamically add routes after set router to the app? Something like this: $di->set('router', require (ROOT_PATH . '/common/config/routes.php')); ... ... ... $router = $di->get('router'); $router->add(...);
|
Mar '14 |
7 |
4752 |
3 |
Is it possible to dynamically add routes after set router to the app? Something like this: $di->set('router', require (ROOT_PATH . '/common/config/routes.php')); ... ... ... $router = $di->get('router'); $router->add(...);
eg: routes.php
$routes = array(
"/admin/users/my-profile" => array(
"controller" => "users",
"action" => "profile",
),
"/admin/users/change-password" => array(
"controller" => "users",
"action" => "changePassword",
)
);
return $route;
You can include that file
$routes = include('routes.php');
In di
$di->set('router', function(){
$router = new \Phalcon\Mvc\Router();
foreach($routes as $route=> $rull){
$router->add($route,$rull);
}
return $router;
});
@relot can you give us an example on how/why this is needed? What do you have in mind?
For me
/login -> controller => 'session', 'action' => 'login'
is pretty much static through the application. I don't understand the need to change it to say
/login -> controller => 'session', 'action' => 'otherlogin'
after the application is dispatched.
Am I missing something?
You can manage to accomplish to build routes from many route config file (you will dispatch in modules). I just started a Project with Phalcon, a new experience, I'm used to ZF2. I just wanted to reproduce a file structure I know. My project is structured like that :
/
/Config
/application.config.php
/Module
/Main
/config
/route.config.php
/src
/Conroller
/IndexController.php
/view
/Module.php
/Public
/index.php
/Vendor
in /Config/applicaiton.config.php
I put the module declaration as follow :
return array(
'module' => array(
'Main' => array(
'className' => 'Main\Module',
'path' => '../Module/Main/Module.php',
),
),
);
in Module/Main/config/route.config.php
Routes are described :
return array(
"home" => array(
"pattern" => "/",
"default" => array(
"module" => "Main",
"controller" => "Index",
"action" => "index",
),
),
);
That is the basic stucture I chose, but the challenge is to load routes before declaring Controllers in bootstrap index.php
. With the following bootstrap, routes will be loaded only for registered modules in Config/application.config.php
$di = new FactoryDefault();
// Import Application Configuration
$appConfig = new \Phalcon\Config(require __DIR__ . '/../Config/application.config.php');
// Include router config file : Module/ModuleName/config/route.config.php
// for each module
$routerConfig = new \Phalcon\Config();
foreach ($appConfig['module'] as $module) {
$dir = __DIR__ . "/" . preg_replace("/\/Module.php$/", "", $module['path']) . '/config';
if (is_dir($dir)) {
if (file_exists($dir . '/route.config.php')) {
$config = new \Phalcon\Config\Adapter\Php($dir . '/route.config.php');
$routerConfig->merge($config);
}
}
}
$routerConfig = $routerConfig->toArray();
// Set Router with merged config
$di->set('router', function() use ($routerConfig){
$router = new \Phalcon\Mvc\Router();
foreach ($routerConfig as $routeName => $routeDefinition) {
if (!array_key_exists('pattern', $routeDefinition)) {
throw new RuntimeException("Bad route definition, key 'pattern' is missing for route named '{$routeName}'");
}
if (!array_key_exists('default', $routeDefinition)) {
$routeDefinition['default'] = null;
}
if (!array_key_exists('verbs', $routeDefinition)) {
$routeDefinition['verbs'] = null;
}
$router->add($routeDefinition['pattern'], $routeDefinition['default'], $routeDefinition['verbs'])
->setName($routeName);
}
return $router;
});
//Create an application
$application = new Application($di);
// Register the installed modules
$application->registerModules($appConfig['module']->toArray());
Now that all routes for my modules are set, I delegate the bootstrap to the Module.php
concerned. This is a generic Module.php file, only the namespace must change.
<?php
namespace Main;
use Phalcon\Loader;
use Phalcon\Mvc\Dispatcher;
use Phalcon\Mvc\ModuleDefinitionInterface;
use Phalcon\Mvc\View;
class Module implements ModuleDefinitionInterface
{
public function registerAutoloaders($dependencyInjector) {
$loader = new Loader();
$loader->registerNamespaces(array(
__NAMESPACE__ => __DIR__ . "/src",
));
$loader->register();
}
public function registerServices($dependencyInjector) {
//Registering a dispatcher
$dependencyInjector->set('dispatcher', function() {
$dispatcher = new Dispatcher();
$dispatcher->setDefaultNamespace(__NAMESPACE__ . "\Controller");
return $dispatcher;
});
//Registering the view component
$dependencyInjector->set('view', function() {
$view = new View();
$view->setViewsDir(__DIR__ . "/view");
return $view;
});
}
}