0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Laravelでneo4jを使ってみる④restfull api

Posted at

neo4jの動作確認のためrestfull APIを実装してみる。

コントローラの生成

$ php artisan make:controller RestRaceController --resource
api.php
Route::resource('race','RestRaceController');

とりあえずGETだけ実装

app/Http/Controllers/RestRaceController.php
class RestRaceController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $items = Race::all();
        return $items->toArray();
    }

[サイトURL]/api/raceでデータが取得できます。

api_tokenのフィールドを追加

api/Models/User.php
class User extends NeoEloquent implements
{
    protected $fillable = [
        'name', 'email', 'password','api_token',
    ];

User登録時にAPIトークンを生成

app/Http/Controllers/Auth/RegisterController.php

class RegisterController extends Controller
{

    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
            'api_token' => Str::random(60),
        ]);
    }

認証時にトークン更新、ログアウト時にトークン削除

app/Http/Controllers/Auth/LoginController.php
class LoginController extends Controller
{


    protected function authenticated(Request $request, $user)
    {
        $user->update(['api_token' => Str::random(60)]);
        return redirect()->intended($this->redirectPath());
    }
    
    public function logout(Request $request)
    {
        $user = $request->user();
        $user->update(['api_token' => null]);
     
        $this->guard()->logout();
     
        $request->session()->flush();
        $request->session()->regenerate();
        
        return redirect('/');
        
    }

Laravel API認証
Laravel+Vue.jsでトークン認証してデータを取得する

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?