MARS FRAMEWORK 备忘
星期一, 五月 16th, 2011路径结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | # /configs 全局config路径 # /libs 全局库路径 # /locales 全局locales路径 # /modules 模块路径 # /modules/index 默认模块,module=default # /modules/user1 自定义模块,module=user1 # /modules/user1/app1 自定义模块(user1)下的app1,app=app1 # /modules/user1/app1/controller 自定义模块(user1)下的app1,controller路径 # /modules/user1/app1/controller/controller1.php 自定义模块(user1)下的app1的controller1 # /modules/user1/app1/views 自定义模块(user1)的app1下的view路径 # /modules/user1/app1/views/controler1 自定义模块(user1)下app1的controller=controller1的view路径 # /modules/user1/configs 自定义模块(user1)的configs路径 # /modules/user1/bootstrap.php 自定义模块(user1)的启动文件 # /public document_root # /public/index.php 入口 # /tests 测试程序 # /bootstrap.php 全局启动路径 |
/public/.htaccess
1 2 3 4 5 | RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] |
/public/index.php
1 | <!--?php include("../bootstrap.php"); $route = Mars_Router::getInstance(); $route--->initialize(); |
Mars_BaseController
所有controller的基类,action响应用func_action(void)获取
4种渲染方法,通过$controller->frontend设置:
- HTML
- JSON
- TEXT(PLAINTEXT)
- XML
默认frame view使用/views/frame.phtml,默认view使用该模块/app/views/controller/action.phtml
如要使用特殊的frame view或view请用
$controller->_frameView
$controller->_view
进行设置
JSON View
相关变量$controller->return是返回数据,$controller->success标示是否成功
如:
1 2 3 4 5 6 | function test_action() { $this->frontend = Mars_FrontEnd::JSON; $this->return = array(1, 2); $this->success = true; } |
返回:
1 | {"success":true,"data":[1,2]} |
TEXT View
1 2 3 4 5 | function text_action() { $this->frontend = Mars_FrontEnd::TEXT; $this->return = "hello world"; } |
返回
1 | hello world |
XML View
1 2 3 4 5 |
返回:
1 2 | <?xml version="1.0" encoding="UTF-8"?> <data><foobar>1</foobar><foobar>2</foobar><foobar>3</foobar></data> |
Mars_BaseController->cache
cache是符合memcache类的实例,将会存储output结果,在缓存存在时直接输出缓存
cache_key由uri及$_GET中的参数组成,如$_GET中有__refresh参数则会刷新缓存,或controller中有一成员变量标示$action.”_refresh”=true/false可以强制不使用或使用缓存。
Mars_EventManager 事件处理
插入事件点(Event Point),如:
1 | Mars_EventManager::register("ACTION_BEGIN", new Mars_Event(Mars_Event::ACTION_BEGIN, $this)); |
在bootstrap.php中添加事件侦听
1 | Mars_EventManager::getInstance()->addEventListener(Mars_Event::BEGIN, callback_function); |
callback_function有一个参数,即Mars_Event
默认支持以下事件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | # router begins const BEGIN = "BEGIN"; # router ends const END = "END"; # controller class not found const CLASS_NOT_FOUND = "CLASS_NOT_FOUND"; # controller's action not found const ACTION_NOT_FOUND = "ACTION_NOT_FOUND"; # controller's action begins const ACTION_BEGIN = "ACTION_BEGIN"; # controller's action ends const ACTION_END = "ACTION_END"; # render view begins const RENDER_BEGIN = "RENDER_BEGIN"; # render view ends const RENDER_END = "RENDER_END"; # launch begins const LAUNCH_BEGIN = "LAUNCH_BEGIN"; # launch ends const LAUNCH_END = "LAUNCH_END"; # view output begin const FILTER_OUTPUT = "FILTER_OUTPUT"; |
前置和后置过滤器
1 2 3 4 5 6 7 8 9 10 11 | function test_filter($c, $t, $in, $out) { return str_replace("A", "BB", $c); } Mars_PostFilter::getInstance()->register("test_filter"); function test_prefilter($param) { $_GET['xxxx'] = $param; } Mars_PreFilter::getInstance()->register(array('function'=>'test_prefilter', 'params'=>array('a'))); |
