phalcon:整合官方多模块功能,方便多表查询

phalcon:整合官方多模块功能,方便多表查询

项目分为:

namespace MultipleBackend;
namespace MultipleFrontend;

目录结构如下:

public/index.php的大致写法:

多模块功能:

// 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',
            )
        )
    );

  

来看下多模块下Module.php的写法,

backend/Module.php

namespace MultipleBackend;
 
use PhalconLoader,
    PhalconMvcDispatcher,
    PhalconDiInterface,
    PhalconMvcView,
    PhalconMvcModuleDefinitionInterface;
 
class Module implements ModuleDefinitionInterface {
 
    public function registerAutoloaders( DiInterface $di = NULL)
    {
        $loader = new Loader();
        $loader->registerNamespaces(array(
            'MultipleBackendControllers' => __DIR__ .'/controllers/'
        ))->register();
        $loader->registerDirs(
            array(
                'modelsDir'      => '../app/models/', #注意这里,必须填写,否则models/下的文件不能共用。
            )
        )->register();
 
    }
 
    public function registerServices( PhalconDiInterface $di)
    {
        $di->set("dispatcher", function(){
            $dispatcher = new Dispatcher();
            $dispatcher->setDefaultController("MultipleBackendControllers");
            return $dispatcher;
        });
 
        $di->set("view", function(){
            $view = new View();
            $view->setViewsDir("../app/backend/views/");
            $view->registerEngines(array(
                '.phtml' => 'PhalconMvcViewEnginePhp'
            ));
            return $view;
        });
 
    }
}

  

models/下的model文件,不需要命名空间,直接写:

use PhalconMvcModel;
class Album extends Model {
  ......

}

controllers/下面的model调用:

namespace MultipleBackendControllers;
use PhalconPaginatorAdapterQueryBuilder as PaginatorQueryBuilder;
class AlbumController extends ControllerBase {

    public function initialize(){
        parent::initialize();
    }

    public function indexAction()
    {

        $currentPage = $this->getParam('page');
        $builder = $this->modelsManager->createBuilder()
            ->columns("aid,atid,name,mid,nid,create_time")
            ->from("Album")
            ->where("enable = 0")
            ->orderBy("aid ASC");

        $paginator = new PaginatorQueryBuilder(array(
            'builder' => $builder,
            'limit' => 10,
            'page' => $currentPage
        ));
        $category = '';
        if( $this->getAlbumCategory() )
        {
            foreach($this->getAlbumCategory() as $k=>$v)
            {
                $category[$v['atid']] = $v;
            }
        }

}

  

  

原文地址:https://www.cnblogs.com/achengmu/p/6890920.html