public function testApplicationModulesDefinitionClosure() { // Creates the autoloader $loader = new \Phalcon\Loader(); $loader->registerNamespaces(array('Frontend\\Controllers' => __DIR__ . '/modules/frontend/controllers/', 'Backend\\Controllers' => __DIR__ . '/modules/backend/controllers/')); $loader->register(); $_GET['_url'] = '/login'; Phalcon\DI::reset(); $di = new Phalcon\DI\FactoryDefault(); $di->set('router', function () { $router = new Phalcon\Mvc\Router(false); $router->add('/index', array('controller' => 'index', 'module' => 'frontend', 'namespace' => 'Frontend\\Controllers\\')); $router->add('/login', array('controller' => 'login', 'module' => 'backend', 'namespace' => 'Backend\\Controllers\\')); return $router; }); $application = new Phalcon\Mvc\Application(); $view = new \Phalcon\Mvc\View(); $application->registerModules(array('frontend' => function ($di) use($view) { $di->set('view', function () use($view) { $view = new \Phalcon\Mvc\View(); $view->setViewsDir(__DIR__ . '/modules/frontend/views/'); return $view; }); }, 'backend' => function ($di) use($view) { $di->set('view', function () use($view) { $view->setViewsDir(__DIR__ . '/modules/backend/views/'); return $view; }); })); $application->setDi($di); $this->assertEquals($application->handle()->getContent(), '<html>here</html>' . PHP_EOL); $loader->unregister(); }
/** * Register the services here to make them general or register in the ModuleDefinition to make them module-specific */ protected function _registerServices() { $config = (include __DIR__ . "/../apps/config/config.php"); $di = new \Phalcon\DI\FactoryDefault(); $loader = new \Phalcon\Loader(); /** * We're a registering a set of directories taken from the configuration file */ $loader->registerDirs(array($config->application->libraryDir, $config->application->pluginDir))->register(); //Registering a router $di->set('router', function () { $router = new \Phalcon\Mvc\Router(); $router->setDefaultModule("frontend"); $router->add('/:controller/:action', array('module' => 'frontend', 'controller' => 1, 'action' => 2)); $router->add("/cats/index", array('module' => 'frontend', 'controller' => 'categories', 'action' => 'index')); $router->add("/cat/:params", array('module' => 'frontend', 'controller' => 'categories', 'action' => 'cat', 'params' => 1)); $router->add("/ask/:params", array('module' => 'frontend', 'controller' => 'ask', 'action' => 'ask', 'params' => 1)); $router->add("/admin/:controller/:action/:params", array('module' => 'backend', 'controller' => 1, 'action' => 2, 'params' => 3)); return $router; }); $di->set('db', function () use($config) { $mysql = new \Phalcon\Db\Adapter\Pdo\Mysql(array("host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->name)); $mysql->query("set names 'utf8'"); return $mysql; }); $di->set('volt', function ($view, $di) use($config) { $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di); $volt->setOptions(array('compiledPath' => $config->volt->path, 'compiledExtension' => $config->volt->extension, 'compiledSeparator' => $config->volt->separator, 'stat' => (bool) $config->volt->stat)); return $volt; }); //Set the views cache service $di->set('viewCache', function () { //Cache data for one day by default $frontCache = new Phalcon\Cache\Frontend\Output(array("lifetime" => 86400)); //Memcached connection settings $cache = new Phalcon\Cache\Backend\File($frontCache, array("cacheDir" => "../apps/caches/")); return $cache; }); /** * If the configuration specify the use of metadata adapter use it or use memory otherwise */ $di->set('modelsMetadata', function () use($config) { if (isset($config->models->metadata)) { $metaDataConfig = $config->models->metadata; $metadataAdapter = 'Phalcon\\Mvc\\Model\\Metadata\\' . $metaDataConfig->adapter; return new $metadataAdapter(); } else { return new Phalcon\Mvc\Model\Metadata\Memory(); } }); //Start the session the first time some component request the session service $di->set('session', function () { $session = new Phalcon\Session\Adapter\Files(); $session->start(); return $session; }); $this->setDI($di); }
/** * Register the services here to make them general or register in * the ModuleDefinition to make them module-specific. */ protected function _registerServices() { $loader = new \Phalcon\Loader(); /** * We're a registering a set of directories taken from the configuration file */ /* $loader->registerDirs( array( __DIR__ . '/../library/', __DIR__ . '/../vendor/', ) )->register(); */ // Init a DI $di = new \Phalcon\DI\FactoryDefault(); // Registering a router: $defaultModule = self::DEFAULT_MODULE; $modules = self::$modules; $di->set('router', function () use($defaultModule, $modules) { $router = new \Phalcon\Mvc\Router(); $router->setDefaultModule($defaultModule); foreach ($modules as $moduleName => $module) { // do not route default module if ($defaultModule == $moduleName) { continue; } $router->add('#^/' . $moduleName . '(|/)$#', array('module' => $moduleName, 'controller' => 'index', 'action' => 'index')); $router->add('#^/' . $moduleName . '/([a-zA-Z0-9\\_]+)[/]{0,1}$#', array('module' => $moduleName, 'controller' => 1)); $router->add('#^/' . $moduleName . '[/]{0,1}([a-zA-Z0-9\\_]+)/([a-zA-Z0-9\\_]+)(/.*)*$#', array('module' => $moduleName, 'controller' => 1, 'action' => 2, 'params' => 3)); } return $router; }); /** * The URL component is used to generate all kind of urls in the application */ $di['url'] = function () { $url = new \Phalcon\Mvc\Url(); $url->setBaseUri('/'); return $url; }; /** * Start the session the first time some component request the session service */ $di['session'] = function () { $session = new \Phalcon\Session\Adapter\Files(); $session->start(); return $session; }; $this->setDI($di); }
/** * Register the services here to make them general or register in the ModuleDefinition to make them module-specific */ protected function _registerServices() { $di = new \Phalcon\DI\FactoryDefault(); $loader = new \Phalcon\Loader(); /** * We're a registering a set of directories taken from the configuration file */ $loader->registerDirs(array(__DIR__ . '/../apps/library/'))->register(); //Registering a router $di->set('router', function () { $router = new \Phalcon\Mvc\Router(); $router->setDefaultModule("frontend"); $router->add('/:controller/:action', array('module' => 'frontend', 'controller' => 1, 'action' => 2)); $router->add("/login", array('module' => 'backend', 'controller' => 'login', 'action' => 'index')); $router->add("/admin/products/:action", array('module' => 'backend', 'controller' => 'products', 'action' => 1)); $router->add("/products/:action", array('module' => 'frontend', 'controller' => 'products', 'action' => 1)); return $router; }); $this->setDI($di); }
public function testUrl() { Phalcon\DI::reset(); //Create a default DI $di = new Phalcon\DI(); $di['router'] = function () { $router = new \Phalcon\Mvc\Router(FALSE); $router->add('/:controller/:action', array('controller' => 1, 'action' => 2))->setHostName('phalconphp.com')->setName('test'); return $router; }; $di->set('url', function () { $url = new Phalcon\Mvc\Url(); $url->setBaseUri('/'); return $url; }); $url = $di->url->get(array('for' => 'test', 'hostname' => true, 'controller' => 'index', 'action' => 'test')); $this->assertEquals($url, 'phalconphp.com/index/test'); }
public static function initSystemService() { global $di; //读取配置项 $config = (require CONFIG_PATH . "/config.php"); $di->setShared('config', function () use($config) { return $config; }); //设置master数据库 $di->setShared('dbMaster', function () use($config) { return new \Phalcon\Db\Adapter\Pdo\Mysql($config->dbMaster->toArray()); }); //设置slave1数据库 $di->setShared('dbSlave1', function () use($config) { return new \Phalcon\Db\Adapter\Pdo\Mysql($config->dbSlave1->toArray()); }); //设置slave2数据库 $di->setShared('dbSlave2', function () use($config) { return new \Phalcon\Db\Adapter\Pdo\Mysql($config->dbSlave2->toArray()); }); //设置redis缓存 $di->setShared('redis', function () use($config) { $frontCache = new \Phalcon\Cache\Frontend\Data($config->cache_life->toArray()); return new Phalcon\Cache\Backend\Redis($frontCache, $config->redis->toArray()); }); //设置Beanstalk队列 $di->setShared('queue', function () use($config) { return new Phalcon\Queue\Beanstalk($config->beanstalk->toArray()); }); //设置session $di->setShared('session', function () { $session = new \Phalcon\Session\Adapter\Files(); $session->start(); return $session; }); //设置router $di->set('router', function () { $router = new \Phalcon\Mvc\Router(); $router->setDefaultModule("home"); $router->add('/:module/:controller/:action', array('module' => 1, 'controller' => 2, 'action' => 3)); return $router; }); }
/** * @dataProvider hostnamedRegexRoutesProvider */ public function testHostnameRegexRouteGroup($actualHost, $expectedHost, $controller) { Phalcon\Mvc\Router\Route::reset(); $di = new Phalcon\DI(); $di->set('request', function () { return new Phalcon\Http\Request(); }); $router = new Phalcon\Mvc\Router(false); $router->setDI($di); $router->add('/edit', array('controller' => 'posts3', 'action' => 'edit3')); $group = new Phalcon\Mvc\Router\Group(); $group->setHostname('([a-z]+).phalconphp.com'); $group->add('/edit', array('controller' => 'posts', 'action' => 'edit')); $router->mount($group); $_SERVER['HTTP_HOST'] = $actualHost; $router->handle('/edit'); $this->assertEquals($router->getControllerName(), $controller); $this->assertEquals($router->getMatchedRoute()->getHostname(), $expectedHost); }
private static function cgiRegister() { $di = get_app_di(); $config = self::getConfig(); self::registCommonService($di, $config); $di->setShared('router', function () { $router = new \Phalcon\Mvc\Router(); $router->removeExtraSlashes(true); $router->add('/:controller/:action/:params', array('controller' => 1, 'action' => 2, 'params' => 3)); return $router; }); $di->setShared('dispatcher', function () { $eventsManager = new \Phalcon\Events\Manager(); $eventsManager->attach('dispatch:beforeException', new NotFoundPlugin()); $dispatcher = new \Phalcon\Mvc\Dispatcher(); $dispatcher->setEventsManager($eventsManager); $dispatcher->setDefaultNamespace("App\\Controllers\\"); return $dispatcher; }); $di->setShared('view', function () use($config) { $view = new \Phalcon\Mvc\View(); $view->setViewsDir($config->view->templatePath); $view->registerEngines(array('.html' => function ($view, $di) { $config = $di->get('config'); $compiledPath = $config->view->compiledPath; if (!file_exists($compiledPath)) { mkdir($compiledPath, 0744, true); } $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di); $volt->setOptions(array('compiledPath' => $compiledPath, 'compiledExtension' => $config->view->compiledExtension, 'compileAlways' => isset($config->view->compileAlways) ?: false)); $compiler = $volt->getCompiler(); $compiler->addExtension(new VoltExtension()); $autoEscape = isset($config->view->autoEscape) ?: true; ClassUtil::modifyPrivateProperties($compiler, array('_autoescape' => $autoEscape)); return $volt; })); return $view; }); $di->setShared('elements', function () { return new ElementsPlugin(); }); }
public function testHostnameRegexRouteGroup() { $this->specify("Router Groups with regular expressions don't work properly", function ($actualHost, $expectedHost, $controller) { \Phalcon\Mvc\Router\Route::reset(); $di = new \Phalcon\DI(); $di->set("request", function () { return new \Phalcon\Http\Request(); }); $router = new \Phalcon\Mvc\Router(false); $router->setDI($di); $router->add("/edit", ["controller" => "posts3", "action" => "edit3"]); $group = new \Phalcon\Mvc\Router\Group(); $group->setHostname("([a-z]+).phalconphp.com"); $group->add("/edit", ["controller" => "posts", "action" => "edit"]); $router->mount($group); $_SERVER["HTTP_HOST"] = $actualHost; $router->handle("/edit"); expect($router->getControllerName())->equals($controller); expect($router->getMatchedRoute()->getHostname())->equals($expectedHost); }, ["examples" => $this->hostnamedRegexRoutesProvider()]); }
/** * Start the session the first time some component request the session service */ $di->setShared('session', function () { $session = new SessionAdapter(); session_name('sessionIGO'); $session->start(); return $session; }); /** * Ajout du routing pour le navigateur construit, en utilisant les paramètres REST plutot que KVP. */ $di->set('router', function () { $router = new \Phalcon\Mvc\Router(); //Define a route $router->add("/contexte/{contexte}", array("controller" => "igo", "action" => "contexte", "contexteid" => 1)); $router->add("/configuration/{configuration}", array("controller" => "igo", "action" => "configuration", "configuration" => 1)); $router->add("/couche/{coucheId}", array("controller" => "igo", "action" => "couche", "coucheid" => 1)); $router->add("/groupe/{groupeId}", array("controller" => "igo", "action" => "groupe", "coucheid" => 1)); $router->setDefaults(array('controller' => 'index', 'action' => 'index')); return $router; }); if (isset($config->application->authentification->module)) { $authentificationModule = new $config->application->authentification->module(); if ($authentificationModule instanceof AuthentificationController) { $di->set("authentificationModule", $authentificationModule); } else { error_log("Le module d'authentificaiton n'est pas une instance d'AuthentificationController"); } } else { $di->set("authentificationModule", 'AuthentificationTest');
return $session; }); //Specify routes for modules $di->setShared('router', function () use($config, $routers) { $router = new \Phalcon\Mvc\Router(false); $router->clear(); $router->removeExtraSlashes(true); $router->clear(); $router->setDefaultModule($config->app->defaultApp); $router->setDefaultController($config->app->defaultController); $router->setDefaultAction($config->app->defaultAction); if (!empty($routers)) { foreach ($routers as $name => $rule) { $pattern = $rule->pattern; unset($rule->pattern); $router->add($pattern, $rule->toArray())->setName($name); } } return $router; }); $di->setShared('db', function () use($config) { $connection = new \Phalcon\Db\Adapter\Pdo\Mysql(array("host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->name, "charset" => $config->database->charset, "collation" => $config->database->collation)); return $connection; }); $di->set('modelsManager', new \Phalcon\Mvc\Model\Manager()); $di->setShared('url', function () use($config) { $url = new \Phalcon\Mvc\Url(); $url->setBaseUri($config->app->base); return $url; }); try {
<?php $router = new \Phalcon\Mvc\Router(); $router->add("/:controller/:action/:params", array("controller" => 1, "action" => 2, "params" => 3)); return $router;
<?php /* * Define custom routes. File gets included in the router service definition. */ $router = new \Phalcon\Mvc\Router(); //Remove trailing slashes automatically $router->removeExtraSlashes(true); $router->add('/([a-zA-Z\\-]+)/([a-zA-Z\\-]+)/:params', array('controller' => 1, 'action' => 2, 'params' => 3))->convert('action', function ($action) { return Phalcon\Text::camelize($action); }); $router->add('/signup', array('namespace' => 'Talon\\Controllers', 'controller' => 'session', 'action' => 'signup'))->setName('session-signup'); $router->add('/login', array('namespace' => 'Talon\\Controllers', 'controller' => 'session', 'action' => 'login'))->setName('session-login'); $router->add('/forgot-password', array('namespace' => 'Talon\\Controllers', 'controller' => 'session', 'action' => 'forgotPassword'))->setName('session-forgotPassword'); $router->add('/reset-password/{code}/{email}', array('controller' => 'users_control', 'action' => 'resetPassword'))->setName('session-resetPassword'); $router->add('/confirm/{code}/{email}', array('controller' => 'users_control', 'action' => 'confirmEmail'))->setName('users_control-confirmEmail'); $router->add('/resend-confirmation/{email}', array('controller' => 'users_control', 'action' => 'resendConfirmation'))->setName('users_control-resendConfirmation'); return $router;
<?php // 如果不使用默认的路由规则,请传入参数false // 此时必需完全匹配路由表,否则调用默认的index/index $router = new Phalcon\Mvc\Router(); // 如果URL以/结尾,删除这个/ $router->removeExtraSlashes(false); $router->add('/', ['controller' => 'home', 'action' => 'index']); // Not Found Paths $router->notFound(['controller' => 'index', 'action' => 'show404']); return $router;
<?php $di['router'] = function () use($modules, $di) { $defaultModule = 'frontend'; $router = new \Phalcon\Mvc\Router(false); $router->clear(); $moduleRouting = ROOT_DIR . 'core/modules/' . $defaultModule . '/config/routing.php'; if (file_exists($moduleRouting) && is_file($moduleRouting)) { include $moduleRouting; } else { $router->add('#^/(|/)$#', array('module' => $defaultModule, 'controller' => 'index', 'action' => 'index')); $router->add('#^/([a-zA-Z0-9\\_]+)[/]{0,1}$#', array('module' => $defaultModule, 'controller' => 1)); $router->add('#^/{0,1}([a-zA-Z0-9\\_]+)/([a-zA-Z0-9\\_]+)(/.*)*$#', array('module' => $defaultModule, 'controller' => 1, 'action' => 2, 'params' => 3)); } foreach ($modules as $moduleName => $module) { if ($defaultModule == $moduleName) { continue; } if (isset($module['router'])) { $routing = $module['router']; } else { $routing = ROOT_DIR . 'core/modules/' . $moduleName . '/config/routing.php'; } if (file_exists($routing) && is_file($routing)) { include $routing; } } return $router; };
<?php $router = new Phalcon\Mvc\Router(true); $router->setDefaultModule("frontend"); $router->removeExtraSlashes(TRUE); $router->add('/:controller/:action[/]{0,1}', array('module' => 'frontend', 'controller' => 1, 'action' => 2, 'module' => 'frontend', 'namespace' => 'reportingtool\\Modules\\Modules\\Frontend\\Controllers')); $router->add('/{language:[a-z]{2}}/:controller[/]{0,1}', array('language' => 1, 'controller' => 2, 'action' => "index", 'module' => 'frontend', 'namespace' => 'reportingtool\\Modules\\Modules\\Frontend\\Controllers')); $router->add('/{language:[a-z]{2}}/:controller/:action[/]{0,1}', array('language' => 1, 'controller' => 2, 'action' => 3, 'module' => 'frontend', 'namespace' => 'reportingtool\\Modules\\Modules\\Frontend\\Controllers')); $router->add('/{language:[a-z]{2}}/:controller/:action/:int[/]{0,1}', array('language' => 1, 'controller' => 2, 'action' => 3, 'uid' => 4, 'module' => 'frontend', 'namespace' => 'reportingtool\\Modules\\Modules\\Frontend\\Controllers')); $router->add('/', array('controller' => 'index', 'action' => 'index', 'module' => 'frontend', 'namespace' => 'reportingtool\\Modules\\Modules\\Frontend\\Controllers')); $router->add('/session/index/', array('controller' => 'session', 'action' => 'index', 'module' => 'frontend', 'namespace' => 'reportingtool\\Modules\\Modules\\Frontend\\Controllers')); $router->add('/session/start[/]{0,1}', array('controller' => 'session', 'action' => 'start', 'module' => 'frontend', 'namespace' => 'reportingtool\\Modules\\Modules\\Frontend\\Controllers')); $router->add('/session/logout[/]{0,1}', array('controller' => 'session', 'action' => 'logout', 'module' => 'frontend', 'namespace' => 'reportingtool\\Modules\\Modules\\Frontend\\Controllers')); $router->add('/backend/{language:[a-z]{2}}/:controller[/]{0,1}', array('language' => 1, 'controller' => 2, 'action' => "index", 'module' => 'backend', 'namespace' => 'reportingtool\\Modules\\Modules\\Backend\\Controllers')); $router->add('/backend/{language:[a-z]{2}}/:controller/:action[/]{0,1}', array('language' => 1, 'controller' => 2, 'action' => 3, 'module' => 'backend', 'namespace' => 'reportingtool\\Modules\\Modules\\Backend\\Controllers')); $router->add('/backend/{language:[a-z]{2}}/:controller/:action/:int[/]{0,1}', array('language' => 1, 'controller' => 2, 'action' => 3, 'uid' => 4, 'module' => 'backend', 'namespace' => 'reportingtool\\Modules\\Modules\\Backend\\Controllers')); $router->add('/backend', array('controller' => 'index', 'action' => 'index', 'module' => 'backend', 'namespace' => 'reportingtool\\Modules\\Modules\\Backend\\Controllers')); $router->handle(); return $router;
public function testRegex() { Phalcon\Mvc\Router\Route::reset(); $router = new Phalcon\Mvc\Router(false); $router->add('/:controller/:action/:params', array("controller" => 1, "action" => 2, "params" => 3)); $router->handle('/c/a/p'); $this->assertEquals($router->getMatches(), array(0 => '/c/a/p', 1 => 'c', 2 => 'a', 3 => '/p')); Phalcon\Mvc\Router\Route::reset(); $router = new Phalcon\Mvc\Router(false); $router->add('/:controller/:action:params', array("controller" => 1, "action" => 2, "params" => 3), array(':controller' => '([a-zA-Z0-9_-]+)', ':action' => '([a-zA-Z0-9_-]+)', ':params' => '(/[a-zA-Z0-9_-]+)?')); $router->handle('/c/a/p'); $this->assertEquals($router->getMatches(), array(0 => '/c/a/p', 1 => 'c', 2 => 'a', 3 => '/p')); Phalcon\Mvc\Router\Route::reset(); $router = new Phalcon\Mvc\Router(false); $router->add(':controller:action:params', array("controller" => 1, "action" => 2, "params" => 3), array(':controller' => '/([a-zA-Z0-9_-]+)', ':action' => '/([a-zA-Z0-9_-]+)', ':params' => '(/[a-zA-Z0-9_-]+)?')); $router->handle('/c/a/p'); $this->assertEquals($router->getMatches(), array(0 => '/c/a/p', 1 => 'c', 2 => 'a', 3 => '/p')); Phalcon\Mvc\Router\Route::reset(); $router = new Phalcon\Mvc\Router(false); $router->add('/(:controller(/:action(/:params)?)?)?', array("controller" => 2, "action" => 4, "params" => 5), array(':controller' => '([a-zA-Z0-9_-]+)', ':action' => '([a-zA-Z0-9_-]+)', ':params' => '([a-zA-Z0-9_-]+)?')); $router->handle('/c/a/p'); $this->assertEquals($router->getMatches(), array(0 => '/c/a/p', 1 => 'c/a/p', 2 => 'c', 3 => '/a/p', 4 => 'a', 5 => '/p', 6 => 'p')); $router->handle('/c/a'); $this->assertEquals($router->getMatches(), array(0 => '/c/a', 1 => 'c/a', 2 => 'c', 3 => '/a', 4 => 'a')); $router->handle('/c'); $this->assertEquals($router->getMatches(), array(0 => '/c', 1 => 'c', 2 => 'c')); }
Host\Object\ExtenderCore::SyncProjectFolder(); } //////////////////////////// // VIEW SETUP $viewDirArray = array(); //////////////////////////// // ROOT $di->set('url', function () { $url = new \Phalcon\Mvc\Url(); $url->setBaseUri('/'); return $url; }); //////////////////////////// // ROUTER $router->setDefaults(array('controller' => 'index', 'action' => 'index', 'namespace' => 'Host\\Controller')); $router->add("/extender", array('controller' => 'extender', 'action' => 'index', 'path' => $_GET['path'], 'namespace' => 'Host\\Controller')); $router->add("/extender/extend", array('controller' => 'extender', 'action' => 'index', 'path' => $_GET['path'], 'namespace' => 'Host\\Controller')); $router->notFound(array('controller' => 'index', 'action' => 'notFound', 'namespace' => 'Host\\Controller')); //////////////////////////// // BIND VIEW Twig_Autoloader::register(); $di['twigService'] = function ($view, $di) { Phalcon\Mvc\View\Engine\Twig::setOptions(array('debug' => true, 'charset' => 'UTF-8', 'base_template_class' => 'Twig_Template', 'strict_variables' => false, 'autoescape' => false, 'cache' => __DIR__ . '/../../cache/twig/', 'auto_reload' => null, 'optimizations' => -1)); $twig = new View($view, $di); return $twig; }; $di->set('twigService', function ($view, $di) use($config) { // create twig object $option = array('cache' => '../cache/'); $arrFolder = array('../views/'); $twig = new \Phalcon\Mvc\View\Engine\Twig($view, $di, $options, $arrFolder);
public function testHostnameRegexRouteGroup() { Phalcon\Mvc\Router\Route::reset(); $di = new Phalcon\DI(); $di->set('request', function () { return new Phalcon\Http\Request(); }); $router = new Phalcon\Mvc\Router(false); $router->setDI($di); $router->add('/edit', array('controller' => 'posts3', 'action' => 'edit3')); $group = new Phalcon\Mvc\Router\Group(); $group->setHostname('([a-z]+).phalconphp.com'); $group->add('/edit', array('controller' => 'posts', 'action' => 'edit')); $router->mount($group); $routes = array(array('hostname' => 'localhost', 'controller' => 'posts3'), array('hostname' => 'my.phalconphp.com', 'controller' => 'posts'), array('hostname' => null, 'controller' => 'posts3')); foreach ($routes as $route) { $_SERVER['HTTP_HOST'] = $route['hostname']; $router->handle('/edit'); $this->assertEquals($router->getControllerName(), $route['controller']); } }
<?php date_default_timezone_set('America/Mexico_City'); ini_set('display_errors', true); error_reporting(E_ALL); $di = new \Phalcon\DI\FactoryDefault(); $di->set('url', function () { $url = new \Phalcon\Mvc\Url(); $url->setBaseUri("http://" . $_SERVER["SERVER_NAME"] . "/"); return $url; }); $di->set('router', function () { $router = new \Phalcon\Mvc\Router(); $router->setDefaultModule("frontend"); $router->add("/", array('module' => 'frontend', 'controller' => 'index', 'action' => 'index')); $router->add("/contactanos", array('module' => 'frontend', 'controller' => 'index', 'action' => 'contactanos')); $router->add("/token", array('module' => 'frontend', 'controller' => 'index', 'action' => 'token')); $router->notFound(array('module' => 'frontend', 'controller' => 'index', 'action' => 'show404')); /* Dashboard */ $router->add("/dashboard", array('module' => 'dashboard', 'controller' => 'index', 'action' => 'index')); $router->add("/login", array('module' => 'dashboard', 'controller' => 'login', 'action' => 'index')); $router->add("/logout", array('module' => 'dashboard', 'controller' => 'login', 'action' => 'logout')); $router->add('/dashboard/([a-zA-Z\\-]+)/([a-zA-Z\\-]+)', array('module' => 'dashboard', 'controller' => 1, 'action' => 2))->setName("controllers")->convert('action', function ($action) { return \Phalcon\Text::lower(\Phalcon\Text::camelize($action)); }); $router->removeExtraSlashes(true); return $router; }); /** * Start the session the first time some component request the session service */
<?php $router = new Phalcon\Mvc\Router(); //Remove trailing slashes automatically $router->removeExtraSlashes(true); //SECURE ROUTES $router->add('/login', 'Secure::login'); $router->addGet('/sign-up', 'Secure::signUp'); $router->addGet('/remind-password', 'Secure::remindPassword'); $router->addGet('/logout', 'Secure::logout'); $router->addPost('/register', 'Secure::register'); $router->add('/plates/{id}', 'Requests::plates'); //testing porpuses $router->addGet('/test', 'Index::test'); //Requests section $router->addGet('/requests', 'Requests::index'); $router->addGet('/requests/history', 'Requests::history'); $router->addPost('/requests/create', 'Requests::create'); $router->addPost('/requests/view/{id}', 'Requests::view'); $router->addGet('/requests/status/{id}/{id_status}', 'Requests::changeStatus'); //Cart $router->addPost('/add-cart-item', 'Cart::add'); $router->addGet('/add-cart-item', 'Index::home'); $router->addGet('/cart', 'Cart::view'); $router->add('/cart-delete/{id}', 'Cart::delete'); $router->add('/delete-cart', 'Cart::deleteCart'); //Inbox section $router->addGet('/inbox', 'Inbox::index'); //Reports section $router->addGet('/reports', 'Reports::index'); $router->addGet('/reports/history', 'Reports::history');
<?php $router = new Phalcon\Mvc\Router(); $router->add('/confirm/{code}/{email}', array('controller' => 'session', 'action' => 'confirmEmail')); return $router;
/* +------------------------------------------------------------------------+ | Phosphorum | +------------------------------------------------------------------------+ | Copyright (c) 2013-2014 Phalcon Team and contributors | +------------------------------------------------------------------------+ | This source file is subject to the New BSD License that is bundled | | with this package in the file docs/LICENSE.txt. | | | | If you did not receive a copy of the license and are unable to | | obtain it through the world-wide-web, please send an email | | to license@phalconphp.com so we can send you a copy immediately. | +------------------------------------------------------------------------+ */ $router = new Phalcon\Mvc\Router(false); $router->add('/sitemap', array('controller' => 'sitemap', 'action' => 'index')); $router->add('/help/stats', array('controller' => 'help', 'action' => 'stats')); $router->add('/help/about', array('controller' => 'help', 'action' => 'about')); $router->add('/help/moderators', array('controller' => 'help', 'action' => 'moderators')); $router->add('/help/voting', array('controller' => 'help', 'action' => 'voting')); $router->add('/help/markdown', array('controller' => 'help', 'action' => 'markdown')); $router->add('/help/karma', array('controller' => 'help', 'action' => 'karma')); $router->add('/help/badges', array('controller' => 'help', 'action' => 'badges')); $router->add('/help/create-post', array('controller' => 'help', 'action' => 'create')); $router->add('/help', array('controller' => 'help', 'action' => 'index')); $router->add('/index.html', array('controller' => 'discussions', 'action' => 'index')); $router->add('/discussions', array('controller' => 'discussions', 'action' => 'index')); $router->add('/hook/mail-bounce', array('controller' => 'hooks', 'action' => 'mailBounce')); $router->add('/hook/mail-reply', array('controller' => 'hooks', 'action' => 'mailReply')); $router->add('/search', array('controller' => 'discussions', 'action' => 'search')); $router->add('/settings', array('controller' => 'discussions', 'action' => 'settings'));
<?php $di->set('router', function () { $router = new \Phalcon\Mvc\Router(); //Define a route $router->add('/login', array('controller' => 'member', 'action' => 'login')); $router->add('/logout', array('controller' => 'member', 'action' => 'logout')); $router->add('/signup', array('controller' => 'member', 'action' => 'signup')); $router->add('member', array('controller' => 'member', 'action' => 'page')); //Define admin route $router->add('/admin/:controller', array('controller' => 1, 'action' => 'admin_index')); $router->add('/admin/:controller/view/:params', array('controller' => 1, 'action' => 'admin_view', 'params' => 2)); $router->add('/admin/:controller/add', array('controller' => 1, 'action' => 'admin_add')); $router->add('/admin/:controller/pending/:params', array('controller' => 1, 'action' => 'admin_pending', 'params' => 2)); $router->add('/admin/login', array('controller' => 'user', 'action' => 'admin_login')); $router->add('/admin/logout', array('controller' => 'user', 'action' => 'admin_logout')); $router->add('/admin', array('controller' => 'user', 'action' => 'admin_login')); $router->add('/admin/:controller/login', array('controller' => 'user', 'action' => 'admin_login')); $router->add('/admin/user/login', array('controller' => 'user', 'action' => 'admin_login')); $router->add('/advertiser/login', array('controller' => 'member', 'action' => 'advertiser_login')); $router->add('/advertiser/signup', array('controller' => 'member', 'action' => 'advertiser_signup')); $router->add('/advertiser/emailConfimation/:params/:params/:params', array('controller' => 'member', 'action' => 'advertiser_emailConfimation', 'params' => 1, 'params' => 2, 'params' => 3)); $router->add('/search', array('controller' => 'index', 'action' => 'search')); return $router; });
<?php /* * Define custom routes. File gets included in the router service definition. */ $router = new Phalcon\Mvc\Router(); $router->add('/restore-password', ['controller' => 'signin', 'action' => 'restorePassword']); $router->add('/new-password', ['controller' => 'profile', 'action' => 'newPassword']); $router->add('/change-password', ['controller' => 'profile', 'action' => 'changePassword']); $router->add('/language/{language}', ['controller' => 'language', 'action' => 'index']); $userManagement = new \Phalcon\Mvc\Router\Group(['controller' => 'usermanagement']); $userManagement->setPrefix('/manage-users'); $userManagement->add('', ['action' => 'index']); $userManagement->add('/email', ['action' => 'sendEmail']); $router->mount($userManagement); return $router;
/** * Registering a dispatcher */ $di->set('dispatcher', function () { $dispatcher = new \Phalcon\Mvc\Dispatcher(); $dispatcher->setDefaultNamespace('Tools\\Controllers'); return $dispatcher; }); /** * Register routers */ $di->setShared('router', function () use($config) { $router = new \Phalcon\Mvc\Router(); $router->removeExtraSlashes(true); $router->setDefaults(array('namespace' => 'Tools\\Controllers', 'controller' => 'index', 'action' => 'index')); $router->add('/:controller/:action/:params', array('namespace' => 'Tools\\Controllers', 'controller' => 1, 'action' => 2, 'params' => 3)); return $router; }); /** * Register assets that will be loaded in every page */ $di->setShared('assets', function () { $assets = new \Phalcon\Assets\Manager(); $assets->collection('header-js')->addJs('js/jquery-1.11.3.min.js')->addJs('js/jquery-ui.min.js')->addJs('js/bootstrap.min.js')->addJs('js/mg.js'); $assets->collection('header-css')->addCss('css/jquery-ui.min.css')->addCss('css/bootstrap.min.css')->addCss('css/style.css'); return $assets; }); /** * Register the flash service with custom CSS classes */ $di->setShared('flash', function () {
<?php $router = new Phalcon\Mvc\Router(false); $router->removeExtraSlashes(true); $router->setDefaults(array('controller' => 'apireference', 'action' => 'index')); $router->add('/error/404', array('controller' => 'error', 'action' => '_404'))->setName('404'); $router->notFound($router->getRouteByName('404')->getPaths()); // this only need to get URL to class reference by route name $router->add('/{language:[A-Za-z]{2,2}}/{version:((\\d+\\.\\d+\\.\\d+)|latest)}/{class:(?i)phalcon(/[\\w/]+)?}', array('controller' => 'apireference', 'action' => 'showClass'))->setName('showClass'); // this only need to get URL by route name $router->add('/{language:[A-Za-z]{2,2}}/{version:((\\d+\\.\\d+\\.\\d+)|latest)}/{type:(classes|namespaces|interfaces|changelog)}', array('controller' => 'apireference', 'action' => 'index'))->setName('showSummary'); $router->add('/?([A-Za-z]{2,2})?(/((\\d+\\.\\d+\\.\\d+)|latest))?(/(classes|namespaces|interfaces|changelog))?', array('controller' => 'apireference', 'action' => 'index', 'language' => 1, 'version' => 3, 'summary' => 6))->convert('language', function ($param) { return $param === 1 || !$param ? null : strtolower($param); })->convert('version', function ($param) { return $param === 3 || !$param ? null : $param; })->convert('summary', function ($param) { return $param === 6 || !$param ? 'classes' : $param; }); $router->add('(/([A-Za-z]{2,2}))?(/((\\d+\\.\\d+\\.\\d+)|latest))?/((?i)phalcon(/[\\w/]+)?)', array('controller' => 'apireference', 'action' => 'showClass', 'language' => 2, 'version' => 4, 'class' => 6))->convert('language', function ($param) { return $param === 2 || !$param ? null : strtolower($param); })->convert('version', function ($param) { return $param === 4 || !$param ? null : $param; })->convert('class', function ($param) { return $param === 6 || !$param ? null : str_replace('/', '\\', strtolower($param)); }); return $router;
<?php $router = new Phalcon\Mvc\Router(false); $router->add('/:controller/:action/:params', array('namespace' => 'MyApp\\Controllers', 'controller' => 1, 'action' => 2, 'params' => 3)); $router->add('/:controller', array('namespace' => 'MyApp\\Controllers', 'controller' => 1)); $router->add('/admin/:controller/:action/:params', array('namespace' => 'MyApp\\Controllers\\Admin', 'controller' => 1, 'action' => 2, 'params' => 3)); $router->add('/admin/:controller', array('namespace' => 'MyApp\\Controllers\\Admin', 'controller' => 1)); return $router;
<?php error_reporting(E_ALL); try { /** * The FactoryDefault Dependency Injector automatically register the right services providing a full stack framework */ $di = new \Phalcon\DI\FactoryDefault(); /** * Registering a router */ $di['router'] = function () { $router = new \Phalcon\Mvc\Router(false); $router->add('/admin', array('module' => 'backend', 'controller' => 'index', 'action' => 'index')); $router->add('/index', array('module' => 'frontend', 'controller' => 'index', 'action' => 'index')); $router->add('/', array('module' => 'frontend', 'controller' => 'index', 'action' => 'index')); return $router; }; /** * The URL component is used to generate all kind of urls in the application */ $di->set('url', function () { $url = new \Phalcon\Mvc\Url(); $url->setBaseUri('/mvc/multiple-shared-layouts/'); return $url; }); /** * Start the session the first time some component request the session service */ $di->set('session', function () { $session = new \Phalcon\Session\Adapter\Files();
protected function _registerServices() { $di = new \Phalcon\DI\FactoryDefault(); $loader = new \Phalcon\Loader(); $loader->registerDirs(array(FOLDER_PROJECT . '/apps/library/', FOLDER_PROJECT . '/apps/backend/models', FOLDER_PROJECT . '/apps/frontend/models'))->register(); //usando autoloader do composer para carregar as depêndencias instaladas via composer require_once FOLDER_PROJECT . 'vendor/autoload.php'; $di->set('router', function () { $router = new \Phalcon\Mvc\Router(); $router->setDefaultModule("frontend"); $router->add('/:controller/:action', array('module' => 'frontend', 'controller' => 1, 'action' => 2)); $router->add('/:controller/:action', array('module' => 'backend', 'controller' => 1, 'action' => 2)); $router->add("/admin", array('module' => 'backend', 'controller' => 'index', 'action' => 'index')); $router->add("/editor", array('module' => 'frontend', 'controller' => 'index', 'action' => 'index')); $router->add("/index/:action", array('controller' => 'index', 'action' => 1)); return $router; }); $di->set("libMail", function () { return new Multiple\Library\Mail(); }); /** * Caso exista o arquivo de configuração config.ini coleta os dados existentes nele e * conecta com o banco de dados */ if (file_exists('../apps/config/config.ini')) { $config = new \Phalcon\Config\Adapter\Ini('../apps/config/config.ini'); //Seta a conexão com o banco de dados $di->set('db', function () use($config) { $dbclass = 'Phalcon\\Db\\Adapter\\Pdo\\' . $config->database->adapter; return new $dbclass(array("host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->name, "charset" => 'utf8')); }); } $this->setDI($di); }