Русское сообщество разработки на PHP-фреймворке Laravel.
Ты не вошёл. Вход тут.
Всем привет!
Laravel 8 с установленным Laravel Sanctum, PHP 7.3
У меня в проекте выходит
Ошибка "The GET method is not supported for this route" при использовании Laravel Sanctum.
Api.php:
Route::middleware('auth:sanctum')->get('/v1/cabinet/user', [CabinetController::class, 'userdata']);
CabinetController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class CabinetController extends Controller
{
public function userdata(Request $request)
{
return $request->user();
}
public function logout()
{
if (Auth()->check()) {
Auth::user()->token()->delete();
return response([
'message' => 'Пользователь разлогинен',
], 200);
}
}
}
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException: The GET method is not supported for this route. Supported methods: POST. in file C:\Users\HP\Desktop\RS\OSPanel\domains\rick_and_morty_backend\vendor\laravel\framework\src\Illuminate\Routing\AbstractRouteCollection.php on line 117
#0 C:\Users\HP\Desktop\RS\OSPanel\domains\rick_and_morty_backend\vendor\laravel\framework\src\Illuminate\Routing\AbstractRouteCollection.php(103): Illuminate\Routing\AbstractRouteCollection->methodNotAllowed(Array, 'GET')
#1 C:\Users\HP\Desktop\RS\OSPanel\domains\rick_and_morty_backend\vendor\laravel\framework\src\Illuminate\Routing\AbstractRouteCollection.php(40): Illuminate\Routing\AbstractRouteCollection->getRouteForMethods(Object(Illuminate\Http\Request), Array)
#2 C:\Users\HP\Desktop\RS\OSPanel\domains\rick_and_morty_backend\vendor\laravel\framework\src\Illuminate\Routing\CompiledRouteCollection.php(144): Illuminate\Routing\AbstractRouteCollection->handleMatchedRoute(Object(Illuminate\Http\Request), NULL)
#3 C:\Users\HP\Desktop\RS\OSPanel\domains\rick_and_morty_backend\vendor\laravel\framework\src\Illuminate\Routing\Router.php(647): Illuminate\Routing\CompiledRouteCollection->match(Object(Illuminate\Http\Request))
#4 C:\Users\HP\Desktop\RS\OSPanel\domains\rick_and_morty_backend\vendor\laravel\framework\src\Illuminate\Routing\Router.php(636): Illuminate\Routing\Router->findRoute(Object(Illuminate\Http\Request))
#5 C:\Users\HP\Desktop\RS\OSPanel\domains\rick_and_morty_backend\vendor\laravel\framework\src\Illuminate\Routing\Router.php(625): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#6 C:\Users\HP\Desktop\RS\OSPanel\domains\rick_and_morty_backend\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(166): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#7 C:\Users\HP\Desktop\RS\OSPanel\domains\rick_and_morty_backend\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(128): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#8 C:\Users\HP\Desktop\RS\OSPanel\domains\rick_and_morty_backend\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php(21): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#9 C:\Users\HP\Desktop\RS\OSPanel\domains\rick_and_morty_backend\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull.php(31): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#10 C:\Users\HP\Desktop\RS\OSPanel\domains\rick_and_morty_backend\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull->handle(Object(Illuminate\Http\Request), Object(Closure))
#11 C:\Users\HP\Desktop\RS\OSPanel\domains\rick_and_morty_backend\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php(21): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#12 C:\Users\HP\Desktop\RS\OSPanel\domains\rick_and_morty_backend\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TrimStrings.php(40): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#13 C:\Users\HP\Desktop\RS\OSPanel\domains\rick_and_morty_backend\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Foundation\Http\Middleware\TrimStrings->handle(Object(Illuminate\Http\Request), Object(Closure))
#14 C:\Users\HP\Desktop\RS\OSPanel\domains\rick_and_morty_backend\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\ValidatePostSize.php(27): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#15 C:\Users\HP\Desktop\RS\OSPanel\domains\rick_and_morty_backend\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle(Object(Illuminate\Http\Request), Object(Closure))
#16 C:\Users\HP\Desktop\RS\OSPanel\domains\rick_and_morty_backend\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance.php(86): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#17 C:\Users\HP\Desktop\RS\OSPanel\domains\rick_and_morty_backend\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance->handle(Object(Illuminate\Http\Request), Object(Closure))
#18 C:\Users\HP\Desktop\RS\OSPanel\domains\rick_and_morty_backend\vendor\fruitcake\laravel-cors\src\HandleCors.php(52): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#19 C:\Users\HP\Desktop\RS\OSPanel\domains\rick_and_morty_backend\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Fruitcake\Cors\HandleCors->handle(Object(Illuminate\Http\Request), Object(Closure))
#20 C:\Users\HP\Desktop\RS\OSPanel\domains\rick_and_morty_backend\vendor\laravel\framework\src\Illuminate\Http\Middleware\TrustProxies.php(39): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#21 C:\Users\HP\Desktop\RS\OSPanel\domains\rick_and_morty_backend\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Http\Middleware\TrustProxies->handle(Object(Illuminate\Http\Request), Object(Closure))
#22 C:\Users\HP\Desktop\RS\OSPanel\domains\rick_and_morty_backend\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(103): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#23 C:\Users\HP\Desktop\RS\OSPanel\domains\rick_and_morty_backend\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(141): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#24 C:\Users\HP\Desktop\RS\OSPanel\domains\rick_and_morty_backend\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(110): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#25 C:\Users\HP\Desktop\RS\OSPanel\domains\rick_and_morty_backend\public\index.php(52): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#26 {main}
Как исправить?
Не в сети
Наверняка ты смотришь не на тот маршрут, про который сообщение об ошибке.
На всякий случай, попробуй команды php artisan route:list и php artisan route:clear чтобы исключить вариант что ты изменил маршруты, а они по прежнему берутся из старого кеша.
There are two hard things in computer science: cache invalidation, naming things, and off-by-one errors.
Не в сети
Пробовал php artisan route:list и php artisan route:clear разные варианты - рез-т тот же.
Не в сети
Это нормально два Middleware: api и App\Http\Middleware\Authenticate:sanctum?
+--------+-----------+--------------------------------------------------------+--------------------+------------------------------------------------------------+------------------------------------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+--------------------------------------------------------+--------------------+------------------------------------------------------------+------------------------------------------+
| | GET|HEAD | / | | Closure | web |
| | GET|HEAD | api/test | | App\Http\Controllers\IndexController@test | api |
| | POST | api/v1/auth/login | login | App\Http\Controllers\Auth\LoginController@login | api |
| | POST | api/v1/auth/register | | App\Http\Controllers\Auth\RegisterController@register | api |
| | POST | api/v1/cabinet/logout | logout.api | App\Http\Controllers\CabinetController@logout | api |
| | | | | | App\Http\Middleware\Authenticate:sanctum |
| | GET|HEAD | api/v1/cabinet/user | | App\Http\Controllers\CabinetController@userdata | api |
| | | | | | App\Http\Middleware\Authenticate:sanctum |
Не в сети
Это нормально иметь два и более middleware.
Я думаю что ошибка "The GET method is not supported for this route. Supported methods: POST" происходит при обращении к api/v1/auth/login . Вероятно это результат редиректа при попытке обращения к кабинету от лица неаутентифицированного пользователя.
Я не пробовал L8 и Sanctum, поэтому это всё мои домыслы.
Изменено artoodetoo (30.08.2021 14:33:28)
There are two hard things in computer science: cache invalidation, naming things, and off-by-one errors.
Не в сети
Нашел решение:
Я добавлял токен, только брал его из таблицы personal_access_tokens,
а надо было тот который возвращает роут /login.
На видео в конце показано как добавлять токен при роуте:
https://www.youtube.com/watch?v=PcZmv1AOfFo
Изменено RsbAstana (30.08.2021 16:41:20)
Не в сети
Круто. Но каким образом это связано с "GET method is not supported" ты разобрался?
There are two hard things in computer science: cache invalidation, naming things, and off-by-one errors.
Не в сети