Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel api route not found but exists in route list

Tags:

php

laravel

I'm trying to add a patch route to routes/api.php but I get "route not found" even after trying route:cache. it's registered in route:list and other routes in that scope are working.

this is my code:

<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Route::group([
    'prefix' => 'v1',
    'namespace' => 'App\Http\Controllers\Api\V1',
], function() {

    Route::group([
        'prefix' => '/users',
    ], function() {
        Route::get('/{user}', 'UsersController@show');
        Route::patch('/{user}/updateStatus', 'UsersController@updateStatus');
    });

});

and this is my code in controller action:


<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;

class UserController extends Controller {
    public function updateStatus(Request $request, User $user) {
         # my logic
    }
}

the route is registered as /api/v1/users/{user}/updateStatus but I get 404.

by the way, I'm using laravel 8 and php 7.3

like image 265
Sina Avatar asked May 25 '21 05:05

Sina


People also ask

How do I list all routes in Laravel artisan?

Laravel Artisan Route Command: The route:list Command The route:list command can be used to show a list of all the registered routes for the application. This command will display the domain, method, URI, name, action and middleware for the routes it includes in the generated table.

How do I use the route cache in Laravel?

When deploying your application to production, you should take advantage of Laravel's route cache. Using the route cache will drastically decrease the amount of time it takes to register all of your application's routes. To generate a route cache, execute the route:cache Artisan command:

What is route model binding in Laravel?

Laravel route model binding provides a convenient way to automatically inject the model instances directly into your routes. For example, instead of injecting a user's ID, you can inject the entire User model instance that matches the given ID.

How do I inject a Laravel model into a route?

When injecting a model ID to a route or controller action, you will often query the database to retrieve the model that corresponds to that ID. Laravel route model binding provides a convenient way to automatically inject the model instances directly into your routes.


1 Answers

I think web server will automatically change url to lowercase so you must use lowercase in laravel route,so you must change "updateStatus" to "updatestatus" or "update-status"

like image 145
Ali X Avatar answered Nov 09 '22 02:11

Ali X