Ok the problem was from the CSRF TOKEN which is being checked on each request.
I added this to Http\Middleware\VerifyCsrfToken is this can help anyone in the future...
protected $except = [
'/logout',
'/api/register'
];
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello,
I am struggling with this one.
I have created an API which uses the Laravel 5.5 passport. When I try to communicate with the url: http://Mywebsite.dev/api/register I get:
405 Method Not Allowed
Any idea why please?
Using the advanced Rest client from chrome I am sending a POST request with all the required variables. There is something wrong somewhere else but cannot work it out :-(
This is my Api\Auth\RegisterController
<?php
namespace App\Http\Controllers\Api\Auth;
use App\User;
use Validator;
use Laravel\Passport\Client;
use Intervention\Image\ImageManagerStatic as Image;
use Illuminate\Validation\Rule;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Storage;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class RegisterController extends Controller
{
private $client;
public function __construct() {
$this->client = Client::find(1);
}
public function register(Request $request)
{
$validator = Validator::make($request->all(), [
'name' => 'required|string|min:2|max:64',
'surname' => 'required|string|min:2|max:64',
'username' => 'required|string|min:2|max:64|unique:users,username',
'email' => 'required|string|email|min:2|max:64|unique:users,email',
'town_id' => 'required|integer',
'role_id' => 'required|integer',
'password' => 'required|string|min:6'
]);
dd($request->all());
}
}
This is the api.php
<?php
use Illuminate\Http\Request;
Route::post('register', 'Api\Auth\RegisterController@register');
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
The user.php model(top of the page) with the HasApiTokens trait.
<?php
namespace App;
//Extra
use App\Town;
use App\Role;
use Laravel\Passport\HasApiTokens;
use Illuminate\Support\Facades\DB;
use Illuminate\Pagination\Paginator;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\CanResetPassword;
//Extra
use Illuminate\Support\Facades\Auth;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use HasApiTokens, Notifiable;
AuthServiceProvider.php
<?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();
}
}
config/auth.php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
],
Thanks in advance!
Ok the problem was from the CSRF TOKEN which is being checked on each request.
I added this to Http\Middleware\VerifyCsrfToken is this can help anyone in the future...
protected $except = [
'/logout',
'/api/register'
];
Please or to participate in this conversation.