Heim  >  Artikel  >  php教程  >  读ThinkPHP的Example有感

读ThinkPHP的Example有感

WBOY
WBOYOriginal
2016-06-13 10:47:17694Durchsuche

前几天下载了ThinkPHP的代码来看,给我的印象没有CodeIgniter(CI)的那么好。或许是因为我下载的是最新的RC版本吧!里面的Examples不全,打开几个提示404,因为比较关心数据库操作方面的代码。另外,跑了一下里面的Blog的Example,功能都挺完善的。不过花了十几分钟看了一下代码,就害怕了。不大喜欢的原因有下:

1、代码很大,这么一个小blog在用了框架之后,还需要敲那么多代码,开发时间也不短吧。

2、把HTML、CSS、Script写在controller里,让controller变得很臃肿,代码也有点混乱。为何不写到View里面去呢?

protected function ajaxUploadResult($info) {
        // Ajax方式附件上传提示信息设置
        // 默认使用mootools opacity效果
        //alert($info);
        $show = '<script language="JavaScript" src="'%20.%20WEB_PUBLIC_PATH%20.%20'/Js/mootools.js"></script><script language="JavaScript" type="text/javascript">&#39; . "\n";
        $show .= &#39; var parDoc = window.parent.document;&#39;;
        $show .= &#39; var result = parDoc.getElementById("&#39; . $info[&#39;uploadResult&#39;] . &#39;");&#39;;
        if (isset($info[&#39;uploadFormId&#39;])) {
            $show .= &#39; parDoc.getElementById("&#39; . $info[&#39;uploadFormId&#39;] . &#39;").reset();&#39;;
        }
        $show .= &#39; result.style.display = "block";&#39;;
        $show .= " var myFx = new Fx.Style(result, &#39;opacity&#39;,{duration:600}).custom(0.1,1);";
        if ($info[&#39;success&#39;]) {
            // 提示上传成功
            $show .= &#39;result.innerHTML = "<div style=\"color:#3333FF\"> 文件上传成功!";&#39;;
            // 如果定义了成功响应方法,执行客户端方法
            // 参数为上传的附件id,多个以逗号分割
            if (isset($info[&#39;uploadResponse&#39;])) {
                $show .= &#39;window.parent.&#39; . $info[&#39;uploadResponse&#39;] . &#39;("&#39; . $info[&#39;uploadId&#39;] . &#39;","&#39; . $info[&#39;savename&#39;] . &#39;");&#39;;
            }
        } else {
            // 上传失败
            // 提示上传失败
            $show .= &#39;result.innerHTML = "<div style=\"color:#FF0000\"> 上传失败:&#39; . $info[&#39;message&#39;] . &#39;";&#39;;
        }
        $show .= "\n" . &#39;</script>';
        //$this->assign('_ajax_upload_',$show);
        header("Content-Type:text/html; charset=utf-8");

        exit($show);
        return;
    }

3、混合使用了Java、 www.2cto.com 微软.Net、PHP三种代码风格(或者确切的说,从Java、微软.Net借鉴了函数、文件或变量的命名风格,但是没有PHP化)。不过在使用上比较一致,出问题几率也不会太大,只是我不是很习惯。

4、在controller代码里写Business Logic和数据库操作。我看Model里的代码基本都很短,看来是基本上博客的功能都写在controller里面去了。比较像Fat Controller的写法,但是数据库的操作写到Model里应该好些吧(按我理解的MVC来说)。Fat Model比Fat Controller多很多好处,便于代码重用。

5、这个要举例说明一下,在看代码的时候,发现一句注释。

if (!empty($id)) {
        $Blog = D("BlogView");
        $result = $Blog->where('Blog.id=' . $id)->find();  // 这里为什么用select()就读不出来
        if ($result) {
            $this->assign('vo', $result);
        } else {
            $this->redirect('index');
            return;
        }
    } else {
        $this->redirect('index');
    }

因为我对数据库操作方面比较关心,之前看过部分ThinkPHP的文档。拜托,写这代码的朋友,TP里select读出来的是记录集,find得到的是记录,你这么assign过去,当然读不出来啦。要把$result改成$result[0]才可以读出来的嘛。这样我感觉TP的Example编写者也对使用者太不负责任了。不过也没多大问题,只是一个RC版本。

6、混合有字符串式拼凑的SQL请求,有些我没读懂!!!可能需要时间深入探究。过多使用这类SQL,会有安全隐患吧(例如SQL注入)。

public function tag() {
        $Tag = M("Tag");
        if (!empty($_GET['name'])) {
            $name = trim($_REQUEST['name']);
            $list = $Tag->where("module='Blog' and name='$name'")->field('id,count')->find();
            $tagId = $list['id'];
            $count = $list['count'];
            import("@.ORG.Page");
            $listRows = 10;
            $fields = 'a.id,a.userId,a.categoryId,a.cTime,a.readCount,a.commentCount,a.title,c.title as category';
            $p = new Page($count, $listRows);
            $p->setConfig('header', '篇日志 ');
            $dao = D("Blog");
            $list = $dao->query("select " . $fields . " from " . C('DB_PREFIX') . 'blog as a,' . C('DB_PREFIX') . 'tagged as b, ' . C('DB_PREFIX') . 'category as c where b.tagId  in (' . $tagId . ') and a.categoryId= c.id and a.status=1  and a.id=b.recordId order by a.id desc limit ' . $p->firstRow . ',' . $p->listRows);
            if ($list) {
                $page = $p->show();
                $this->assign("page", $page);
                $this->assign('list', $list);
            }
            $this->assign('tag', $name);
            $this->assign("count", $count);
        } else {
            $list = $Tag->where("module='Blog'")->select();
            //dump($list);
            $this->assign('tags', $list);
        }
        $this->display();
    }

7、代码风格能体现一个程序员的水平,跟CI的Examples相比,还是有一定差距。注释写的比较随意。我有时写代码,注释也挺随意的。有时候为了尊重其他人,还是需要像写代码一样很大耐心地去写注释。

8、跟代码无关。附带的用户文档不是很人性化,在我本本上都看到字体很大,行距很大,一段简单功能的PHP代码就显示了两页。有些代码为了语法高亮使用了图片,不过图片的质量太低了,可能我玩多了单反。作成HTML其实也方便用户使用、查找的嘛,弄个PDF多了些麻烦。

以上观点仅针对ThinkPHP 3.0 RC1的Example而言。我没有看过核心代码,所以无权评论。同时也没有否定国内MVC框架实力的意思,只不过我觉得,TP的开发者应该在一些细节上多下些功夫,多灌注一些心血,让TP的手册和例子的质量有所提高!

摘自 Xiaoxia

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn