目录结构如下
public/index.php:
use Phalcon\Mvc\Router;
use Phalcon\Tag;
use Phalcon\Mvc\Url;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Application;
use Phalcon\DI\FactoryDefault;
use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter; // Create a DI
$di = new FactoryDefault(); $di->set('router', function () { $router = new Router(); $router->setDefaultModule("frontend"); $router->setDefaults(array(
"namespace" => 'Multiple\Frontend\Controllers',
"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'=>'Multiple\Backend\Controllers', // 一定要加上,要不然找不到后台
'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' => 'Multiple\Frontend\Module',
'path' => '../app/frontend/Module.php',
),
'backend' => array(
'className' => 'Multiple\Backend\Module',
'path' => '../app/backend/Module.php',
)
)
);
echo $application->handle()->getContent();
} catch (Exception $e) {
echo "Exception: ", $e->getMessage();
}
frontend/module.php(backend/module.php的配置跟它的雷同):
<?php
namespace Multiple\Frontend; use Phalcon\Loader,
Phalcon\Mvc\Dispatcher,
Phalcon\Mvc\View,
Phalcon\Mvc\ModuleDefinitionInterface; class Module implements ModuleDefinitionInterface { public function registerAutoloaders( \Phalcon\DiInterface $di = NULL)
{
$loader = new Loader();
$loader->registerNamespaces(array(
'Multiple\Frontend\Controllers' => __DIR__ . '/controllers/',
'Multiple\Models' => __DIR__ . '/../models/'
))->register();
$loader->registerDirs(
array(
'modelsDir' => '../app/models/',
)
)->register(); } public function registerServices( \Phalcon\DiInterface $di)
{
$di->set("dispatcher", function(){
$dispatcher = new Dispatcher();
$dispatcher->setDefaultController("Multiple\Frontend\Controllers");
return $dispatcher;
}); $di->set("view", function(){
$view = new View();
$view->setViewsDir("../app/frontend/views/");
$view->registerEngines(array(
'.phtml' => 'Phalcon\Mvc\View\Engine\Php'
));
return $view;
}); }
}
frontend/controllers/indexController.php:
namespace Multiple\Frontend\Controllers;
//use Multiple\Models\Level;
class IndexController extends \Phalcon\Mvc\Controller { public function indexAction()
{
$level = Level::findFirst("id = 10");
$this->view->pick('index/index');
} }
models/Level.php
//namespace Multiple\Models;
class Level extends \Phalcon\Mvc\Model
{ public $lid;
public $name;
public $description;
public $create_time; public function beforeCreate()
{
if( is_null($this->create_time) )
{
$this->create_time = time();
} }
}
那么多模块下,如何分页的,如果直接用->from(表名),报错找不到此类,此时要引用model类的全命名空间名称如下:
Multiple\Models\Level(表)。
namespace Multiple\Backend\Controllers;
use Phalcon\Paginator\Adapter\QueryBuilder;
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');
} }