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('/');
}