代碼已放在 github 上,歡迎參考和提出 issue:lumen-quick-start github地址php
安裝包: composer install
mysql
複製配置文件, 並修改配置: cp .env.example .env
【數據庫配置, passport配置】laravel
執行數據庫遷移: php artisan migrate
git
安裝passport: php artisan passport:install
github
啓動項目: php -S localhost:8088 -t public/
web
生成一條假用戶數據: php artisan db:seed --class=UsersTableSeeder
sql
安裝lumen安裝器: composer global require "laravel/lumen-installer"
數據庫
先 lumen new user-center
初始化一個項目 或者 composer create-project --prefer-dist laravel/lumen user-center
json
執行 composer install
安裝依賴包,若是是用 lumen new
命令能夠省略這一步。bootstrap
複製配置文件 cp .env.example .env
設置 APP_KEY
等配置信息, 由於 php artisan key:generate
沒用
啓動項目 php -S localhost:8000 -t public
lumen-passport
包 #composer require dusterio/lumen-passport
複製代碼
bootstrap/app.php
文件// 集成passport
//只是取消註釋
// Enable Facades
$app->withFacades();
// Enable Eloquent
$app->withEloquent();
// Enable auth middleware (shipped with Lumen)
$app->routeMiddleware([
'auth' => App\Http\Middleware\Authenticate::class,
]);
//新增
// Finally register two service providers - original one and Lumen adapter
$app->register(Laravel\Passport\PassportServiceProvider::class);
$app->register(Dusterio\LumenPassport\PassportServiceProvider::class);
// 自定義-下面有說到, 能夠以後加
// 配置-新增
$app->configure('auth');
// 開啓AppServiceProvider-取消註釋
$app->register(App\Providers\AppServiceProvider::class);
$app->register(App\Providers\AuthServiceProvider::class);
// $app->alias('cache', 'Illuminate\Cache\CacheManager'); //新增,解決Lumen的Cache問題
複製代碼
migrate
和安裝 passport
# Create new tables for Passport
php artisan migrate
# Install encryption keys and other necessary stuff for Passport
php artisan passport:install
複製代碼
$ php artisan migrate
Migration table created successfully.
Migrating: 2016_06_01_000001_create_oauth_auth_codes_table
Migrated: 2016_06_01_000001_create_oauth_auth_codes_table
Migrating: 2016_06_01_000002_create_oauth_access_tokens_table
Migrated: 2016_06_01_000002_create_oauth_access_tokens_table
Migrating: 2016_06_01_000003_create_oauth_refresh_tokens_table
Migrated: 2016_06_01_000003_create_oauth_refresh_tokens_table
Migrating: 2016_06_01_000004_create_oauth_clients_table
Migrated: 2016_06_01_000004_create_oauth_clients_table
Migrating: 2016_06_01_000005_create_oauth_personal_access_clients_table
Migrated: 2016_06_01_000005_create_oauth_personal_access_clients_table
複製代碼
$ php artisan passport:install
Encryption keys generated successfully.
Personal access client created successfully.
Client ID: 1
Client Secret: oIWaBwhNt2KZD606lb0Il5dZl8D72fhMBUwkPvHW
Password grant client created successfully.
Client ID: 2
Client Secret: gvpDe6KIieDD1dvouk639fsxD6wLiNjbPuabT4wh
複製代碼
config/auth.php
文件,加入如下內容return [
'defaults' => [
'guard' => 'api',
'passwords' => 'users',
],
'guards' => [
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => \App\User::class
]
]
];
複製代碼
bootstrap/app.php
中引入配置文件$app->configure('auth');
複製代碼
Next, you should call the LumenPassport::routes method within the boot method of your application (one of your service providers). This method will register the routes necessary to issue access tokens and revoke access tokens, clients, and personal access tokens:
Dusterio\LumenPassport\LumenPassport::routes($this->app);
複製代碼
You can add that into an existing group, or add use this route registrar independently like so;
Dusterio\LumenPassport\LumenPassport::routes($this->app, ['prefix' => 'v1/oauth']);
複製代碼
.env
文件中加入幾項配置項# 環境
APP_ENV=local
# 調試
APP_DEBUG=true
# 祕鑰
APP_KEY=base64:24uriVnENMM+x8u8ouLsNlE4EohGNY1mxTGWdxmPt2w=
# 時區
APP_TIMEZONE=UTC
# 語言
#APP_LOCALE
# 數據庫配置
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=user_center
DB_USERNAME=root
DB_PASSWORD=
# 緩存
CACHE_DRIVER=file
# 隊列
QUEUE_DRIVER=sync
# passport password grant_type client(passport進行密碼受權的客戶端)
APP_CLIENT_ID=2
APP_CLIENT_SECRET=bohwT7qPxj8ltPUn21nDvmMVg5DYiRgoFCZYvVh7
# passport進行密碼受權時請求路徑,向本身請求
APP_URL=http://www.b.com
複製代碼
app
目錄下添加通用函數文件 helper.php
而且經過文件的形式自動載入, 在 composer.json
裏的 autoload
添加以下代碼:"autoload": {
"psr-4": {
"App\\": "app/"
},
"files": [
"app/helpers.php"
]
},
複製代碼
routes
文件下新建 api
路由文件夾, 並在 bootstrap\app.php
文件將它加載進來。$app->group(['namespace' => 'App\Http\Controllers'], function ($app) {
require __DIR__.'/../routes/web.php';
require __DIR__.'/../routes/api/v1.php';
});
複製代碼
dingo
包 #composer require dingo/api:1.0.x@dev
複製代碼
lumen 5.5
使用下面命令
"require": {
"dingo/api": "2.0.0-alpha1"
}
複製代碼
dingo
引入到 lumen
在 bootstrap/app.php
文件中引入:
$app->register(Dingo\Api\Provider\LumenServiceProvider::class);
複製代碼
在 app
目錄下新增 Models
和 Serializers
和 Transformers
目錄,dingo
返回數據的時候能夠 transform
下
配置自定義配置文件
你還能夠建立自定義的配置文件並使用 $app->configure()
方法來加載它們。例如,若是你的配置文件位於config/options.php
,你能夠像這樣加載它:$app->configure('options');
爲了刷新 token
還能夠引入 Listeners
引入全部的 config
// config
$app->configure('app');
$app->configure('auth');
$app->configure('secrets');
$app->configure('filesystems');
複製代碼
經過 passport
進行鑑權
Unauthorized.
以狀態碼加提示信息的形式返回。在 Exceptions\Handler.php
目錄的 render
函數中加入switch (true) {
case $e instanceof AuthorizationException:
return response('This action is unauthorized.', 403);
case $e instanceof ModelNotFoundException:
return response('The model is not found.', 404);
}
複製代碼
// laravel-cors
composer require barryvdh/laravel-cors
複製代碼
在 bootstrap/app.php
文件中
// laravel-cors
// 註冊 `cors` 的服務提供者
$app->register(Barryvdh\Cors\ServiceProvider::class);
$app->routeMiddleware([
'cors' => \Barryvdh\Cors\HandleCors::class,
]);
複製代碼