1.在配置中开启路由完整匹配
~~~
// 路由使用完整匹配
//'route_complete_match' => false,
'route_complete_match' => true,
~~~
2.书写主题路由
~~~
Route::get('api/:version/theme','api/:version.Theme/getSimpleList');
Route::get('api/:version/theme/:id','api/:version.Theme/getComplexOne');
~~~
3.新建Product.php模型
~~~
<?php
namespace app\api\model;
class Product extends BaseModel
{
}
~~~
4.新建Theme.php模型
~~~
<?php
namespace app\api\model;
class Theme extends BaseModel
{
protected $hidden = ['delete_time', 'topic_img_id', 'head_img_id'];
public function topicImg()
{
return $this->belongsTo('Image', 'topic_img_id', 'id');
}
public function headImg()
{
return $this->belongsTo('Image', 'head_img_id', 'id');
}
//关联product,多对多关系
public function products()
{
return $this->belongsToMany(
'Product', 'theme_product', 'product_id', 'theme_id');
}
}
~~~
5.更改BaseValidate.php验证器
~~~
<?php
namespace app\api\validate;
use app\lib\exception\ParameterException;
use think\Request;
use think\Validate;
class BaseValidate extends Validate
{
public function goCheck()
{
// 获取http传入的参数
// 对这些参数做检验
$request = Request::instance();
$params = $request->param();
$result = $this->batch()->check($params);
if(!$result){
$e =new ParameterException();
$e->msg=$this->error;
throw $e;
//$error = $this->error;
//throw new Exception($error);
}
else{
return true;
}
}
protected function isPositiveInteger($value, $rule='', $data='', $field='')
{
if (is_numeric($value) && is_int($value + 0) && ($value + 0) > 0) {
return true;
}
return false;
//return $field . '必须是正整数';
}
}
~~~
6.更改IDMustBePostiveInt.php验证器
~~~
<?php
namespace app\api\validate;
use think\Validate;
class IDMustBePostiveInt extends BaseValidate
{
protected $rule = [
'id' => 'require|isPositiveInteger',
// 'num'=>'in:1,2,3'
];
protected $message = [
'id' => 'id必须是正整数'
];
}
~~~
7.新建IDCollection.php验证器
~~~
<?php
namespace app\api\validate;
class IDCollection extends BaseValidate
{
// 千万不要在require | checkIDS中加空格
// 不然你会哭的
// 源码中是没有去处多余空格的判断的
// 这将导致验证不执行
protected $rule = [
'ids' => 'require|checkIDs'
];
protected $message = [
'ids' => 'ids参数必须为以逗号分隔的多个正整数,仔细看文档啊'
];
protected function checkIDs($value)
{
$values = explode(',', $value);
if (empty($values)) {
return false;
}
foreach ($values as $id) {
if (!$this->isPositiveInteger($id)) {
// 必须是正整数
return false;
}
}
return true;
}
}
~~~
8.新建ThemeException .php异常类
~~~
<?php
namespace app\lib\exception;
class ThemeException extends BaseException
{
public $code = 404;
public $msg = '指定主题不存在,请检查主题ID';
public $errorCode = 30000;
}
~~~
9.新建Theme.php控制器
~~~
<?php
namespace app\api\controller\v1;
use app\api\validate\IDCollection;
use app\api\model\Theme as ThemeModel;
use app\lib\exception\ThemeException;
use think\Controller;
class Theme extends Controller
{
public function getSimpleList($ids = '')
{
$validate = new IDCollection();
$validate->goCheck();
$ids = explode(',', $ids);
$result = ThemeModel::with('topicImg,headImg')->select($ids);
if (!$result) {
throw new ThemeException();
}
return $result;
}
public function getComplexOne($id)
{
return "1111";
}
}
~~~
结果
![](https://box.kancloud.cn/18864cfcab9332a9c024750ce7e63ba1_944x661.png)
![](https://box.kancloud.cn/77484c3469f11d155e51bd1484b134fb_660x507.png)