/** * Runs the application performing all initializations * * @param $options * * @return mixed */ public function run($options) { $loaders = array('session', 'config', 'loader', 'url', 'router', 'view', 'cache', 'markdown'); foreach ($loaders as $service) { $function = 'init' . ucfirst($service); $this->{$function}(); } $application = new PhApplication(); $application->setDI($this->di); return $application->handle($_SERVER['REQUEST_URI'])->getContent(); }
/** * @param array $config 配置文件 */ public function run(array $config) { $this->di = new FactoryDefault(); $this->app = new Application(); if (defined('APP_ENV') && APP_ENV == 'product') { $this->debug = false; error_reporting(0); } else { $this->debug = true; error_reporting(E_ALL); } $this->initConfig($config); try { $this->onBefore(); $this->initRouters(); $this->initUrl(); $this->initView(); $this->initDatabase(); $this->initModelsMetadata(); $this->initCookie(); $this->initCrypt(); $this->initLogger(); $this->onAfter(); $this->app->setDI($this->di); $this->registerModules(); echo $this->app->handle()->getContent(); } catch (\Exception $e) { echo $e->getMessage(); } }
/** * Init application * * @return Application */ public function registerApplication() { $this->registerConfig(); $this->registerServices(); $this->registerModules(); $this->application->setDI($this->di); return $this->application; }
/** * */ public function setUp() { $di = new DI(); $_SERVER['HTTP_HOST'] = 'example.com'; $_SERVER['REQUEST_METHOD'] = 'GET'; $_SERVER['REQUEST_URI'] = '/path?foo=aaa&bar=bbb'; $_GET = array('_url' => '/path', 'foo' => 'aaa', 'bar' => 'bbb'); $request = new Request(); $request->setDI($di); $this->request = $request; $response = new Response(); $response->setDI($di); $dispatcher = new Dispatcher(); $dispatcher->setDI($di); $this->dispatcher = $dispatcher; $cache = new BackendCache(new FrontendCache()); $di->set('viewCache', $cache); $config = new Config(array('cache' => array('enable' => true))); $di->set('config', $config); $eventsManager = new Manager(); $di->set('request', $request, true); $di->set('response', $response, true); $di->set('dispatcher', $dispatcher, true); $di->set('eventsManager', $eventsManager); $this->di = $di; $application = new Application(); $application->setDI($di); $application->setEventsManager($eventsManager); $this->application = $application; }
public function setUp() { $di = new DI(); $_SERVER['HTTP_HOST'] = 'example.com'; $_SERVER['REQUEST_METHOD'] = 'GET'; $_SERVER['REQUEST_URI'] = '/path?foo=aaa&bar=bbb'; $_GET = array('_url' => '/path', 'foo' => 'aaa', 'bar' => 'bbb'); $request = new Request(); $request->setDI($di); $this->request = $request; $response = new Response(); $response->setDI($di); $this->response = $response; $eventsManager = new Manager(); $cors = new Cors(array(array('domain' => 'bar.com'))); $di->set('request', $request, true); $di->set('response', $response, true); $di->set('eventsManager', $eventsManager); $di->set('cors', $cors); $this->di = $di; $application = new Application(); $application->setDI($di); $application->setEventsManager($eventsManager); $this->application = $application; }
/** * HMVCApplication Constructor * * @param Phalcon\DiInterface */ public function __construct(DiInterface $di) { $loader = new Loader(); //Application Loader $loader->registerDirs(array('../app/controllers/'))->register(); //Register the view service $di['view'] = function () { $view = new View(); $view->setViewsDir('../app/views/'); return $view; }; //Register the app itself as a service $di['app'] = $this; //Sets the parent Id parent::setDI($di); }
/** * Runs the application performing all initializations * * @param $options * * @return mixed */ public function run($options) { $loaders = array('session', 'config', 'loader', 'url', 'router', 'view', 'cache'); foreach ($loaders as $service) { $function = 'init' . ucfirst($service); $this->{$function}(); } $application = new PhApplication(); $application->setDI($this->di); if (PHP_OS == 'Linux') { $uri = $_SERVER['REQUEST_URI']; } else { $uri = null; } return $application->handle($uri)->getContent(); }
/** * Runs the application performing all initializations * * @param $options * * @return mixed */ public function run($options) { $loaders = array('config', 'session', 'loader', 'url', 'router', 'database', 'view', 'cache', 'log', 'utils', 'debug'); try { // Handing missing controller errors $this->di->set('dispatcher', function () { //Create an EventsManager $eventsManager = new EventsManager(); // Attach a listener $eventsManager->attach("dispatch:beforeException", function ($event, $dispatcher, $exception) { // Handle 404 exceptions if ($exception instanceof DispatchException) { $dispatcher->forward(array('controller' => 'index', 'action' => 'internalServerError')); return false; } // Alternative way, controller or action doesn't exist if ($event->getType() == 'beforeException') { switch ($exception->getCode()) { case \Phalcon\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND: case \Phalcon\Dispatcher::EXCEPTION_ACTION_NOT_FOUND: $dispatcher->forward(array('controller' => 'index', 'action' => 'internalServerError')); return false; } } }); // Instantiate the Security plugin $security = new Security($di); // Listen for events produced in the dispatcher using the Security plugin $eventsManager->attach('dispatch', $security); $dispatcher = new \Phalcon\Mvc\Dispatcher(); // Bind the EventsManager to the dispatcher $dispatcher->setEventsManager($eventsManager); return $dispatcher; }, true); foreach ($loaders as $service) { $function = 'init' . ucfirst($service); $this->{$function}(); } $application = new PhApplication(); $application->setDI($this->di); return $application->handle()->getContent(); } catch (PhException $e) { echo $e->getMessage(); } catch (\PDOException $e) { echo $e->getMessage(); } }
/** * Runs the application performing all initializations * * @param $options * * @return mixed */ public function run($options) { $loaders = array('session', 'config', 'loader', 'url', 'router', 'view', 'cache'); try { foreach ($loaders as $service) { $function = 'init' . ucfirst($service); $this->{$function}(); } $application = new PhApplication(); $application->setDI($this->di); return $application->handle()->getContent(); } catch (PhException $e) { echo $e->getMessage(); } catch (\PDOException $e) { echo $e->getMessage(); } }
/** * Runs the application performing all initializations * * @param $options * * @return mixed */ public function run($options) { $loaders = array('config', 'loader', 'environment', 'timezone', 'debug', 'flash', 'url', 'dispatcher', 'view', 'logger', 'database', 'session', 'cache', 'behaviors'); try { foreach ($loaders as $service) { $function = 'init' . ucfirst($service); $this->{$function}($options); } $application = new PhApplication(); $application->setDI($this->_di); return $application->handle()->getContent(); } catch (PhException $e) { echo $e->getMessage(); } catch (\PDOException $e) { echo $e->getMessage(); } }
/** * Application Constructor * * @param \Phalcon\DiInterface $di */ public function __construct(DiInterface $di) { /** * Sets the parent DI and register the app itself as a service, * necessary for redirecting HMVC requests */ parent::setDI($di); $di->set('app', $this); /** * Register application wide accessible services */ $this->_registerServices(); /** * Register the installed/configured modules */ $this->registerModules(require __DIR__ . '/../../../config/modules.php'); }
protected function initApplication() { $di = $this->getDI(); if (!$this->getUserOption('app') instanceof Application) { $this->setUserOption('app', new Application()); } $this->app = $this->getUserOption('app'); $this->app->setDI($di); $this->app->setEventsManager($di->get('eventsManager')); // disable implicit views if using simple views if ($di->has('view') && !$di->get('view') instanceof ViewInterface) { $this->app->useImplicitView(false); } $di->setShared('app', $this->app); if (!defined('APP_ENV')) { define('APP_ENV', getenv('APP_ENV') ?: static::ENV_PRODUCTION); } }
/** * 构建网站服务入口(Constructor) * * @param $di */ public function __construct(\Phalcon\DiInterface $di) { $this->_di = $di; $loaders = array('loader', 'config', 'timezone', 'db', 'crypt', 'cache', 'language', 'url', 'router'); // 注册各载入服务(Register services) try { foreach ($loaders as $service) { $this->{$service}(); } } catch (\Exception $e) { exit('网站模块注册出错!请通知管理员尽快恢复正常运行,谢谢!'); } // 注册服务模块(Register modules) $this->registerModules(array('home' => array('className' => 'App\\Home\\Module', 'path' => APP_PATH . '/home/Module.php'), 'admin' => array('className' => 'App\\Admin\\Module', 'path' => APP_PATH . '/admin/Module.php'), 'backend' => array('className' => 'App\\Backend\\Module', 'path' => APP_PATH . '/backend/Module.php'))); // 注册本类为应用服务(Register the app itself as a service) $this->_di->set('app', $this); // 调用父类注册入口(Sets the parent Di) parent::setDI($this->_di); }
/** * 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 run() { try { $this->onBefore(); $this->initLoader(); $this->initRouters(); $this->initUrl(); $this->initDatabase(); $this->initModelsMetadata(); $this->initCookie(); $this->initLogger(); $this->initShortFunc(); \Phalcon\Mvc\Model::setup(['notNullValidations' => false]); $this->onAfter(); $this->app->setDI($this->di); $this->registerModules(); echo $this->app->handle()->getContent(); } catch (\Exception $e) { echo $e->getMessage(); } }
define('APPLICATION_ENV', getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'local'); require ROOT_DIR . 'core/config/loader.php'; require ROOT_DIR . 'vendor/autoload.php'; try { /** * 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 */
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()); }
public function mvcApp() { $app = new Application(); $app->setDI(Bootstrap::get()->mvcDi()); $app->registerModules(Bootstrap::get()->modules()); return $app; }
/** * 自定义服务注入 * */ protected function customServices() { $di = $this->application->getDI(); // 自定义服务 $this->application->setDI($di); }
use Phalcon\Mvc\Application; use Phalcon\Mvc\Router; use Phalcon\Mvc\Url as UrlResolver; use Phalcon\DI\FactoryDefault; use Phalcon\Session\Adapter\Files as SessionAdapter; use Phalcon\Loader; error_reporting(E_ALL); try { /** * Handle the request */ $application = new Application(); /** * Assign the DI */ $application->setDI(setDi()); require __DIR__ . '/../config/modules.php'; echo $application->handle()->getContent(); } catch (Phalcon\Exception $e) { echo $e->getMessage(); } catch (PDOException $e) { echo $e->getMessage(); } function setDi() { $di = new FactoryDefault(); $di['router'] = function () use($di) { $router = new Router(); $router->setDefaultModule('mobimall'); return $router; };