码迷,mamicode.com
首页 > Web开发 > 详细

ThinkPHP 学习之控制器 ( Controller )

时间:2014-11-17 00:17:06      阅读:821      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   ar   os   使用   sp   

/**
 * ThinkPHP version 3.1.3
 * 部署方式:应用部署 
* 文内的 http://localhost/ 由实际主机地址代替
*/

入口文件 index.php:

bubuko.com,布布扣
<?php

define(‘THINK_PATH‘,‘./ThinkPHP/‘);
//定义项目名称和路径
define(‘APP_NAME‘,‘Myapp‘);
define(‘APP_PATH‘,‘./home/‘);
define(‘APP_DEBUG‘,true);
//加载框架入口文件
require(THINK_PATH."ThinkPHP.php");
index.php

访问入口文件 index.php ( http://localhost/index.php ) 后自动生成 ./home 目录。

bubuko.com,布布扣

index 初始页面的内容由 ./home/Lib/Action/IndexAction.class.php 文件显示:

bubuko.com,布布扣
<?php
// 本类由系统自动生成,仅供测试用途
class IndexAction extends Action {
    public function index(){
    $this->show(‘<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>ThinkPHP</b>!</p></div><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>‘,‘utf-8‘);
    }
}
IndexAction.class.php

( 一 ) 控制器的调用

现在修改 IndexAction.class.php,使用 A 函数调用本项目控制器:

<?php

class IndexAction extends Action {

    public function test(){
    
        $obj = A("Member");
        $obj->user();
    }
}    

A 函数相当于 new,A("Member") 表示实例化本项目中的 Member 控制器,然后再调用 Member 控制器中的 user 方法。user 方法 ( MemberAction.class.php ):

<?php

class MemberAction extends Action{

    public function user(){
    
        $this->display();
    }
}

因为在 Index 控制器 中的 test 动作调用了 Member 控制器的 user 方法 ( 因为此处的 user 动作已被实例化,因此此处不能成为动作,而是作为类的方法 ) 的 $this->display(),所以需要在当前动作 ( test 动作 ) 中输出模板 ( ./home/Tpl/Index/test.html ) :

bubuko.com,布布扣
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>test</title>
</head>
<body>欢迎使用 ThinkPHP</body>
</html>
test.html

通过 PATHINFO 模式的 url ( http://localhost/index.php/index/test ,我的主机地址是127.0.0.26 ) 可以访问 Index 控制器的 test 动作

bubuko.com,布布扣

 

可以使用 R 函数来代替 A 函数,R 函数可以在调用控制时指定调用方法 ( 动作 ),修改 IndexAction.class.php:

<?php

class IndexAction extends Action {

    public function test(){
    
        $obj = R("Member/user"); //使用 R 函数调用本项目的控制器
    }
}

可以达到同样的效果。

 

( 二 ) 空控制器与空动作

空控制器和空动作可以实现错误 404 的功能 ( 但是不能取代服务器错误页面处理机制,空控制器和空动作只能处理 MVC 框架内的页面,而且也不能处理 URL REWRITE 自定义格式的页面 )。

1、空控制器

当用户访问的 URL 不存在需要访问的控制器时,可以使用空控制器,例如访问 http://localhost/index.php/bbs,实际上不存在控制器 BbsAction,系统会给出错误 ( 需要在入口文件 index.php 开启 define(‘APP_DEBUG‘,true); ):

bubuko.com,布布扣

现在在项目目录 ( ./home/Lib/Action ) 下创建控制器类文件 EmptyAction.class.php :

<?php

class EmptyAction extends Action{

    public function index(){
    
        $this->assign("msg","你所查看的栏目已经不存在");
        $this->display("./Public/html/error.html");
    }
}

同时在 WEB 根目录的 Public/html 下创建error.html,这样访问  http://localhost/index.php/bbs 出现的就是自定义的信息内容。

 

2、空动作

相较空控制器定位到栏目,空动作则是定位到具体的页面。如果 Index 控制器中不存在 user 动作,只需要在 Index 控制器中加入空动作,系统会把 _empty 动作代替 user 动作:

<?php

class IndexAction extends Action {

    public function test(){
    
        $obj = R("Member/user");
    }

    public function _empty(){
    
        $this->assign("msg","你所查看的页面不存在");
        $this->display("./Public/html/error.html");
    }
}

 

参考资料:《PHP MVC 开发实战》

 

ThinkPHP 学习之控制器 ( Controller )

标签:style   blog   http   io   color   ar   os   使用   sp   

原文地址:http://www.cnblogs.com/dee0912/p/4102700.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!