示例#1
0
文件: Database.php 项目: xueron/pails
 public function boot(Container $container)
 {
     $container->setShared('db', function () {
         $env = env('APP_ENV', 'development');
         $yaml = Yaml::parse(@file_get_contents($this->configPath() . '/database.yml'));
         if (!empty($yaml['environments'][$env])) {
             $database = $yaml['environments'][$env];
             return new Mysql(array("host" => $database['host'], "port" => $database['port'], "username" => $database['user'], "password" => $database['pass'], "dbname" => $database['name'], 'charset' => $database['charset']));
         }
     });
 }
示例#2
0
 public function boot(Container $container)
 {
     // As of phalcon 3, definition will bind to di by default
     $container->setShared('dispatcher', function () {
         $eventsManager = $this->getShared('eventsManager');
         $eventsManager->attach('dispatch', new \Pails\Plugins\CustomRender());
         $dispatcher = new PhalconDispatcher();
         $dispatcher->setEventsManager($eventsManager);
         $dispatcher->setDefaultNamespace('App\\Controllers');
         $dispatcher->setModelBinding(true);
         return $dispatcher;
     });
 }
示例#3
0
文件: View.php 项目: xueron/pails
 /**
  * @param $container
  */
 public function boot(Container $container)
 {
     $container->setShared('view', function () {
         $view = new PhalconView();
         $view->setEventsManager($this->getShared('eventsManager'));
         $view->setViewsDir($this->path('views'));
         $view->registerEngines(['.volt' => 'volt', '.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php']);
         return $view;
     });
     $container->setShared('volt', function () {
         $volt = new Volt($this->get('view'));
         $volt->setOptions(['compiledPath' => $this->tmpPath() . '/cache/volt/', 'compiledSeparator' => '_', 'compileAlways' => true]);
         $volt->getCompiler()->addExtension(new \Pails\Extensions\Volt());
         return $volt;
     });
 }
示例#4
0
文件: Router.php 项目: xueron/pails
 public function boot(Container $container)
 {
     // Pails 不使用Phalcon的Module功能,通过Namespace组织Controllers.
     // 如果出现多级,比如AdminApi,则一定要以Namespace的形式组织 Admin\Api\xxxxController
     //
     // Note: from phalcon 3, closure bind di as $this by default. so no use($app) needed.
     //
     $container->setShared('router', function () {
         //
         $router = new Annotations(false);
         $router->removeExtraSlashes(true);
         $router->setEventsManager($this->get('eventsManager'));
         $router->setDefaultNamespace('App\\Controllers');
         $router->setDefaultController('application');
         $router->setDefaultAction('index');
         // Process /config/routes.php
         // Verb	        Path	            Action	Route Name
         // GET	        /photo	            index	photo.index
         // GET	        /photo/create	    create	photo.create
         // POST	        /photo	            store	photo.store
         // GET	        /photo/{photo}	    show	photo.show
         // GET	        /photo/{photo}/edit	edit	photo.edit
         // PUT/PATCH	/photo/{photo}	    update	photo.update
         // DELETE	    /photo/{photo}	    destroy	photo.destroy
         foreach ($this->getConfig('routes') as $url => $route) {
             //                $resourceDefaults = ['index', 'create', 'store', 'show', 'edit', 'update', 'destroy'];
             //
             //                // a RESTful resource
             //                if (isset($route['resource'])) {
             //                    if (!isset($route[''])) {}
             //                    foreach ($this->getResourceMethods($defaults, $options) as $m) {
             //                        $this->{'addResource'.ucfirst($m)}($url, $route, $options);
             //                    }
             //                } else {
             //                    if (count($route) !== count($route, COUNT_RECURSIVE)) {
             //                        if (isset($route['pattern']) && isset($route['paths'])) {
             //                            $method = isset($route['httpMethods']) ? $route['httpMethods'] : null;
             //                            $router->add($route['pattern'], $route['paths'], $method);
             //                        } else {
             //                            throw new \RuntimeException(sprintf('No route pattern and paths found by route %s', $url));
             //                        }
             //                    } else {
             //                        $router->add($url, $route);
             //                    }
             //                }
         }
         // 定义注解路由
         $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->path('Controllers')), \RecursiveIteratorIterator::SELF_FIRST);
         foreach ($iterator as $item) {
             if (Text::endsWith($item, "Controller.php", false)) {
                 $name = str_replace([$this->path('Controllers') . DIRECTORY_SEPARATOR, "Controller.php"], "", $item);
                 $name = str_replace(DIRECTORY_SEPARATOR, "\\", $name);
                 $router->addResource('App\\Controllers\\' . $name);
             }
         }
         // 定义404路由
         $router->notFound(["controller" => "application", "action" => "notfound"]);
         //
         return $router;
     });
 }