简体   繁体   中英

Laravel api 404 when try to call method from controller

I have an application in Laravel deployed on the server. When I try to call POST in Postman :

http://api.mydomain.com/api/auth/signin

I get the error 404 not found .

In file RouteServiceProvider I commented section for web:

public function boot()
    {
        $this->configureRateLimiting();

        $this->routes(function () {
            Route::prefix('api')
                ->middleware('api')
                ->namespace($this->namespace)
                ->group(base_path('routes/api.php'));
            /*
            Route::middleware('web')
                ->namespace($this->namespace)
                ->group(base_path('routes/web.php'));
            */
        });
    }

When I had uncommented section for web and send GET from Postman but without any suffix, only domain: api.mydomain.com/ it worked. Now I commented web and have only API section and any request to API method doesn't work. My controller:

class JwtAuthController extends Controller
{
    //
    public function __construct() {
        $this->middleware('auth:api', ['except' => ['login', 'register']]);
    }

    /**
     * Get a JWT via given credentials.
    */
    public function login(Request $request){
...
}

In api.php file:

Route::group([
    'middleware' => 'api',
    'prefix' => 'auth'
], function ($router) {
    Route::post('/signin', [JwtAuthController::class, 'login']);
}

Result for php artisan route:list :

| App\Http\Middleware\Authenticate:api |
|        
| POST     
| api/auth/signin                                                             
| generated::1YOyMAVvTTnBZtym 
| App\Http\Controllers\JwtAuthController@login                        
| api  

                            

On my local machine, it works, but when I deploy it on the server it won't work.

Not found because you did not add the subdomain api , To fix it

You can just remove api. from url, like that http://exmaple.com/api/auth/signin

OR

you can add the subdomain to RouteServiceProvider

like that

 Route::middleware('api')
    ->prefix('api')
    ->domain('api.exmaple.com') // <-
    ->namespace($this->namespace)
    ->group(base_path('routes/api.php'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM