《PHP实战:基于laravel制作APP接口(API)》要点:
本文介绍了PHP实战:基于laravel制作APP接口(API),希望对您有用。如果有疑问,可以联系我们。
PHP实战前期准备
PHP实战前言,为什么做以及要做个啥
本人姓小名白,不折不扣编程届小白一名,但是自从大一那会儿接触到编程这件奇妙的事情,就完完全全的陷入的程序的世界.
PHP实战这不,最近又开始折腾APP了,话说现在开发一款APP真是容易,只用JavaScript和一点点HTML+css技术就可以完成.但是做APP的后台就不一样了.开发了APP,想让读点数据进去,那我们就要去开发个后台了.
PHP实战laravel框架,是我最喜欢的PHP框架了,没有之一.去年就曾经用laravel写了我的个人网站但粗糙程度让我十分脸红,好了不扯了,让我们直接进入主题――先安装laravel吧!
PHP实战基础环境配置
PHP实战具体的步骤直接看文档吧laravel5.2安装
PHP实战我自己的环境是win10上面安装了wampsrver2.5,但是这里值得好好注意一下,用wampsrver2.5了话,这几个地方要改动一下.关于这个请看我的笔记点击预览
工具:sublime
浏览器:chrome(要用到的插件postman)
PHP实战关于API
PHP实战API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是提供应用程序与开发人员基于某软件或硬件得以访问一组例程的能力,而又无需访问源码,或理解内部工作机制的细节.
需要注意的是:API有它的具体用途,我们应该清楚它是干啥的.访问API的时候应该输入什么.访问过API过后应该得到什么.
PHP实战在开始设计API时,我们应该注意这8点
这里的内容摘抄自大神的博客
后续的开发计划就围绕着这个进行了.(真心好棒的总结)
PHP实战1.Restful设计原则
2.API的命名
3.API的安全性
4.API返回数据
5.图片的处理
6.返回的提示信息
7.在线API测试文档
8.在app启动时,调用一个初始化API获取必要的信息
PHP实战用laravel开发API
PHP实战就在我上愁着要不要从零开始学习的时候,找到了这个插件dingo/api那么现在就来安装吧!
首先一定是下载的没错
在新安装好的laravel的composer.json加入如下内容
PHP实战然后打开cmd执行
PHP实战
composer update
PHP实战在config/app.php中的providers里添加
PHP实战
App\Providers\OAuthServiceProvider::class,
Dingo\Api\Provider\LaravelServiceProvider::class,
LucaDegasperi\OAuth2Server\Storage\FluentStorageServiceProvider::class,
LucaDegasperi\OAuth2Server\OAuth2ServerServiceProvider::class,
PHP实战在aliases里添加
PHP实战
'Authorizer' => LucaDegasperi\OAuth2Server\Facades\Authorizer::class,
PHP实战修改app/Http/Kernel.php文件里的内容
PHP实战
protected $middleware = [\LucaDegasperi\OAuth2Server\Middleware\OAuthExceptionHandlerMiddleware::class,
];
protected $routeMiddleware = [
'oauth' => \LucaDegasperi\OAuth2Server\Middleware\OAuthMiddleware::class,
'oauth-user' => \LucaDegasperi\OAuth2Server\Middleware\OAuthUserOwnerMiddleware::class,
'oauth-client' => \LucaDegasperi\OAuth2Server\Middleware\OAuthClientOwnerMiddleware::class,
'check-authorization-params' => \LucaDegasperi\OAuth2Server\Middleware\CheckAuthCodeRequestMiddleware::class,
'csrf' => \App\Http\Middleware\VerifyCsrfToken::class,
];
PHP实战然后执行
PHP实战
php artisan vendor:publish
php artisan migrate
PHP实战在.env文件里添加这些配置
PHP实战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
PHP实战修改app\config\oauth2.php文件
PHP实战
'grant_types' => [
'password' => [
'class' => 'League\OAuth2\Server\Grant\PasswordGrant',
'access_token_ttl' => 604800,
'callback' => '\App\Http\Controllers\Auth\PasswordGrantVerifier@verify',
],
],
PHP实战新建一个服务提供者,在app/Providers下新建OAuthServiceProvider.php文件内容如下
PHP实战
namespace App\Providers;
use Dingo\Api\Auth\Auth;
use Dingo\Api\Auth\Provider\OAuth2;
use Illuminate\Support\ServiceProvider;
class OAuthServiceProvider extends ServiceProvider
{
public function boot()
{
$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()
{
//
}
}
PHP实战然后打开routes.php添加相关路由
PHP实战
//Get access_token
Route::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 App\User();
$user->name="tester";
$user->email="test@test.com";
$user->password = \Illuminate\Support\Facades\Hash::make("password");
$user->save();
});
$api = app('Dingo\Api\Routing\Router');
//Show user info via restful service.
$api->version('v1', ['namespace' => 'App\Http\Controllers'], 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())];
});
});
PHP实战分别创建BaseController.php和UsersController.php内容如下
PHP实战
//BaseController
namespace App\Http\Controllers;
use Dingo\Api\Routing\Helpers;
use Illuminate\Routing\Controller;
class BaseController extends Controller
{
use Helpers;
}
//UsersController
namespace App\Http\Controllers;
use App\User;
use App\Http\Controllers\Controller;
class UsersController extends BaseController
{
public function index()
{
return User::all();
}
public function show($id)
{
$user = User::findOrFail($id);
// 数组形式
return $this->response->array($user->toArray());
}
}
PHP实战随后在app/Http/Controllers/Auth/下创建PasswordGrantVerifier.php内容如下
PHP实战
namespace App\Http\Controllers\Auth;
use Illuminate\Support\Facades\Auth;
class PasswordGrantVerifier
{
public function verify($username, $password)
{
$credentials = [
'email' => $username,
'password' => $password,
];
if (Auth::once($credentials)) {
return Auth::user()->id;
}
return false;
}
}
PHP实战打开数据库的oauth_client表新增一条client数据
PHP实战
INSERT INTO 'oauth_clients' ('id', 'secret', 'name', 'created_at', 'updated_at') VALUES ('1', '2', 'Main website', '2016C03C13 23:00:00', '0000C00C00 00:00:00');
PHP实战随后的就是去愉快的测试了,这里要测试的API有
PHP实战新增一个用户
PHP实战http://localhost/register
PHP实战读取所有用户信息
PHP实战http://localhost/api/users
PHP实战只返回用户id为4的信息
PHP实战http://localhost/api/users/4
PHP实战获取access_token
PHP实战http://localhost/oauth/access_token
PHP实战利用token值获得时间,token值正确才能返回正确值
PHP实战http://localhost/api/time
PHP实战打开PostMan
PHP实战
PHP实战
PHP实战
PHP实战
维易PHP培训学院每天发布《PHP实战:基于laravel制作APP接口(API)》等实战技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培养人才。
转载请注明本页网址:
http://www.vephp.com/jiaocheng/7359.html