現在ionicとLaravelを使用してHTTP通信を使おうとしているのですが、Laravel側のルートで
Laravel
1Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException 2The GET method is not supported for this route. Supported methods: POST.
というエラーが表示され、送信することが出来ません。
/routes/api.php の内容を修正するらしいことは分かったのですが、どのように修正すればいいのでしょうか。
api.php
php
1<?php 2 3use Illuminate\Http\Request; 4 5Route::group([ 6 'prefix' => 'auth' 7], function () { 8 Route::post('login', 'Auth\AuthController@login')->name('login'); 9 Route::post('register', 'Auth\AuthController@register'); 10 Route::group([ 11 'middleware' => 'auth:api' 12 ], function() { 13 Route::get('logout', 'Auth\AuthController@logout'); 14 Route::get('user', 'Auth\AuthController@user'); 15 }); 16});
追記
ionicのリクエストを送っているコードです。
(現在ioonicではこちらの質問にあるように422エラーが出ています。)
コードはこちらの記事とこちらの記事を参考に作成しました。
TypeScript
1import { HttpClient, HttpHeaders } from '@angular/common/http'; 2import { Injectable } from '@angular/core'; 3import { tap } from 'rxjs/operators'; 4import { NativeStorage } from '@ionic-native/native-storage/ngx'; 5import { EnvService } from './env.service'; 6import { User } from '../models/user'; 7@Injectable({ 8 providedIn: 'root' 9}) 10export class AuthService { 11 isLoggedIn = false; 12 token:any; 13 constructor( 14 private http: HttpClient, 15 private storage: NativeStorage, 16 private env: EnvService, 17 ) { } 18 login(email: String, password: String) { 19 return this.http.post(this.env.API_URL + 'auth/login', 20 {email: email, password: password} 21 ).pipe( 22 tap(token => { 23 this.storage.setItem('token', token) 24 .then( 25 () => { 26 console.log('Token Stored'); 27 }, 28 error => console.error('Error storing item', error) 29 ); 30 this.token = token; 31 this.isLoggedIn = true; 32 return token; 33 }), 34 ); 35 } 36 register(fName: String, lName: String, email: String, password: String) { 37 return this.http.post(this.env.API_URL + 'auth/register', 38 {fName: fName, lName: lName, email: email, password: password} 39 ) 40 } 41 logout() { 42 const headers = new HttpHeaders({ 43 'Authorization': this.token["token_type"]+" "+this.token["access_token"] 44 }); 45 return this.http.get(this.env.API_URL + 'auth/logout', { headers: headers }) 46 .pipe( 47 tap(data => { 48 this.storage.remove("token"); 49 this.isLoggedIn = false; 50 delete this.token; 51 return data; 52 }) 53 ) 54 } 55 user() { 56 const headers = new HttpHeaders({ 57 'Authorization': this.token["token_type"]+" "+this.token["access_token"] 58 }); 59 return this.http.get<User>(this.env.API_URL + 'auth/user', { headers: headers }) 60 .pipe( 61 tap(user => { 62 return user; 63 }) 64 ) 65 } 66 getToken() { 67 return this.storage.getItem('token').then( 68 data => { 69 this.token = data; 70 if(this.token != null) { 71 this.isLoggedIn=true; 72 } else { 73 this.isLoggedIn=false; 74 } 75 }, 76 error => { 77 this.token = null; 78 this.isLoggedIn=false; 79 } 80 ); 81 } 82}

回答2件
あなたの回答
tips
プレビュー