框架对PHP来说并不是最重要的。
一个完整的程序能够实现它自己的功能就已经可以了。 如果实现功能的同时考虑到常见问题的解决,那就更完美
下面的问题是一个常见web程序需要考虑到的问题,你的系统考虑到了没有呢?
1、可以方便的替换模板,可以使用替换模板样式文件方式更换主题
2、可以提供多语言
3、可以对模板进行缓存
4、可以方便的生成静态HTML
5、可以实现数据库引擎灵活更换
6、可以方便实现权限控制
7、可以方便实现URL REWRITE
8、可以安全防止代码泄露问题
9、可以快速进行全局内容过滤实现
10、可以方便进行安装、配置
11、可以实现插件、二次开发
12、可以实现代码共享
13、可以实现对外接口开发
….
双向路由配置这个还没有实现。 这个是比较麻烦的东西,要弄到能够比较灵活的实现双向路由的话,不是那么简单的。
里面涉及的东西不是很好搞,估计后面可能会用正则来试试看能不能实现。
当前路由器实现的路由效果有:
www.abc.com/login.html => 跳转到 default > default > login (必须不存在 login>*)
www.abc.com/user/add.html => 跳转到 user > user > add
www.abc.com/user-usergroup.html => 跳转到 user > usergroup > index
www.abc.com/user-usergroup/hello.html => 跳转到 user > usergroup > hello
www.abc.com/user.html => 跳转到 user > user > index
.html 后缀可配置,因此可以实现URL文件类型伪装(例如伪装成 www.abc.com/user.asp)这个估计用来玩玩吧 :D
www.abc.com/user/add.html?name=jack => 这种后面带参数的一律转换成额外的 REQUEST 参数
当然,还实现 默认URL模式, PATHINFO 模式,REWRITE模式的切换,默认model 等的配置,多重POST控制、redirect跳转、alert跳转等小功能啦。
整体感觉还行吧,可以在一定程度上减少 程序员在这方面的考虑,写程序的时候按照模块写,然后传一个参数就OK了。 相对于其他的框架来说,少了很多的额外的配置(虽然额外独立的配置可以提供更加强大的URL REWRITE功能,但是学习成本、开发成本有很大的影响) :D
i think it’s time to publish my framework after serval months developing & debuging… -__-
before that, its needs to have a name for the framework…
en…. “simple” ? no… sb has made it(god bless him).
….en … how about “my way” ?
i really like this song orz
haha … it a bit difficult to have this job done..
it’s jus thinking :)
design to following targets:
1、MVC foundation;
2、modules developing mode, user can build independent mode & has own configuration to their code.
3、clear file structure, easy for manage(like add new modules, delete modules…), also the directory must be simplize;
4、all addons & modules should be driving by configuration. use set different config in different project to realize required function or effect
5、some circuit form like this in base project:request -> router -> filter -> data validate -> data operation -> data cache -> exception catching -> message dispatch & log system -> template engine -> template caching -> output
also the following function is required:javascript validate code auto generate
1、规范的命名规则。
无论你喜欢世界上哪一种命名规则都好,你必须坚持在你的代码里面使用一致的命名规则,还有你的文件目录规则;
2、代码规范。
需要些变量声明的时候就要声明,需要注释就得有注释,甚至到最基本的:需要换行就换行;
3、虽然PHP没有固定的变量类型,但是实际的编程尽量呿固定一个变量类型,例如
function say($name=”, $age=”, $apples){}; 这种代码尽量应该转换成为
function say($name=”, $age=0, $apples=array()){};这样谁看到这样的代码都清楚要传什么东西了。
4、模板分离。
php是一个web server script 语言,一个web 应用大部分都有大量的模板,你必须把这些模板独立写开,然后用到了再 include(‘template.html’)引入进来;这样很大程度在编辑修改的时候方便好多。加入一些模板引擎可以使程序看起来更加整洁;
5、减少使用变量。
$a = ‘hello’;
say($a); 这样的代码如果没有充分理由说明$a的存在的话,为什么不用 say(‘hello’)呢? 还有用的最多的return ,有这个习惯,至少你的代码量可以减少n倍;
6、PHP有的,就用PHP的。
看过一些SB的函数: _get(), _post(), _array_sort()。。。 不知道程序员是不是真的那么有空,那么多内存可以免费使用的。有测试数据,函数的效率比直接写要低 10% 左右;
7、别以为 @是什么好东西。
任何PHP的错误信息都有他存在的理由。加入你的程序在网上运行需要,你可以用一个error_reporting(0)来代替。
8、少点用extract,少点用global 吧。
中国的PHP程序员给DZ带坏了,全局变量满天飞!稍微大一点的程序不知道那些东西丢了要去哪里找。变量给人入侵了还不知道哪里入侵了。 需要的话,建议使用$GLOBALS来代替 global ,然后里面尽量少用不同的key;
9、编码 – 年轻人的处女伤。
文件编码,文件声明编码,流编码,数据库编码,php程序编码,mysql查询编码,apache 编码。当你弄清楚这些的时候,你的程序不会有编码问题了。
10、我们不是打字员,少大一些代码吧。
如果一段代码重复了10遍,你还没有想过消灭它,那你绝对是个100%敬业的打字员了
。。。。学会偷懒一下,有空可以弄弄自己的框架什么的,别在代码堆里面晃悠了。
php可以写的很优美,程序可以弄的很漂亮。上面只是我个人在没有写代码时候发的一点小牢骚。
不过相信做到了这些已经很不错了。至少我眼前的这几位能做到的就不错了。
简单框架使用。
简单框架:编写更少的代码 :>
<?php
App::init();
class App {
private $cur_path;
protected static $ins;
private $class;
private $method;
public $class_label = 'class';
public $method_label = 'method';
public $def_class = 'Main';
public $def_method = 'index';
protected $request_filter = '';
protected function App() {
$this->cur_path = dirname($_SERVER['SCRIPT_FILENAME']).'/';
}
public function getInstance(){
if(!self::$ins){
self::$ins = new self();
}
return self::$ins;
}
protected function getClass(){
if(!empty($_GET[$this->class_label])){
$class = ucfirst(trim($_GET[$this->class_label]));
}
$class = !empty($class) ? $class : $this->def_class;
try {
$class_file = $this->cur_path."$class.class.php";
if(!file_exists($class_file)){
throw new Ex('FILE NO FOUND',$class_file, 3);
} else {
include_once $class_file;
if(!class_exists($class)){
throw new Ex('CLASS NO FOUND', $class, 3);
}
}
} catch (Ex $e){
$e->printEx();
}
return $class;
}
public function link($class='', $method = '', $param = ''){
$url = '?';
$ins = self::getInstance();
$class = !empty($class) ? $class : $ins->getClass();
$method = !empty($method) ? $method : $ins->getMethod();
$url .= $ins->class_label.'='.strtolower($class);
$url .= '&'.$ins->method_label.'='.$method;
if(!empty($param)){
foreach($param as $key=>$p){
$url .= "&$key=$p";
}
}
return $url;
}
public function genFormUrl($class='', $method = ''){
$ins = self::getInstance();
$class = !empty($class) ? $class : $ins->getClass();
$method = !empty($method) ? $method : $ins->getMethod();
$url = '';
$url .= '';
return $url;
}
protected function getMethod(){
if(!empty($_GET['method'])){
$method = trim($_GET[$this->method_label]);
} else {
$method = $this->def_method;
}
try {
if(!method_exists($this->class, $method)){
throw new Ex('METHOD NO FOUND', $this->class, $method, 3);
}
} catch (Ex $e){
$e->printEx();
}
return $method;
}
protected function setInterceptor(){
}
protected function callMethod(){
$ins = new $this->class();
$method = $this->method;
$ins->$method(self::filte($_GET),self::filte($_POST));
}
public function getThemes(){
if(empty($GLOBALS['CFG']['TPL_PATH']) || !empty($GLOBALS['CFG']['THEME_LIST'])){
return null;
}
$hd = dir($GLOBALS['CFG']['TPL_PATH']);
while(($folder = $hd->read()) !== false){
if($folder != '.' && $folder != '..'){
$folder_list[] = $folder;
}
}
$GLOBALS['CFG']['THEME_LIST'] = $folder_list;
}
public function render($tpl_file = '', $client = false){
//read tpl cache
if($GLOBALS['CFG']['TPL_CACHE']){
}
if(!empty($tpl_file)){
$tpl = $GLOBALS['CFG']['TPL_PATH'].$GLOBALS['CFG']['TPL_THEME'].'/'.$tpl_file;
$def_tpl = $GLOBALS['CFG']['TPL_PATH'].'default/'.$tpl_file;
try {
if(!file_exists($tpl) && !file_exists($def_tpl)){
throw new Ex('TPL NO FOUND', $tpl, 3);
}
} catch(Ex $e){
$e->printEx();
}
if($client){
if(file_exists($tpl)){
echo $GLOBALS['CFG']['TPL_WWW'].$GLOBALS['CFG']['TPL_THEME'].'/'.$tpl_file;
} else {
echo $GLOBALS['CFG']['TPL_WWW'].'default/'.$tpl_file;
}
} else {
return file_exists($tpl) ? $tpl : $def_tpl;
}
} else {
$ins = self::getInstance();
$ins->class = $ins->getClass();
$ins->method = $ins->getMethod();
$tpl = $GLOBALS['CFG']['TPL_PATH'].$GLOBALS['CFG']['TPL_THEME'].'/'.strtolower($ins->class).'_'.strtolower($ins->method).'.php';
$def_tpl = $GLOBALS['CFG']['TPL_PATH'].'default/'.strtolower($ins->class).'_'.strtolower($ins->method).'.php';
try {
if(!file_exists($tpl) && !file_exists($def_tpl)){
throw new Ex('TPL NO FOUND', $tpl, 3);
}
} catch(Ex $e){
$e->printEx();
}
return file_exists($tpl) ? $tpl : $def_tpl;
}
}
public function init(){
register_shutdown_function(array(App, 'run'));
}
public function run(){
$ins = self::getInstance();
$ins->getThemes();
$ins->class = $ins->getClass();
$ins->method = $ins->getMethod();
$ins->setInterceptor();
$ins->callMethod();
}
public function load($class_name = ''){
if(empty($class_name) || empty($GLOBALS['CFG']['CLASS_PATH'])){
return null;
}
$class_name = ucfirst($class_name);
$class_file = $GLOBALS['CFG']['CLASS_PATH'].$class_name.'.class.php';
if(file_exists($class_file)){
include_once $class_file;
return new $class_name();
} else {
return null;
}
}
public function filte($var, $rule='html', $defvar = ''){
$return = '';
if(is_array($var)){
foreach($var as $key=>$item){
$return[$key] = self::filte($var[$key], $rule, $defvar);
}
} else {
$rule = strtolower($rule);
$var = trim($var);
//include regular rule file
$reg_code_file = dirname(__FILE__).DIRECTORY_SEPARATOR.'reg.inc.php';
if(file_exists($reg_code_file)){
$reg_list = include($reg_code_file);
} else {
$reg_list = array();
}
switch($rule){
//regular mode
case in_array($rule, array_keys($reg_list)):
preg_match($reg_list[$rule], $var, $match);
$return = $match[0];
break;
//string compact mode
case 'html':
$return = str_ireplace('';
}
if(empty($url)){
$html .= '';
} else {
$html .= '';
}
} else if(file_exists($tpl)){
include_once $tpl;
return true;
} else {
$html = '' .
'' .
' "Content-Type" content="text/html; charset=utf-8">';
$html .= '
';
$html .= $msg;
if (!empty ($url))
$html .= '页面正在自动跳转这个地址,请稍候...' . $sec . ' 秒,';
$html .= '';
$html .= '';
}
echo $html;
exit;
}
public function cache(){
}
}
?>
simple fw 新版 App – sobizz – 一葉之秋simple fw 新版 App – sobizz – 一葉之秋