thinkphp3.x自定義Action、Model及View的簡單實(shí)現(xiàn)方法

字號(hào):


    本文實(shí)例講述了thinkphp3.x自定義Action、Model及View的實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
    1、在xmall/Lib/Action中創(chuàng)建文件TestAction.class.php
    class TestAction extends Action{
       function index(){
        $this->display("test");
       }
    }
    2、在xmall/tpl下創(chuàng)建default文件夾,在default下創(chuàng)建Test文件夾,在Test下創(chuàng)建test.html模版文件;
    3、執(zhí)行URL:http://localhost/xmall/index.php/Test/index就會(huì)出現(xiàn)test.html頁面的內(nèi)容
    4、在操作過程中出現(xiàn)的錯(cuò)誤:
    (1) URL中的Test的T要大寫;
    (2) Display不需要提供文件的擴(kuò)展名,默認(rèn)為index
    5、注意事項(xiàng):
    (1) 為方便調(diào)試,應(yīng)在index.php入口文件中添加
    define("APP_DEBUG",true);
    (2) 最好在配置文件(xmall/Conf/config.php)中指定默認(rèn)模版:'DEFAULT_THEME' => 'default'
    6、在xmall/lib/Model下創(chuàng)建文件UserModel.class.php
    class UserModel extends Model{
       function test(){
         return "123456";
       }
    }
    7、在xmall/Lib/Action/TestAction.class.php添加新方法
    public function test(){
       $m=D("User");
       echo $m->test();
    }
    8、執(zhí)行URL:http://localhost/xmall/index.php/Index/test,頁面輸出123456
    9、注:Model文件名要與model的名稱一直,并且在調(diào)用時(shí)區(qū)分大小寫;
    在xmall/conf/config.php中添加'URL_CASE_INSENSITIVE' =>true,//URL不區(qū)分大小寫
    希望本文所述對(duì)大家基于ThinkPHP框架的PHP程序設(shè)計(jì)有所幫助。