基础环境配置
具体的步骤直接看文档吧laravel5.2安装
我自己的环境是win10上面安装了wampsrver2.5,但是这里值得好好注意一下,用wampsrver2.5了话,这几个地方要改动一下。关于这个请看我的笔记点击预览
工具:sublime
浏览器:chrome(要用到的插件postman)
关于api
api(application programming interface,应用程序编程接口)是一些预先定义的函数,目的是提供应用程序与开发人员基于某软件或硬件得以访问一组例程的能力,而又无需访问源码,或理解内部工作机制的细节。
需要注意的是:api有它的具体用途,我们应该清楚它是干啥的。访问api的时候应该输入什么。访问过api过后应该得到什么。
在开始设计api时,我们应该注意这8点
这里的内容摘抄自大神的博客
后续的开发计划就围绕着这个进行了。(真心好棒的总结)
1.restful设计原则
2.api的命名
3.api的安全性
4.api返回数据
5.图片的处理
6.返回的提示信息
7.在线api测试文档
8.在app启动时,调用一个初始化api获取必要的信息
用laravel开发api
就在我上愁着要不要从零开始学习的时候,找到了这个插件dingo/api那么现在就来安装吧!
首先一定是下载的没错
在新安装好的laravel的composer.json加入如下内容
然后打开cmd执行
|
1
|
composer update |
在config/app.php中的providers里添加
|
1
2
3
4
|
appprovidersoauthserviceprovider::class,dingoapiproviderlaravelserviceprovider::class,lucadegasperioauth2serverstoragefluentstorageserviceprovider::class,lucadegasperioauth2serveroauth2serverserviceprovider::class, |
在aliases里添加
|
1
|
'authorizer' => lucadegasperioauth2serverfacadesauthorizer::class, |
修改app/http/kernel.php文件里的内容
|
1
2
3
4
5
6
7
8
9
|
];protected $routemiddleware = [ 'oauth' => lucadegasperioauth2servermiddlewareoauthmiddleware::class, 'oauth-user' => lucadegasperioauth2servermiddlewareoauthuserownermiddleware::class, 'oauth-client' => lucadegasperioauth2servermiddlewareoauthclientownermiddleware::class, 'check-authorization-params' => lucadegasperioauth2servermiddlewarecheckauthcoderequestmiddleware::class, 'csrf' => apphttpmiddlewareverifycsrftoken::class,]; |
然后执行
|
1
2
|
php artisan vendor:publish php artisan migrate |
在.env文件里添加这些配置
api_standards_tree=x
api_subtype=rest
api_name=rest
api_prefix=api
api_version=v1
api_conditional_request=true
api_strict=false
api_debug=true
api_default_format=json
修改appconfigoauth2.php文件
|
1
2
3
4
5
6
7
|
'grant_types' => [ 'class' => 'leagueoauth2servergrantpasswordgrant', 'access_token_ttl' => 604800, ],], |
新建一个服务提供者,在app/providers下新建oauthserviceprovider.php文件内容如下
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
namespace appproviders;use dingoapiauthauth;use dingoapiauthprovideroauth2;use illuminatesupportserviceprovider;class oauthserviceprovider extends serviceprovider{ { $this->app[auth::class]->extend('oauth', function ($app) { $provider = new oauth2($app['oauth2-server.authorizer']->getchecker()); $provider->setuserresolver(function ($id) { // logic to return a user by their id. }); $provider->setclientresolver(function ($id) { // logic to return a client by their id. }); return $provider; }); } public function register() { // }} |
然后打开routes.php添加相关路由
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
//get access_tokenroute::post('oauth/access_token', function() { return response::json(authorizer::issueaccesstoken());});//create a test user, you don't need this if you already have.route::get('/register',function(){ $user = new appuser(); $user->name="tester"; $user->email="test@test.com"; $user->password = illuminatesupportfacadeshash::make("password"); $user->save();});$api = app('dingoapiroutingrouter');//show user info via restful service.$api->version('v1', ['namespace' => 'apphttpcontrollers'], function ($api) { $api->get('users', 'userscontroller@index'); $api->get('users/{id}', 'userscontroller@show');});//just a test with auth check.$api->version('v1', ['middleware' => 'api.auth'] , function ($api) { $api->get('time', function () { return ['now' => microtime(), 'date' => date('y-m-d',time())]; });}); |
分别创建basecontroller.php和userscontroller.php内容如下
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
//basecontrollernamespace apphttpcontrollers;use dingoapiroutinghelpers;use illuminateroutingcontroller;class basecontroller extends controller{ use helpers;}//userscontrollernamespace apphttpcontrollers;use appuser;use apphttpcontrollerscontroller;class userscontroller extends basecontroller{ public function index() { return user::all(); } public function show($id) { $user = user::findorfail($id); // 数组形式 return $this->response->array($user->toarray()); }} |
随后在app/http/controllers/auth/下创建passwordgrantverifier.php内容如下
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
namespace apphttpcontrollersauth;use illuminatesupportfacadesauth;class passwordgrantverifier{ public function verify($username, $password) { $credentials = [ 'email' => $username, 'password' => $password, ]; if (auth::once($credentials)) { return auth::user()->id; } return false; }} |
打开数据库的oauth_client表新增一条client数据
|
1
|
insert into 'oauth_clients' ('id', 'secret', 'name', 'created_at', 'updated_at') values ('1', '2', 'main website', '2016–03–13 23:00:00', '0000–00–00 00:00:00'); |
随后的就是去愉快的测试了,这里要测试的api有
新增一个用户
http://localhost/register
读取所有用户信息
http://localhost/api/users
只返回用户id为4的信息
http://localhost/api/users/4
获取access_token
http://localhost/oauth/access_token
利用token值获得时间,token值正确才能返回正确值
http://localhost/api/time
联系信息:邮箱aoxolcom@163.com或见网站底部。

















请登录后发表评论
注册
社交帐号登录