快速开始lumen项目,集成passport+dingo,并解决跨域问题,统一返回数据格式,例子包含用户中心。...

代码已放在 github 上,欢迎参考和提出 issue:lumen-quick-start github地址

快速开始

  • 安装包: composer install

  • 复制配置文件, 并修改配置: cp .env.example .env【数据库配置, passport配置】

  • 执行数据库迁移: php artisan migrate

  • 安装passport: php artisan passport:install

  • 启动项目: php -S localhost:8088 -t public/

  • 生成一条假用户数据: php artisan db:seed --class=UsersTableSeeder

从零开始构造介绍

项目初始化

  • 安装lumen安装器: composer global require "laravel/lumen-installer"

  • lumen new user-center 初始化一个项目 或者 composer create-project --prefer-dist laravel/lumen user-center

  • 执行 composer install 安装依赖包,如果是用 lumen new 命令可以省略这一步。

  • 复制配置文件 cp .env.example .env

  • 设置 APP_KEY 等配置信息, 因为 php artisan key:generate 没用

  • 启动项目 php -S localhost:8000 -t public

引入lumen-passport

  • 安装 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 目录下新增 ModelsSerializersTransformers 目录,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);
        }
复制代码

解决跨域问题lumen-cors【现在换成了用laravel-cors#

// 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,
]);
复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值