作者:手机用户2702932521 | 来源:互联网 | 2023-08-10 13:59
范例1:路由规则和分组支持绑定模型数据,例如:usethink\Route;Route::rule(hello:id,indexindexhello,GET,[ext
范例1:
路由规则和分组支持绑定模型数据,例如:
use think\Route;
Route::rule('hello/:id','index/index/hello','GET',[
'ext' => 'html',
'bind_model' => [
'user' => '\app\index\model\User',
],
]);
会自动给当前路由绑定 id 为 当前路由变量值的User 模型数据(需要手动建立/var/www/think/apps/index/model/User.php和/var/www/think/apps/index/controller/Index.php)。
// 我们为think_user 表定义一个User 模型(位于apps/index/model/User.php )如下:
namespace app\index\model;
use think\Model;
class User extends Model
{
}
//大多情况下,我们无需为模型定义任何的属性和方法即可完成基础的操作。
namespace app\index\controller;
use think\Controller;
class Index extends Controller
{
public function hello($id)
{
$user = $this->request->user;
return $user;
}
}
// hello GET http://contoso.org/hello/3.html
/*
{"id":3,"nickname":"李四","name":"Python","birthday":226338100,"email":"lisi@163.com","password":"123456","create_time":"2017-07-30 10:45:19","update_time":"2017-07-30 10:45:19","status":1}
*/
范例2:
use think\Route;
//可以定义模型数据的查询条件,例如:
Route::rule('hello/:name/:status','index/index/hello','GET',[
'ext' => 'html',
'bind_model' => [
'user' => ['\app\index\model\User','status&name']
],
]);
//表示查询id 和name 的值等于当前路由变量的模型数据。
// 我们为think_user 表定义一个User 模型(位于apps/index/model/User.php )如下:
namespace app\index\model;
use think\Model;
class User extends Model
{
}
//大多情况下,我们无需为模型定义任何的属性和方法即可完成基础的操作。
namespace app\index\controller;
use think\Controller;
class Index extends Controller
{
public function hello()
{
$user = $this->request->user;
return $user;
}
}
// hello GET http://contoso.org/hello/Go/2.html
请求实际提交的SQL语句:
SELECT * FROM `think_user` WHERE `status` = 2 AND `name` = 'Go' LIMIT 1 // 实际上数据库表里还有一条记录满足查询条件的数据:
{"id":6,"nickname":"孙七","name":"Go","birthday":226336400,"email":"sunqi@163.com","password":"123456","create_time":"2017-07-30 10:45:19","update_time":"2017-07-30 10:45:19","status":2}
/*
{"id":5,"nickname":"赵六","name":"Go","birthday":226359300,"email":"zhaoliu@126.com","password":"123456","create_time":"2017-07-30 10:45:19","update_time":"2017-07-30 10:45:19","status":2}
*/
范例3:
use think\Route;
//也可以使用闭包来返回模型对象数据
Route::rule('hello/:name/:status','index/index/hello','GET',[
'ext' => 'html',
'bind_model' => [
'user' => function($param){
$model = new \app\index\model\User;
return $model->where($param)->select();
}
],
]);
//闭包函数的参数就是当前请求的URL变量信息。
namespace app\index\controller;
use think\Controller;
class Index extends Controller
{
public function hello()
{
$user = $this->request->user;
return $user;
}
}
// hello GET http://contoso.org/hello/Go/2.html
/*
[{"id":5,"nickname":"赵六","name":"Go","birthday":226359300,"email":"zhaoliu@126.com","password":"123456","create_time":"2017-07-30 10:45:19","update_time":"2017-07-30 10:45:19","status":2},{"id":6,"nickname":"孙七","name":"Go","birthday":226336400,"email":"sunqi@163.com","password":"123456","create_time":"2017-07-30 10:45:19","update_time":"2017-07-30 10:45:19","status":2}]
*/
如果范例3执行出现如下错误:
解决办法1:
return $user; 将数组类型的返回值转换成json类型的返回值,此行代码换成 return json($user); 即可解决;
解决办法2:
在config.php配置文件中修改默认的数据输出类型
// 默认输出类型改成下面返回类型即可解决
'default_return_type' => 'json',
// 默认输出类型不出错的搭配方案如下:
搭配方案1 'default_return_type' => 'html', 控制器方法内返回值必须是 return json($user);
搭配方案2 'default_return_type' => 'json', 控制器方法内返回值必须是 return $user;