/** * Register application modules */ public function registerModules() { try { $modules = array(); foreach ($this->config as $moduleConfig) { $modules[$moduleConfig->moduleName] = array('className' => $moduleConfig->className, 'path' => $moduleConfig->path); } //add modules to application $this->application->registerModules($modules); } catch (Exception $e) { throw new \Foundation\Helper\Exception($e->getMessage(), $e->getCode()); } }
public function modulesClosure(IntegrationTester $I) { $I->wantTo('handle request and get content by using single modules strategy (closure)'); Di::reset(); $_GET['_url'] = '/login'; $di = new FactoryDefault(); $di->set('router', function () { $router = new Router(false); $router->add('/index', ['controller' => 'index', 'module' => 'frontend', 'namespace' => 'Phalcon\\Test\\Modules\\Frontend\\Controllers']); $router->add('/login', ['controller' => 'login', 'module' => 'backend', 'namespace' => 'Phalcon\\Test\\Modules\\Backend\\Controllers']); return $router; }); $application = new Application(); $view = new View(); $application->registerModules(['frontend' => function ($di) use($view) { /** @var \Phalcon\DiInterface $di */ $di->set('view', function () use($view) { $view->setViewsDir(PATH_DATA . 'modules/frontend/views/'); return $view; }); }, 'backend' => function ($di) use($view) { /** @var \Phalcon\DiInterface $di */ $di->set('view', function () use($view) { $view->setViewsDir(PATH_DATA . 'modules/backend/views/'); return $view; }); }]); $application->setDI($di); $I->assertEquals('<html>here</html>' . PHP_EOL, $application->handle()->getContent()); }
/** * Initialize phalcon application * @return PhalconApplication */ protected function initPhalconApplication() { $diFactory = $this->diManager->getDI(); $moduleHandler = $diFactory->get('moduleHandler'); // Register autoloader (new Autoloader($diFactory))->register(); // Register services and routers $this->diManager->initInvokableServices()->initFactoriedServices()->initRouterDi(); // Init listeners (new Listener($diFactory))->listenApplicationEvents(new Listener\Application())->listenDispatchEvents(new Listener\Dispatch()); // Register modules $application = new PhalconApplication($diFactory); $application->setEventsManager($diFactory['eventsManager']); $application->registerModules($moduleHandler->getRegisteredModules()); return $application; }
/** * Runs the application performing all initializations * * @param $options * * @return mixed */ public function run($options) { $loaders = ['config', 'loader', 'session', 'permission', 'url', 'database', 'logger', 'environment', 'flash', 'flashsession', 'router', 'dispatcher', 'modelsmanager', 'metadata', 'annotations', 'view', 'cache', 'security', 'crypt', 'cookie', 'beanstalkd', 'acl', 'filemanager', 'authentication']; foreach ($loaders as $service) { $function = 'init' . ucfirst($service); $this->{$function}($options); } $application = new PhApplication(); $application->setDI($this->di); $modules = $this->getModules(); $application->registerModules($modules); $eventsManager = new PhEventsManager(); $application->setEventsManager($eventsManager); $eventsManager->attach('application:beforeHandleRequest', function ($event, $application) { $config = $this->di->get('config'); $response = $this->di->get('response'); $dispatcher = $this->di->get('dispatcher'); $cookie = $this->di->get('cookie'); //Detect mobile device $detect = new \Fly\Mobile_Detect(); if ($config->app_mobile == true && $detect->isMobile() && SUBDOMAIN != 'm' && $dispatcher->getModuleName() == 'common') { //begin redirect link to mobile version $curPageURL = \Fly\Helper::getCurrentUrl(); $curPageURL = str_replace(array('http://', 'https://'), array('http://m.', 'https://m.'), $curPageURL); $response->redirect($curPageURL, true); } //Setting language service $this->di->setShared('lang', function () use($dispatcher, $cookie) { $language = ''; // Detect language via cookie if ($cookie->has('language')) { $language = $cookie->get('language')->getValue(); } else { //Get default language $language = $this->config->defaultLanguage; } return new FlyTranslate(['module' => strtolower($dispatcher->getModuleName()), 'controller' => $dispatcher->getControllerName(), 'language' => $language]); }); }); return $application->handle()->getContent(); }
public function __construct(Di $di, Application $app, $config = [], array $userOptions = null) { $this->setDi($di); if (!$config instanceof Config) { $config = new Config(is_array($config) ? $config : []); } $this->app = $app; $this->initOptions($di, $userOptions); $default = $this->getDefaultModule(); $detected = $this->_detectModuleConf(); $modconf = $this->_mergeModConf($detected, $config); $modarray = $modconf->toArray(); $this->config = $modconf; if (count($modarray)) { $app->registerModules($modarray); if (in_array($default, array_keys($modarray))) { $app->setDefaultModule($default); $this->router->setDefaultModule($default); } $this->initModuleRouting($app); } }
<?php namespace Depot; use Phalcon\Mvc\Router; use Phalcon\Mvc\Application; use Phalcon\DI\FactoryDefault; $config = (include __DIR__ . '/../config/config.php'); include __DIR__ . '/../config/services.php'; $di->set('config', $config); $application = new Application($di); $application->registerModules(array('api' => array('className' => 'Depot\\Api\\Module', 'path' => '../apps/api/Module.php'), 'cdn' => array('className' => 'Depot\\Cdn\\Module', 'path' => '../apps/cdn/Module.php'), 'ui' => array('className' => 'Depot\\Ui\\Module', 'path' => '../apps/ui/Module.php'))); echo $application->handle()->getContent();
} }); $connection->setEventsManager($eventsManager); return $connection; }; $di["dbBackupTool"] = function () use($di) { $config = $di->get("config")->get("common")["db"]; return new \Phalcon_wifi\Common\Ext\DBManager($config["username"], $config["password"], $config["host"], $config["dbname"]); }; $di["filter"] = function () { return new \Phalcon_wifi\Common\Ext\Filter(); }; $di["validateCodeCreator"] = function () use($di) { return new \Phalcon_wifi\Common\Ext\ValidateCode($di->get('session')); }; $di["config"] = function () { $config = new \Phalcon_wifi\Common\Config\Config(); return $config->config; }; $di->set('modelsManager', function () { return new \Phalcon\Mvc\Model\Manager(); }); $di->set('auth', function () { return new \Phalcon_wifi\Common\Models\AuthGroup(); }); $application = new Application($di); $application->registerModules(array("Admin" => array("className" => "Phalcon_wifi\\Admin\\Module", "path" => __DIR__ . "/../apps/Admin/Module.php"), "Portal" => array("className" => "Phalcon_wifi\\Portal\\Module", "path" => __DIR__ . "/../apps/Portal/Module.php"))); echo $application->handle()->getContent(); } catch (Exception $e) { echo $e->getMessage(); }
/** * Include services */ require __DIR__ . '/../common/config/services.php'; /** * Handle the request */ $application = new Application(); /** * Assign the DI */ $application->setDI($di); /** * Include modules */ $application->registerModules(require __DIR__ . '/../common/config/modules.php'); echo $application->handle()->getContent(); } catch (Phalcon\Exception $e) { echo $e->getMessage(); } catch (PDOException $e) { echo $e->getMessage(); } function d($object, $kill = true) { echo '<pre style="text-aling:left">'; print_r($object); if ($kill) { die('END'); } echo '</pre>'; }
/** * 注册模块 */ protected function registerModules() { if ($this->config->offsetExists('modules')) { $this->app->registerModules($this->config->modules->toArray()); } }
// "password" => "haha", // "dbname" => "anj", // "options" => array( // PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4' // ) // ) //); // //// Assign the eventsManager to the db adapter instance //$connection->setEventsManager($eventsManager); // //return $connection; return new \Phalcon\Db\Adapter\Pdo\Mysql(array("host" => "localhost", "username" => "root", "password" => "haha", "dbname" => "anj", "options" => array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4'))); }; // Setup a base URI so that all generated URIs include the "tutorial" folder //$di->set('url', function () { //$url = new UrlProvider(); //$url->setBaseUri('/public/index.php?_url=/'); //return $url; //}); $di->setShared('session', function () { $session = new Phalcon\Session\Adapter\Files(); $session->start(); return $session; }); // Handle the request $application = new Application($di); $application->registerModules(array('frontend' => array('className' => 'Multiple\\Frontend\\Module', 'path' => '../apps/frontend/Module.php'), 'backend' => array('className' => 'Multiple\\Backend\\Module', 'path' => '../apps/backend/Module.php'))); //require "/../app/controllers/BaseController.php"; echo $application->handle()->getContent(); } catch (\Exception $e) { echo "PhalconException: ", $e->getMessage(); }
/** * Register an array of modules present in the application * *<code> * $this->registerModules(array( * 'Example' => array( * 'className' => 'Example\\Module', * 'path' => APP_ROOT . '/app/modules/Example/Module.php' * ), * 'Test' => array( * 'className' => 'Test\\Module', * 'path' => APP_ROOT . '/app/modules/Test/Module.php' * ) * )); *</code> * * @param array $modules * @param boolean $merge */ public function registerModules($modules, $merge = null) { $this->registerSharedData($modules); parent::registerModules($modules, $merge); }
public function mvcApp() { $app = new Application(); $app->setDI(Bootstrap::get()->mvcDi()); $app->registerModules(Bootstrap::get()->modules()); return $app; }
<?php error_reporting(E_ALL); use Phalcon\Mvc\Router, Phalcon\Mvc\Application, Phalcon\DI\FactoryDefault, Phalcon\Exception; $di = new FactoryDefault(); $di->set('router', function () { $router = new Router(); $router->setDefaultModule("frontend"); $router->add("/api.php", array('module' => 'core', 'controller' => 'api', 'action' => 'index')); // better to locate somewhere in the module $router->add('/integration/{name:.*}/', array('module' => 'frontend', 'controller' => 'integration', 'action' => 'parts')); return $router; }); try { //Create an application $application = new Application($di); // Register the installed modules $application->registerModules(array('frontend' => array('className' => 'Kladr\\Frontend\\Module', 'path' => '../apps/frontend/Module.php'), 'core' => array('className' => 'Kladr\\Core\\Module', 'path' => '../apps/core/Module.php'))); //Handle the request echo $application->handle()->getContent(); } catch (Exception $e) { echo $e->getMessage(); }
public function setModules($modules) { parent::registerModules($modules); }
/** * 多级缓存 */ $di->set('cache', function () use($config) { $ultraFastFrontend = new DataFrontend(array("lifetime" => 3600)); $fastFrontend = new DataFrontend(array("lifetime" => 86400)); $slowFrontend = new DataFrontend(array("lifetime" => 604800)); $cache = new Multiple(array(new Xcache($ultraFastFrontend, array("prefix" => 'cache_')), new RedisCache($fastFrontend, array("prefix" => 'cache_', "host" => $config->redis->host, "port" => $config->redis->port)), new FileCache($slowFrontend, array("prefix" => 'cache_', "cacheDir" => "../apps/cache/")))); return $cache; }); $di->set('security', function () { $security = new Security(); // Set the password hashing factor to 12 rounds $security->setWorkFactor(12); return $security; }, true); $di->setShared('session', function () { $session = new Phalcon\Session\Adapter\Files(); $session->start(); return $session; }); try { // 创建应用 $application = new Application($di); // 注册模块 $application->registerModules(array('frontend' => array('className' => 'Blog\\Frontend\\Module', 'path' => '../apps/frontend/Module.php'), 'user' => array('className' => 'Blog\\User\\Module', 'path' => '../apps/user/Module.php'), 'backend' => array('className' => 'Blog\\Backend\\Module', 'path' => '../apps/backend/Module.php'))); // 处理请求 echo $application->handle()->getContent(); } catch (\Exception $e) { echo $e->getMessage(); }
<?php use Phalcon\Mvc\Router; use Phalcon\Mvc\Application; use Phalcon\Di\FactoryDefault; $di = new FactoryDefault(); // Специфичные роуты для модуля // More information how to set the router up https://docs.phalconphp.com/ru/latest/reference/routing.html $di->set('router', function () { $router = new Router(false); $router->setDefaultModule("frontend"); $router->add("/", array('controller' => 'index', 'action' => 'index')); return $router; }); try { // Создание приложения $application = new Application($di); // Регистрация установленных модулей $application->registerModules(array('frontend' => array('className' => 'Pigeon\\Frontend\\Module', 'path' => '../apps/frontend/Module.php'))); //Setup a base URI so that all generated URIs include the "tutorial" folder // Обработка запроса echo $application->handle()->getContent(); } catch (\Exception $e) { echo $e->getMessage(); }
/** * Registers the Application modules. * * @see parent::registerModules * @param array $module * @param bool $merge * @return void */ public function registerModules(array $modules, $merge = false) { foreach ($modules as $name => &$config) { $config = function () use($name, $config) { $this->di->get('module', [$name, $config])->registerServiceProviders(); }; } parent::registerModules($modules, $merge); }
/** * Init modules and register them. * * @param array $modules Modules bootstrap classes. * @param null $merge Merge with existing. * * @return $this */ public function registerModules($modules, $merge = null) { $bootstraps = []; $di = $this->getDI(); foreach ($modules as $moduleName => $moduleClass) { if (isset($this->_modules[$moduleName])) { continue; } $bootstrap = new $moduleClass($di, $this->getEventsManager()); $bootstraps[$moduleName] = function () use($bootstrap, $di) { $bootstrap->initialize(); return $bootstrap; }; } return parent::registerModules($bootstraps, $merge); }
/** * Include services */ require ROOT_DIR . 'core/config/services.php'; /** * Handle the request */ $application = new Application(); /** * Assign the DI */ $application->setDI($di); /** * Include modules */ $application->registerModules(require ROOT_DIR . 'core/config/modules.php'); /** * Sets the event manager */ $application->setEventsManager($eventsManager); echo $application->handle()->getContent(); } catch (Exception $e) { echo $e->getMessage(); echo $e->getTraceAsString(); /** * Show an static error page */ if (!$di->get('config')->application->debug) { $response = new Response(); $response->redirect('errors/503'); $response->send();
*/ require APP_PATH . '/config/services.php'; /** * Include web environment specific services */ require APP_PATH . '/config/services_web.php'; /** * Get config service for use in inline setup below */ $config = $di->getConfig(); /** * Include Autoloader */ include APP_PATH . '/config/loader.php'; /** * Handle the request */ $application = new Application($di); /** * Register application modules */ $application->registerModules(['frontend' => ['className' => '@@namespace@@\\Modules\\Frontend\\Module']]); /** * Include routes */ require APP_PATH . '/config/routes.php'; echo $application->handle()->getContent(); } catch (\Exception $e) { echo $e->getMessage() . '<br>'; echo '<pre>' . $e->getTraceAsString() . '</pre>'; }
/** * Register modules */ protected function registerModules() { $modules = $this->getModulesList(); $this->application->registerModules($modules); }
$crypt = new Crypt(); $crypt->setKey($config->crypt->key); return $crypt; }); $di->setShared('redis', function () use($config) { $redis = new Redis(); $redis->open($config->redis->host, $config->redis->port); return $redis; }); $di->setShared('session', function () { $session = new RedisSessionAdapter(array('path' => 'tcp://127.0.0.1:6379?weight=1')); $session->start(); return $session; }); $di->set('router', function () { $router = new Router(); $router->add("/:controller/:action/:params", array('module' => 'frontend', 'controller' => 1, 'action' => 2, 'params' => 3)); $router->add("/admin/:controller/:action/:params", array('module' => 'backend', 'controller' => 1, 'action' => 2, 'params' => 3)); $router->add("/:controller/([a-z0-9:/+]+)/page:([0-9]+)", array('module' => 'frontend', 'controller' => 1, 'action' => 'index', 'params' => 2, 'page' => 3)); $router->add("/:controller/([0-9]+)", array('module' => 'frontend', 'controller' => 1, 'action' => 'detail', 'id' => 2)); $router->add("/", array('module' => 'frontend', 'controller' => 'index', 'action' => 'index')); return $router; }); try { $app = new Application($di); $app->registerModules(array('frontend' => array('className' => 'Pohome\\Frontend\\Module', 'path' => '../apps/frontend/Module.php'), 'backend' => array('className' => 'Pohome\\Backend\\Module', 'path' => '../apps/backend/Module.php'))); $response = $app->handle(); $response->send(); } catch (\Exception $e) { echo $e->getMessage(); }
/** * Register the given modules in the parent and prepare to load * the module routes by triggering the init routes method */ public function registerModules($modules, $merge = null) { parent::registerModules($modules, $merge); $loader = new Loader(); $modules = $this->getModules(); /** * Iterate the application modules and register the routes * by calling the initRoutes method of the Module class. * We need to auto load the class */ foreach ($modules as $module) { $className = $module['className']; if (!class_exists($className, false)) { $loader->registerClasses([$className => $module['path']], true)->register()->autoLoad($className); } /** @var \App\Common\Lib\Application\ApplicationModule $className */ $className::initRoutes($this->di); } }
<?php use Phalcon\Mvc\Application; /** * Handle the request */ $application = new \Phalcon\Mvc\Application($di); /** * Register application modules */ if (isset($modules) && !empty($modules)) { $application->registerModules($modules); } echo $application->handle()->getContent();
*/ $app = new Application($di); /** * Attach the eventsManager to the app for use a Middleware */ $eventsManager = $app->di->get(AppServices::EVENTS_MANAGER); $app->dispatcher->setEventsManager($eventsManager); $router = $app->di->get(AppServices::ROUTER); /** * Attach plugins, components, modules to app */ include APP_PATH . 'Config/middleware.php'; include APP_PATH . 'Config/routes.php'; $modules = (include APP_PATH . 'Config/modules.php'); if (count($modules) > 0) { $app->registerModules($modules); } /** * Disable automatic rendering */ $app->useImplicitView(false); /** * Start application */ $app->handle(); /** * Set content */ $returnedValue = $app->dispatcher->getReturnedValue(); if ($returnedValue !== null && !is_string($returnedValue)) { $app->response->setJsonContent($returnedValue);