Skip to content

Commit

Permalink
多模块示例代码
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Jan 3, 2017
1 parent 8c04829 commit 962b8f3
Show file tree
Hide file tree
Showing 21 changed files with 483 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/app/cache/*
Empty file added app/.gitignore
Empty file.
101 changes: 101 additions & 0 deletions app/backend/BackendModule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace Marser\App\Backend;

use \Phalcon\Loader,
\Phalcon\Mvc\View,
\Phalcon\DiInterface,
\Phalcon\Mvc\Dispatcher,
\Phalcon\Mvc\ModuleDefinitionInterface;

class BackendModule implements ModuleDefinitionInterface{

public function registerAutoloaders(DiInterface $di=null){

}

/**
* DI注册相关服务
* @param DiInterface $di
*/
public function registerServices(DiInterface $di){
/** DI注册dispatcher服务 */
$this -> registerDispatcherService($di);
/** DI注册url服务 */
$this -> registerUrlService($di);
/** DI注册view服务 */
$this -> registerViewService($di);
}

/**
* DI注册dispatcher服务
* @param DiInterface $di
*/
protected function registerDispatcherService(DiInterface $di){
$config = $di -> get('config');
$di->setShared('dispatcher', function() use ($config) {
$eventsManager = new \Phalcon\Events\Manager();
$eventsManager -> attach("dispatch:beforeException", function($event, $dispatcher, $exception) {
if ($event -> getType() == 'beforeException') {
switch ($exception->getCode()) {
case \Phalcon\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
$dispatcher->forward(array(
'controller' => 'Index',
'action' => 'notfound'
));
return false;
case \Phalcon\Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
$dispatcher->forward(array(
'controller' => 'Index',
'action' => 'notfound'
));
return false;
}
}
});
$dispatcher = new \Phalcon\Mvc\Dispatcher();
$dispatcher -> setEventsManager($eventsManager);
//默认设置为后台的调度器
$dispatcher -> setDefaultNamespace('\\Marser\\App\\Backend\\Controllers');
return $dispatcher;
});
}

/**
* DI注册url服务
* @param DiInterface $di
*/
protected function registerUrlService(DiInterface $di){
$config = $di -> get('config');
$di -> setShared('url', function() use($config){
$url = new \Phalcon\Mvc\Url();
$url -> setBaseUri('/admin/');
$url -> setStaticBaseUri('/admin-assets/');
return $url;
});
}

/**
* DI注册view服务
* @param DiInterface $di
*/
protected function registerViewService(DiInterface $di){
$config = $di -> get('config');
$di -> setShared('view', function() use($config) {
$view = new \Phalcon\Mvc\View();
$view -> setViewsDir(ROOT_PATH . '/app/backend/views');
$view -> registerEngines(array(
'.phtml' => function($view, $di) use($config) {
$volt = new \Marser\App\Core\PhalBaseVolt($view, $di);
$volt -> setOptions(array(
'compileAlways' => false,
'compiledPath' => ROOT_PATH . '/app/cache/compiled/backend'
));
$volt -> initFunction();
return $volt;
},
));
return $view;
});
}
}
Empty file.
15 changes: 15 additions & 0 deletions app/backend/controllers/IndexController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Marser\App\Backend\Controllers;

class IndexController {

public function indexAction(){
echo 'backend index';
}

public function notfoundAction(){
echo '404 not found --- backend';
}

}
Empty file added app/backend/models/.gitignore
Empty file.
11 changes: 11 additions & 0 deletions app/config/routers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

return array(
//后台路由规则
'/admin/:controller/:action/:params' => array(
'module' => 'backend',
'controller'=>1,
'action'=>2
),

);
19 changes: 19 additions & 0 deletions app/config/system.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

return array(
//数据库表配置
'database' => array(
//数据库连接信息
'db' => array(
'host' => '127.0.0.1',
'port' => 3306,
'username' => 'admin',
'password' => 'admin',
'dbname' => 'test',
'charset' => 'utf8',
),

//表前缀
'prefix' => 'test_',
),
);
Empty file added app/core/.gitignore
Empty file.
32 changes: 32 additions & 0 deletions app/core/PhalBaseVolt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Marser\App\Core;

use \Phalcon\Mvc\View\Engine\Volt;

class PhalBaseVolt extends Volt{

/**
* 添加扩展函数
*/
public function initFunction(){
$compiler = $this->getCompiler();

/** 添加str_repeat函数 */
$compiler -> addFunction('str_repeat', 'str_repeat');

/** 添加substr_count函数 */
$compiler -> addFunction('substr_count', 'substr_count');

/** 添加explode函数 */
$compiler -> addFunction('explode', 'explode');

/** 添加array_rand函数 */
$compiler -> addFunction('array_rand', 'array_rand');
}


}



