skip to Main Content

Maybe someone has encountered this.
I only use the API in Laravel, there are no views in it.
I defined "delete" but after calling this routing, I get a 301 redirect to "notifications".

routes.php

Route::controller(NotificationController::class)
    ->middleware(['auth:sanctum'])
    ->group(function () {
        Route::get('/', 'index');
        Route::delete('/{id}', 'destroy');
        Route::delete('/', 'destroyAll');
        Route::put('/{id}', 'markAsRead');
    });

NotificationController.php

class NotificationController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index(Request $request)
    {
        return new NotificationResource($request->user()->notifications->sortBy('read_at'));
    }



    /**
     * Update the specified resource in storage.
     */
    public function markAsRead(Request $request, string $id)
    {
        return $request->user()->notifications()->where('id', $id)->first()->markAsRead();
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Request $request, string $id)
    {
        return $request->user()->notifications()->where('id', $id)->first()->delete();
    }

    /**
     * Remove all notifications from storage.
     */
    public function destroyAll(Request $request)
    {
        return $request->user()->notifications()->delete();
    }
}

When i run DELETE /api/notifications/
it returns 301 to GET /api/notifications/(index in controller).

Anyone knows why?

Interestingly, everything works fine for me locally, it doesn’t work on the server.
Clearing the cache or dump-autoload does not help.

UPDATE: I defined routes in RouteServiceProvider (simplified version):

public function boot() {
    Route::middleware('api')
        ->prefix('api/notifications')
        ->group(base_path('routes/routes.php'));
}

UPDATE: output of php artisan route:list:

GET|HEAD api/notifications ..................NotificationController@index 
DELETE api/notifications/destroyAll .........NotificationController@destroyAll 
DELETE api/notifications/{id}................NotificationController@destroy 
PUT api/notifications/{id}...................NotificationController@markAsRead

2

Answers


  1. Chosen as BEST ANSWER

    Solved!

    The problem was "slash" at the end of url.

    When i request GET api/notifications not GETapi/notifications/, everything works.

    Sorry for bothering you. Cheers


  2. To resolve this issue: Correct the code that you provided as fellow:
    1- Because you have Route / and DELETE Method it will saved this route and all words after /.
    that means you can’t set 2 route for same PATH
    so change the route like this:

    Route::controller(NotificationController::class)
        ->middleware(['auth:sanctum'])
        ->group(function () {
            Route::get('/item', 'index');
            Route::delete('/item/{id}', 'destroy');
            Route::delete('/all', 'destroyAll');
            Route::put('/item/{id}', 'markAsRead');
        });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search