zoukankan      html  css  js  c++  java
  • phalcon: 官方多模块

    目录结构如下

    public/index.php:

    use PhalconMvcRouter;
    use PhalconTag;
    use PhalconMvcUrl;
    use PhalconMvcView;
    use PhalconMvcApplication;
    use PhalconDIFactoryDefault;
    use PhalconDbAdapterPdoMysql as DbAdapter;
    
    // Create a DI
    $di = new FactoryDefault();
    
    $di->set('router', function () {
    
        $router = new Router();
    
        $router->setDefaultModule("frontend");
    
        $router->setDefaults(array(
            "namespace" => 'MultipleFrontendControllers',
            "controller" => "Index",
            "action" => "index"
        ));
        //frontend
        $router->add("/:controller", array(
            'module'=>'frontend',
            'controller' => 1,
        ));
        $router->add("/:controller/:action", array(
            'module'=>'frontend',
            'controller' => 1,
            'action'     => 2,
        ));
        $router->add("/:controller/:action/:param", array(
            'module'=>'frontend',
            'controller' => 1,
            'action'     => 2,
            'param'      => 3,
        ));
        //backend
        $router->add("/admin/:controller", array(
            'module'=>'backend',
         'namespace'=>'MultipleBackendControllers', // 一定要加上,要不然找不到后台 'controller' => 1, )); $router->add("/admin/:controller/:action", array( 'module'=>'backend',
         'namespace'=>同上, 'controller' => 1, 'action' => 2, )); $router->add("/admin/:controller/:action/:param", array( 'module'=>'backend',
    'namespace'=>同上, 'controller' => 1, 'action' => 2, 'param' => 3, )); return $router; }); try { $di['db'] = function(){ return new DbAdapter(array( "host" => "localhost", "username" => "root", "password" => "", "dbname" => "demo", "charset" => "utf8" )); }; // Setup a base URI so that all generated URIs include the "tutorial" folder $di['url'] = function() { $url = new Url(); $url->setBaseUri('/'); return $url; }; // Handle the request $application = new Application($di); //加入模块分组配置 $application->registerModules( array( 'frontend' => array( 'className' => 'MultipleFrontendModule', 'path' => '../app/frontend/Module.php', ), 'backend' => array( 'className' => 'MultipleBackendModule', 'path' => '../app/backend/Module.php', ) ) ); echo $application->handle()->getContent(); } catch (Exception $e) { echo "Exception: ", $e->getMessage(); }

      

    frontend/module.php(backend/module.php的配置跟它的雷同):

    <?php
    namespace MultipleFrontend;
     
    use PhalconLoader,
        PhalconMvcDispatcher,
        PhalconMvcView,
        PhalconMvcModuleDefinitionInterface;
     
    class Module implements ModuleDefinitionInterface {
     
        public function registerAutoloaders( PhalconDiInterface $di = NULL)
        {
            $loader = new Loader();
            $loader->registerNamespaces(array(
                'MultipleFrontendControllers' => __DIR__ . '/controllers/',
                'MultipleModels' => __DIR__ . '/../models/'
            ))->register();
            $loader->registerDirs(
                array(
                    'modelsDir'      => '../app/models/',
                )
            )->register();
     
        }
     
        public function registerServices( PhalconDiInterface $di)
        {
            $di->set("dispatcher", function(){
                $dispatcher = new Dispatcher();
                $dispatcher->setDefaultController("MultipleFrontendControllers");
                return $dispatcher;
            });
     
            $di->set("view", function(){
                $view = new View();
                $view->setViewsDir("../app/frontend/views/");
                $view->registerEngines(array(
                    '.phtml' => 'PhalconMvcViewEnginePhp'
                ));
                return $view;
            });
     
        }
    }
    

      

    frontend/controllers/indexController.php:

    namespace MultipleFrontendControllers;
    //use MultipleModelsLevel;
    class IndexController extends PhalconMvcController {
    
        public function indexAction()
        {
        $level = Level::findFirst("id = 10"); $this->view->pick('index/index'); } }

      models/Level.php

    //namespace MultipleModels;
    class Level extends PhalconMvcModel
    {
    
        public $lid;
        public $name;
        public $description;
        public $create_time;
    
        public function beforeCreate()
        {
            if( is_null($this->create_time) )
            {
                $this->create_time = time();
            }
    
        }
    }
    

      

     那么多模块下,如何分页的,如果直接用->from(表名),报错找不到此类,此时要引用model类的全命名空间名称如下:

    MultipleModelsLevel(表)。
    namespace MultipleBackendControllers;
    use PhalconPaginatorAdapterQueryBuilder;
    class IndexController extends ControllerBase {
    
        public function initialize()
        {
            parent::initialize();
    
    
        }
    
        public function indexAction()    {
    
    
            $builder = $this->modelsManager->createBuilder()
                ->columns("id,username,name,email")
                ->from('Users')
                ->orderBy("id ASC")
                ->getQuery()
                ->execute();
    
            $paginator = new QueryBuilder(array(
                "builder"=>$builder,
                "limit"=>10,
                "page"=>1
            ));
    
            $this->view->setTemplateBefore('base');
            $this->view->pick('index/index');
        }
    
    }
    

      





  • 相关阅读:
    QQ恢复解散后的群聊或删除后的好友的方法
    微软双屏手机Surface Duo曝新料
    利用Travis CI+GitHub实现持续集成和自动部署
    利用echarts展示旅行足迹
    03_K近邻算法
    02_感知机算法
    《面试官之你说我听》:简明的图解Redis RDB持久化、AOF持久化
    【绝对有收获】看看?必须告诉你为什么要使用MQ消息中间件(图解版)
    推荐收藏系列:一文理解JVM虚拟机(内存、垃圾回收、性能优化)解决面试中遇到问题(图解版)
    利用window.performance.timing进行性能分析
  • 原文地址:https://www.cnblogs.com/achengmu/p/5985767.html
Copyright © 2011-2022 走看看