20 changes: 20 additions & 0 deletions app/core/loader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

$loader = new \Phalcon\Loader();

/**
* 注册命名空间
*/
$loader -> registerNamespaces(array(
'Marser' => ROOT_PATH,

'Marser\App\Core' => ROOT_PATH . '/app/core',
'Marser\App\Libs' => ROOT_PATH . '/app/libs',
'Marser\App\Tasks' => ROOT_PATH . '/app/tasks',

'Marser\App\Frontend\Controllers' => ROOT_PATH . '/app/frontend/controllers',
'Marser\App\Frontend\Models' => ROOT_PATH . '/app/frontend/models',

'Marser\App\Backend\Controllers' => ROOT_PATH . '/app/backend/controllers',
'Marser\App\Backend\Models' => ROOT_PATH . '/app/backend/models',
)) -> register();
123 changes: 123 additions & 0 deletions app/core/services.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

/**
* DI注册服务配置文件
* @category PhalconCMS
* @copyright Copyright (c) 2016 PhalconCMS team (http://www.marser.cn)
* @license GNU General Public License 2.0
* @link www.marser.cn
*/

use Phalcon\DI\FactoryDefault,
Phalcon\Mvc\View,
Phalcon\Mvc\Url as UrlResolver,
Phalcon\Db\Profiler as DbProfiler,
Phalcon\Mvc\Model\Manager as ModelsManager,
Phalcon\Mvc\View\Engine\Volt as VoltEngine,
Phalcon\Session\Adapter\Files as Session;

$di = new FactoryDefault();

/**
* 设置路由
*/
$di -> setShared('router', function(){
$router = new \Phalcon\Mvc\Router();
$router -> setDefaultModule('frontend');

$routerRules = new \Phalcon\Config\Adapter\Php(ROOT_PATH . "/app/config/routers.php");
foreach ($routerRules->toArray() as $key => $value){
$router->add($key,$value);
}

return $router;
});

/**
* DI注册session服务
*/
$di -> setShared('session', function(){
$session = new Session();
$session -> start();
return $session;
});

/**
* DI注册cookies服务
*/
$di -> setShared('cookies', function() {
$cookies = new \Phalcon\Http\Response\Cookies();
$cookies -> useEncryption(false);
return $cookies;
});

/**
* DI注册DB配置
*/
$di -> setShared('db', function () use($config) {
$dbconfig = $config -> database -> db;
$dbconfig = $dbconfig -> toArray();
if (!is_array($dbconfig) || count($dbconfig)==0) {
throw new \Exception("the database config is error");
}

$eventsManager = new \Phalcon\Events\Manager();
// 分析底层sql性能,并记录日志
$profiler = new DbProfiler();
$eventsManager -> attach('db', function ($event, $connection) use ($profiler) {
if($event -> getType() == 'beforeQuery'){
//在sql发送到数据库前启动分析
$profiler -> startProfile($connection -> getSQLStatement());
}
if($event -> getType() == 'afterQuery'){
//在sql执行完毕后停止分析
$profiler -> stopProfile();
//获取分析结果
$profile = $profiler -> getLastProfile();
$sql = $profile->getSQLStatement();
$executeTime = $profile->getTotalElapsedSeconds();
//日志记录
$currentDay = date('Ymd');
$logger = new \Phalcon\Logger\Adapter\File(ROOT_PATH . "/app/cache/logs/{$currentDay}.log");
$logger -> debug("{$sql} {$executeTime}");
}
});

$connection = new \Phalcon\Db\Adapter\Pdo\Mysql(array(
"host" => $dbconfig['host'], "port" => $dbconfig['port'],
"username" => $dbconfig['username'],
"password" => $dbconfig['password'],
"dbname" => $dbconfig['dbname'],
"charset" => $dbconfig['charset'])
);

/* 注册监听事件 */
$connection->setEventsManager($eventsManager);

return $connection;
});

/**
* DI注册modelsManager服务
*/
$di -> setShared('modelsManager', function() use($di){
return new ModelsManager();
});

/**
* DI注册日志服务
*/
$di -> setShared('logger', function() use($di){
$currentDay = date('Ymd');
$logger = \Phalcon\Logger\Adapter\File(ROOT_PATH . "/app/cache/logs/{$currentDay}.log");
return $logger;
});

/**
* DI注册配置服务
*/
$di -> setShared('config', function() use($config){
return $config;
});


Loading

0 comments on commit 962b8f3

Please sign in to comment.