I downloaded the ThinkPHP code a few days ago and looked at it. My impression was not as good as that of CodeIgniter (CI). Maybe it’s because I downloaded the latest RC version! The Examples inside are not complete, so I opened a few 404 prompts because I am more concerned about the code for database operations. In addition, I ran through the Blog Example and found that the functions are quite complete. But after spending more than ten minutes looking at the code, I was scared. The reasons why I don’t like it very much are as follows:
1. The code is very large. After using the framework, such a small blog still needs to type so much code, and the development time is not short.
2. Write HTML, CSS, and Script in the controller, which makes the controller bloated and the code a bit confusing. Why not write it into View?
protected function ajaxUploadResult($info) { // Ajax方式附件上传提示信息设置 // 默认使用mootools opacity效果 //alert($info); $show = '<script language="JavaScript" src="' . WEB_PUBLIC_PATH . '/Js/mootools.js"></script><script language="JavaScript" type="text/javascript">' . "\n"; $show .= ' var parDoc = window.parent.document;'; $show .= ' var result = parDoc.getElementById("' . $info['uploadResult'] . '");'; if (isset($info['uploadFormId'])) { $show .= ' parDoc.getElementById("' . $info['uploadFormId'] . '").reset();'; } $show .= ' result.style.display = "block";'; $show .= " var myFx = new Fx.Style(result, 'opacity',{duration:600}).custom(0.1,1);"; if ($info['success']) { // 提示上传成功 $show .= 'result.innerHTML = "<div style=\"color:#3333FF\"> 文件上传成功!</div>";'; // 如果定义了成功响应方法,执行客户端方法 // 参数为上传的附件id,多个以逗号分割 if (isset($info['uploadResponse'])) { $show .= 'window.parent.' . $info['uploadResponse'] . '("' . $info['uploadId'] . '","' . $info['savename'] . '");'; } } else { // 上传失败 // 提示上传失败 $show .= 'result.innerHTML = "<div style=\"color:#FF0000\"> 上传失败:' . $info['message'] . '</div>";'; } $show .= "\n" . '</script>'; //$this->assign('_ajax_upload_',$show); header("Content-Type:text/html; charset=utf-8"); exit($show); return; }
3. Mixed use of Java, www.2cto.com Microsoft.Net, and PHP three coding styles (or to be precise, borrowed the naming style of functions, files, or variables from Java and Microsoft.Net, but without PHP change). However, it is relatively consistent in use and the probability of problems is not too great, but I am not very used to it.
4. Write Business Logic and database operations in the controller code. I see that the code in the Model is basically very short. It seems that basically all the functions of the blog are written in the controller. It's more like the way Fat Controller is written, but it should be better to write the database operations in the Model (according to my understanding of MVC). Fat Model has many advantages over Fat Controller and facilitates code reuse.
5. Let me give you an example. When I was looking at the code, I found a comment.
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'); }
Because I am more concerned about database operations, I have read some ThinkPHP documents before. Please, friends who wrote this code, what you select in TP reads is a record set, and what you get in find is a record. Of course you can't read it if you assign it like this. You have to change $result to $result[0] to read it out. In this way, I feel that the TP Example writer is too irresponsible to the users. But it’s not a big problem, it’s just an RC version.
6. SQL requests mixed with strings, some of which I didn’t understand! ! ! It may take time to delve deeper. Excessive use of this type of SQL may cause security risks (such as SQL injection).
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. Coding style can reflect the level of a programmer. Compared with CI Examples, there is still a certain gap. The comments are written more casually. I sometimes write code and the comments are quite casual. Sometimes in order to respect other people, you still need to write comments with the same patience as writing code.
8. It has nothing to do with code. The accompanying user documentation is not very user-friendly. On my laptop, I saw that the fonts are very large and the line spacing is very large. A simple PHP code only displays two pages. Some codes use pictures for syntax highlighting, but the quality of the pictures is too low. Maybe I play too much with DSLRs. Creating HTML is actually convenient for users to use and search, but creating a PDF is a little more troublesome.
The above opinions are only for the Example of ThinkPHP 3.0 RC1. I haven't seen the core code, so I can't comment. At the same time, I don’t mean to deny the strength of the domestic MVC framework, but I think that the developers of TP should work more on some details and put more effort into improving the quality of TP’s manuals and examples!
Excerpted from Xiaoxia