phalcon:整合官方多模块功能,方便多表查询
phalcon:整合官方多模块功能,方便多表查询
项目分为:
namespace Multiple\Backend;
namespace Multiple\Frontend;
目录结构如下:
public/index.php的大致写法:
多模块功能:
// Handle the request $application = new Application($di); //加入模块分组配置 $application->registerModules( array( 'frontend' => array( 'className' => 'Multiple\Frontend\Module', 'path' => '../app/frontend/Module.php', ), 'backend' => array( 'className' => 'Multiple\Backend\Module', 'path' => '../app/backend/Module.php', ) ) );
来看下多模块下Module.php的写法,
backend/Module.php
namespace Multiple\Backend; use Phalcon\Loader, Phalcon\Mvc\Dispatcher, Phalcon\DiInterface, Phalcon\Mvc\View, Phalcon\Mvc\ModuleDefinitionInterface; class Module implements ModuleDefinitionInterface { public function registerAutoloaders( DiInterface $di = NULL) { $loader = new Loader(); $loader->registerNamespaces(array( 'Multiple\Backend\Controllers' => __DIR__ .'/controllers/' ))->register(); $loader->registerDirs( array( 'modelsDir' => '../app/models/', #注意这里,必须填写,否则models/下的文件不能共用。 ) )->register(); } public function registerServices( \Phalcon\DiInterface $di) { $di->set("dispatcher", function(){ $dispatcher = new Dispatcher(); $dispatcher->setDefaultController("Multiple\Backend\Controllers"); return $dispatcher; }); $di->set("view", function(){ $view = new View(); $view->setViewsDir("../app/backend/views/"); $view->registerEngines(array( '.phtml' => 'Phalcon\Mvc\View\Engine\Php' )); return $view; }); } }
models/下的model文件,不需要命名空间,直接写:
use \Phalcon\Mvc\Model; class Album extends Model { ...... }
controllers/下面的model调用:
namespace Multiple\Backend\Controllers; use Phalcon\Paginator\Adapter\QueryBuilder 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; } } }