官方提供的两种实现方式,最新文档地址:
http://www.kancloud.cn/manual/thinkphp/1879一、两种方式的实现:第1种:
http://localhost/user/login  用户登录,自动判断请求类型返回相应的资源。
http://localhost/user/reg     用户注册,自动判断请求类型返回相应的资源。第2种:http://locahost/user/1.json  读取用户(ID为1)的信息返回json类型
http://locahost/user/1.xml   读取用户(ID为1)的信息返回xml类型
http://locahost/user/1.html 读取用户(ID为1)的信息返回html类型二、路由配置说明如下:{
    //路由
    'URL_ROUTER_ON' => true, // 开启URL路由
    'URL_ROUTE_RULES' => array(array('user/:id','user/read',array('ext'=>'xml','method'=>'get'))
)注意:以上配置有个问题,就是这句加不加都行:array('ext'=>'xm得出结论:采用read_get_xm
即:通过http://locahost/user/1.json这种网址访问的时候,最终好像是程序根据后缀名判断的,好像路由没起作用。
三、直接贴代码吧:
1、控制器代码
<?php
namespace Home\Controller;
use Think\Controller\RestController;
class UserController extends RestController {
    /**
     * 读取用户信息
     */
    Public function read_get_json(){
        echo "read_get_json";
    }
    Public function read_get_xml(){
        echo "read_get_xml";
    }
   //官方文档中未直接说明的地方:如果去掉上面的read_get_xml(),访问locahost/user/1.xml时,系统将自动访问以下read_xml()方法。
   Public function read_xml(){
        echo "read_xml";
    }
    Public function read_post_json(){
        echo "read_post_json";
    }
    Public function read_post_xml(){
        echo "read_post_xml";
    }
    /**
     * 登录认证
     @param username 用户名
     @param password 密码
     @return 成功:{"code":200,"msg":"登录成功"}
     @return 失败:{"code":401,"msg":"用户名或密码错误!"}
     */
    Public function login(){
        switch ($this->_method){
            case 'get':
                break;
            case 'put':
                break;
            case 'post':
                $data['uname'] =I('post.username');
                $data['pwd']   =I('post.password');
                $result=M('user')->where($data)->find();
                if(false == $result) {
                    $value['code']    =401;
                    $value['msg'] ='用户名或密码错误!';
                    $this->response($value,'json');
                }else {
                    if($result['pwd'] != I('post.password')) {
                        $value['code']    =401;
                        $value['msg'] ='用户名或密码错误!';
                        $this->response($value,'json');
                    }
                    $value['code']=200;
                    $value['msg']="登录成功!";
                }
                $this->response($value,'json');
                break;
        }
    }
    /**
     * 新用户注册
     @param username 用户名
     @param password 密码
     @return 成功:{"code":200,"msg","注册成功!"}
     @return 失败1:{"code":403,"msg","用户名已被抢注,请换一个试试!"}
     @return 失败2:{"code":500,"msg","注册似乎遇到问题!"}
     */
    Public function reg(){
        switch ($this->_method){
            case 'get':
                break;
            case 'put':
                break;
            case 'post':
                $userModel = M('user');
                $data['uname'] =I('post.username');
                //是否被抢注
                $FinduserModel = $userModel->getFieldByuname($data['uname'],"id");
                if($FinduserModel){
                    $value['code']=403;
                    $value['msg']="用户名已被抢注,请换一个试试!";
                    $this->response($value,'json');
                }
                $data['pwd']   =I('post.password');
                $data['ctime'] =time();
                $data['ltime'] =time();
                $result=$userModel->add($data);
                if($result){
                    $value['code'] =200;
                    $value['msg']  ="注册成功!";
                }else{
                    $value['code'] =500;
                    $value['msg']  ="注册遇到问题!";
                }
                $this->response($value,'json');
                break;
        }
    }
}2、测试post时的测试代码,很简单 <!DOCTYPE html>
 <html>
 <head>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     <title>测试</title>
     <style type="text/css">
     div{
         margin-top:30px;
     }
     </style>
 </head>
 <body>
     <div align="center">
        <p align="center">测试get</p>
        <a href="/user/1.json">get提交json</a>
        <a href="/user/1.xml">get提交xml</a>
     </div>
    <div align="center">
        <p align="center">测试post</p>
         <form method="post" name="" action="/user/1.json">
            <input type="submit" value="post提交json"/>
         </form>
        <form method="post" name="" action="/user/1.xml">
            <input type="submit" value="post提交xml"/>
        </form>
    </div>
 </body>
 </html>有不对的地方,欢迎指出,谢谢~~		最佳答案
		