以前咱们已经准备好了基本的安装过程 如今咱们去实现一下具体的业务部分php
对于用户注册 这对于一款应用来讲再正常不过了 为了接下来咱们的效果 咱们能够去生成一个UserController
laravel
即在项目终端执行chrome
$ php artisan make:controller App\\Api\\Controllers\UserController
生成用户以后咱们暂时先不去编辑字段 后面咱们须要用到时再加json
返回字段数据沿用以前建立的Lesson Model
这里咱们先在routes/api.php
加上咱们的基本路由api
Route::middleware('auth:api')->get('/user', function (Request $request) { return $request->user(); }); $api = app('Dingo\Api\Routing\Router'); $api->version('v1', function ($api) { $api->group(['namespace' => 'App\Api\Controllers'], function ($api) { $api->get('lessons', 'LessonsController@index'); $api->get('lessons/{id}', 'LessonsController@show'); }); });
咱们就去访问这个http://host/api/lessons
先去完成控制器的相关方法app
public function index() { $lessons = Lesson::all(); return response()->json($data); }
这只是简单的返回Lessons
的所有字段信息 可是咱们能够看到已经返回了全部的字段信息less
可是这个时候有个问题就是 咱们的字段返回时不多是所有返回的 咱们确定会对一些字段进行筛选 这就用到了咱们以前建立的Transformers
这个文件下的了工具
话很少说 在App/Api/Transformer
s目录下新建LessonTranformer
post
use App\Lesson; use League\Fractal\TransformerAbstract; class LessonTransformer extends TransformerAbstract { public function transform(Lesson $lesson) { return [ 'title' => $lesson['title'], 'content' => $lesson['body'], 'is_free' => (boolean)$lesson['free'] ]; } }
这样咱们就实现了对字段数据的映射 再去LessonController去对以前的全部字段进行映射测试
public function index() { $lessons = Lesson::all(); return $this->collection($lessons,new LessonTransformer()); }
固然这个要想实现这个collection
咱们须要使用Dingo
提供给咱们的Helpers trait
这样一来的话咱们以前所继承的基类能够这样写
use Dingo\Api\Routing\Helpers; class Controller extends BaseController { use Helpers,AuthorizesRequests, DispatchesJobs, ValidatesRequests; }
这样咱们返回的就只有title
content
is_free
这三个字段
一样的若是咱们想要获取其中一条数据的话 咱们也是能够这样去映射字段的
public function show($id) { $lesson = Lesson::find($id); if(!$lesson){ return $this->response()->errorNotFound("Lesson nt found"); } return $this->response()->item($lesson,new LessonTransformer()); }
这里我用的是Dingo
提供给的返回方法 若是没有这条数据会返回404和对应的错误信息
下面就是 结合jwt的auth认证
为此咱们在App\Api\Controllers
目录下新建一个AuthController
这里咱们须要去编写咱们的认证方法和获取认证用户的方法
新建认证
public function authenticate(Request $request) { // grab credentials from the request $credentials = $request->only('email','password'); try { // attempt to verify the credentials and create a token for the user if (! $token = JWTAuth::attempt($credentials)) { return response()->json(['error' => 'invalid_credentials'], 401); } } catch (JWTException $e) { // something went wrong whilst attempting to encode the token return response()->json(['error' => 'could_not_create_token'], 500); } // all good so return the token return response()->json(compact('token')); }
为了执行这个方法 能够去路由中定义
$api->version('v1', function ($api) { $api->group(['namespace' => 'App\Api\Controllers'], function ($api) { $api->post('user/login','AuthController@authenticate'); $api->post('user/register','AuthController@register'); }); });
这个时候再去查看一下咱们的路由的话就会看到新定义的post
路由
为了验证请求的结果 咱们能够使用postman这个chrome工具 去请求http://localhost:8000/api/use...
这个时候是会返回{"error":"invalid_credentials"}
为了可以正确经过,咱们能够在body
部分给出用户邮箱和密码(用户可用thinker
建立一个) 这个时候就会正确返回一个token
固然为了测试的话能够直接去tinker
新建一个用户便可 这里我已经新建了一个用户邮箱是jellybean@163.com
的用户
这样咱们就能够拿到用户的token
咱们在集成jwt认证
时 添加了jwt:auth
的中间件
这个其实很好理解 就好比一篇文章 若是用户没有登陆进来你是不能对文章进行收藏 评论等操做的
那么这个token
咱们是在用户登陆以后或者用户注册以后会返回给客户段一个token
值 这样用户凭借这个token
也就是一个证实你是已经登陆的用户 你是咱们这个网站的用户 这样你就能够发表文章等操做
这样的话咱们能够为一些路由添加一些中间件
$api->group(['namespace' => 'App\Api\Controllers','middleware'=>'jwt.auth'],function ($api){ $api->get('lessons','LessonsController@index'); $api->get('lessons/{id}','LessonsController@show'); });
这样的话咱们再去访问某一个lesson
的话
提示咱们缺乏token
由于如今至关于一个没有登陆的用户去访问这条路由 那么咱们再去接上咱们的token
的话
这下咱们就能够访问到对应的数据了 这个token
也是会有过时时间的 若是过时了 咱们须要去更新这个token
在应用中咱们能够加一个api_token
字段来存储这个字段 这样每次用户去访问相应的路由的话咱们会去验证这个token
固然 jwt
安装成功后 ,默认使用的 auth
进行 认证 ,读取的也是users
表的数据
若是是须要验证其余的数据表的话能够参照这篇文章 jwt 自定义 数据表 区别于 auth 中的 users 表
这样基本就实现了一个基本的后台接口的请求过程