iT邦幫忙

0

從頭開始用 Laravel 來做CRUD -20- 初始 - Laravel - Passport 安裝

https://ithelp.ithome.com.tw/upload/images/20181105/20112678eCPKQK8fHY.jpg

目前環境

  • Laravel:5.7
  • 資料庫:MariaDB

說明

  • 所謂的CRUD是指 新增(Create)、讀取(Read)、更新(Update)、刪除(Delete) 在程式動作中,基本的四個動作。
  • Laravel 是一個開源的PHP框架,它由Taylor Otwell開發,支持MVC模式。
  • Vue.js是一個用於建立使用者介面的開源JavaScript框架,也是一個建立單頁面應用的Web應用框架。
  • passport 使用 Laravel Passport 來輕易的做到 API 認證,Passport 可以在幾分鐘內為你的 Laravel 應用程式提供一個完整的 OAuth2 伺服器實作。

--- 本文開始 ---

相關連結

https://ithelp.ithome.com.tw/upload/images/20181105/20112678eoZOLNfYGM.png

1、安裝 passport

composer require laravel/passport

https://ithelp.ithome.com.tw/upload/images/20181105/20112678d23ZRWd6on.png
https://ithelp.ithome.com.tw/upload/images/20181105/20112678im8r4nQDh8.png
https://ithelp.ithome.com.tw/upload/images/20181105/201126786xd25WSA8H.png

2、執行 migrate

php artisan migrate

https://ithelp.ithome.com.tw/upload/images/20181105/20112678wOoATAV7rV.png

3、執行 passport:install

php artisan passport:install

執行後,會產生2個id

https://ithelp.ithome.com.tw/upload/images/20181105/20112678b2JoXqgXSi.png

4、修改 HasApiTokens

位置:App/User.php
新增 HasApiTokens 如圖

namespace App;

use Laravel\Passport\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use HasApiTokens, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

https://ithelp.ithome.com.tw/upload/images/20181105/20112678GdmFf1Q4ju.png

5、修改 AuthServiceProvider

位置:App/Providers/AuthServiceProvider.php

namespace App\Providers;

use Laravel\Passport\Passport;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        'App\Model' => 'App\Policies\ModelPolicy',
    ];

    /**
     * Register any authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();

        Passport::routes();
    }
}

https://ithelp.ithome.com.tw/upload/images/20181105/20112678oR5HATWMJM.png

6、修改 auth.php

位置:config/auth.php

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'passport',
            'provider' => 'users',
        ],
    ],

https://ithelp.ithome.com.tw/upload/images/20181105/20112678KRIsXEl3IW.png

7、完工

目前算完工了,剩下就等有要用到時候,我們再來寫吧!

--- 相關連結 ---
上一篇:從頭開始用 Laravel 來做CRUD -19- 初始 - Laravel - Mysql 調整 2
--- 同步更新 ---
[CRUD for Laravel & Vue.js] 文章列表 - 在痞客邦
--- 更多的文章 在痞客邦 ----
痞客邦

感謝

感謝各位的觀看!
友情連結:鑽頭-瑜誠
文中如有技術不良或寫錯的部份,如您願意指出,我們也很樂意接受您的指正,但請不要惡意攻撃,我們只是為了樂趣而寫的文章。


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言