/** * Attach dependencies to the DI container. * All DI Objects are attached here. The DI is not modified * outside of this method. * @return \Georeferencer\Application\Micro **/ private function bootstrapDi($appConfig) { $di = $this->getApplication()->getDI(); //configuration $di->setShared(self::DI_CONFIG, $appConfig); //circular $di->setShared(self::DI_APPLICATION, function () { return $this; }); //Set the DB. $di->setShared(self::DI_DB, function () use($appConfig) { return new \Phalcon\Db\Adapter\Pdo\Postgresql($appConfig['db']); }); //Router $router = new \Phalcon\Mvc\Router(); $router->setUriSource(\Phalcon\Mvc\Router::URI_SOURCE_SERVER_REQUEST_URI); $di->setShared(self::DI_ROUTER, $router); //Url Helper $di->setShared(self::DI_URL_HELPER, new \Phalcon\Mvc\Url()); //Listener $di->setShared(self::DI_REST_LISTENER, new \Georeferencer\Listener\Rest()); return $this; }
public function testUriSource() { Phalcon\Mvc\Router\Route::reset(); $_GET['_url'] = '/some/route'; $router = new Phalcon\Mvc\Router(false); $this->assertEquals($router->getRewriteUri(), '/some/route'); $router->setUriSource(Phalcon\Mvc\Router::URI_SOURCE_GET_URL); $this->assertEquals($router->getRewriteUri(), '/some/route'); $router->setUriSource(Phalcon\Mvc\Router::URI_SOURCE_SERVER_REQUEST_URI); $_SERVER['REQUEST_URI'] = '/some/route'; $this->assertEquals($router->getRewriteUri(), '/some/route'); $_SERVER['REQUEST_URI'] = '/some/route?x=1'; $this->assertEquals($router->getRewriteUri(), '/some/route'); }
<?php use Phalcon\Mvc\Dispatcher as MvcDispatcher, Phalcon\Mvc\Dispatcher\Exception as DispatchException, Phalcon\Events\Manager as EventsManager; include __DIR__ . '/../../../common/config/services.php'; error_reporting(7); /* @var $di Phalcon\DI\FactoryDefault */ $di->setShared('router', function () { // 如果不使用默认的路由规则,请传入参数false // 此时必需完全匹配路由表,否则调用默认的index/index $router = new Phalcon\Mvc\Router(); // 如果URL以/结尾,删除这个/ $router->removeExtraSlashes(false); // use $_SERVER['REQUEST_URI'] (default) $router->setUriSource($router::URI_SOURCE_SERVER_REQUEST_URI); // Not Found Paths $router->notFound(['controller' => 'index', 'action' => 'show404']); $router->add('/', ['controller' => 'home', 'action' => 'index']); return $router; }); /** * debug模式使用File存储,正式使用memcache,未来使用redis */ $di->setShared('cache', function () use($config) { // 默认15分钟 $frontCache = new \Phalcon\Cache\Frontend\Data(["lifetime" => 900]); if ($config->debug) { return new \Phalcon\Cache\Backend\File($frontCache, ["cacheDir" => __DIR__ . "/../cache/"]); } else { return new \Phalcon\Cache\Backend\Redis($frontCache, ["host" => $config->redis->host, "port" => $config->redis->port, 'persistent' => $config->redis->persistent, "prefix" => $config->redis->prefix]); } });
<?php $router = new \Phalcon\Mvc\Router(); $router->setUriSource(\Phalcon\Mvc\Router::URI_SOURCE_SERVER_REQUEST_URI); $router->removeExtraSlashes(TRUE); /*$router->notFound(array( "module" => "frontend", "controller" => "index", "action" => "route404" )); /** * Default Routes * */ $router->setDefaultModule("frontend"); $router->add("/api/crud/:params", array('module' => 'api', 'controller' => 'crud', 'action' => 'index', 'params' => 1)); $router->add('/user/:action', array('module' => 'frontend', 'controller' => 'user', 'action' => '1')); $router->add('/galeria/:action', array('module' => 'frontend', 'controller' => 'galeria', 'action' => '1'));
/** * This methods registers the services to be used by the application */ protected function _registerServices() { $di = new \Phalcon\DI\FactoryDefault(); // Registering a Config shared-service $di->setShared('config', function () { $config['main'] = (include ROOT_DIR . "/config/main.php"); $config['db'] = (include ROOT_DIR . "/config/db.php"); $config['router'] = (include ROOT_DIR . "/config/router.php"); return new \Phalcon\Config($config); }); $config = $di->get('config'); // Change FactoryDefault default Router service $di->getService('router')->setDefinition(function () use($config) { $router = new \Phalcon\Mvc\Router(); //Setup routes from /config/router.php foreach ($config->router->toArray() as $key => $value) { $router->add($key, $value); } // Mount each module routers $router->mount(new \App\Frontend\Router()); $router->mount(new \App\Backend\Router()); // URI_SOURCE_SERVER_REQUEST_URI $router->setUriSource(\Phalcon\Mvc\Router::URI_SOURCE_SERVER_REQUEST_URI); // Remove trailing slashes automatically $router->removeExtraSlashes(true); // Setting a specific default using an array // DefaultModule // DefaultNamespace // DefaultController // DefaultAction $router->setDefaults(array('modul' => 'frontend', 'namespace' => 'App\\Frontend\\Controllers\\', 'controller' => 'index', 'action' => 'index')); // Set 404 paths $router->notFound(["controller" => "error", "action" => "error404"]); return $router; }); // Registering a View shared-service $di->setShared('view', function () { return new \Phalcon\Mvc\View(); }); // Registering a Mysql shared-service $di->setShared('mysql', function () use($config) { return new \Phalcon\Db\Adapter\Pdo\Mysql($config->db->mysql->toArray()); }); // Registering a Db shared-service $di->setShared('db', function () use($di) { return $di->get('mysql'); }); // Registering a Url shared-service $di->setShared('url', function () { $url = new \Phalcon\Mvc\Url(); $url->setBaseUri('/'); return $url; }); // Start the Session service $di->get('session')->start(); // Setup the Crypt service $di->get('crypt')->setMode(MCRYPT_MODE_CFB)->setKey($config->main->crypt_key); // Setup the Security service $di->get('security')->setWorkFactor(12); // Registering a custom authentication shared-service $di->setShared('auth', function () { return new \App\Common\Libs\Auth(); }); $this->setDI($di); }
<?php $router = new \Phalcon\Mvc\Router(); $router->add("/:controller/:action", array('controller' => 1, 'action' => 2))->convert('action', function ($action) { return str_replace('-', '', $action); }); $router->add("/:controller/:action/:params", array('controller' => 1, 'action' => 2, "seo" => 3))->convert('action', function ($action) { return str_replace('-', '', $action); }); $router->add("/coba", array('module' => 'backend', 'namespace' => 'HaiQuan\\Backend\\Controllers')); $router->add("/coba/:controller", array('module' => 'backend', 'namespace' => 'HaiQuan\\Backend\\Controllers', 'controller' => 1, 'action' => "index")); $router->add("/coba/:controller/:action", array('module' => 'backend', 'namespace' => 'HaiQuan\\Backend\\Controllers', 'controller' => 1, 'action' => 2))->convert('action', function ($action) { return str_replace('-', '', $action); }); $router->add("/tin-tuc/:params", array('module' => 'frontend', "controller" => "tintuc", "action" => "index", "seo" => 1)); $router->add("/tong-hop-tin-tuc", array('module' => 'frontend', "controller" => "tintuc", "action" => "tonghoptintuc", "seo" => 1)); $router->add("/", array('module' => 'frontend', 'namespace' => 'HaiQuan\\Frontend\\Controllers', "controller" => "index", "action" => "index")); $router->add("/tu-van", array('module' => 'frontend', 'namespace' => 'HaiQuan\\Frontend\\Controllers', "controller" => "tuvancauhoi", "action" => "index")); $router->add("/thu-tuc", array('module' => 'frontend', 'namespace' => 'HaiQuan\\Frontend\\Controllers', "controller" => "thutuc", "action" => "index")); $router->add("/van-ban/:params", array('module' => 'frontend', "controller" => "vanban", "action" => "index", "seo" => 1)); $router->add("/tong-hop-van-ban", array('module' => 'frontend', "controller" => "vanban", "action" => "tonghopvanban", "seo" => 1)); $router->setUriSource(\Phalcon\Mvc\Router::URI_SOURCE_GET_URL); $router->setDefaultModule("frontend"); $router->setDefaultNamespace("HaiQuan\\Frontend\\Controllers"); $router->removeExtraSlashes(true); $router->handle(); return $router;