1.连上数据库
~~~
// 数据库名
'database' => 'xcx',
// 用户名
'username' => 'root',
// 密码
'password' => '123456',
~~~
2.完善路由
~~~
Route::get('api/v1/banner/:id','api/v1.Banner/getBanner');
~~~
3.model中Banner.php代码
~~~
<?php
namespace app\api\model;
use think\Db;
use think\Model;
class Banner
{
public static function getBannerById($id)
{
$result=Db::query('select * from banner_item where banner_id=?',[$id]);
return $result;
}
}
~~~
3.操作器中Banner.php代码
~~~
<?php
namespace app\api\controller\v1;
use app\api\validate\IDMustBePostiveInt;
use app\api\model\Banner as BannerModel;
use app\lib\exception\BannerMissException;
class Banner{
//获取指定id的banner信息
public function getBanner($id)
{
(new IDMustBePostiveInt())->goCheck();
$banner=BannerModel::getBannerByID($id);
if(!$banner){
throw new BannerMissException();
}
return json($banner);
}
}
~~~
结果
![](https://box.kancloud.cn/b448947c9f9cedcb309fa73e290ec67c_929x490.png)
![](https://box.kancloud.cn/cfdb8aa420462008106942873912b076_1130x560.png)