Why trailing slash changing the type of request in Lumen?

180 Views Asked by At

I am new to Lumen and I have defined routes like this:

$app->group(['middleware' => 'auth'], function ($app) {

    $app->get('/', function () use ($app) {
        return $app->version();
    });
    
    $app->get('users[/{userId}]', ['uses' => 'UserController@getUsers']);
    
    $app->post('users[/{userId}]', ['uses' => 'UserController@saveUsers']);
    
    $app->delete('users/{userId}[/]', ['uses' => 'UserController@deleteUsers']);
});

And my controller is like:

public function getUsers($userId = '') {

        try {
            return array('status' => 1, 'users' => ($userId != '') ? User::findOrFail($userId) : User::all());
        } catch (\Exception $e) {
            return array('status' => 0, 'message' => 'error occured : ' . $e->getMessage());
        }
    }

    public function saveUsers($userId = '') {
        
        try {
            $user = ($userId != '') ? User::find($userId) : new User;
            $user->save();
            return array('status' => 1, 'message' => 'user saved successfully', 'userId' => $user->userId);
        } catch (\Exception $e) {
            return array('status' => 0, 'message' => 'failed to save user : ' . $e->getMessage());
        }
    }

If am trying to hit the url with trailing slash, it's going to getUsers and if I remove the trailing slash it's going to saveUsers.

Note: I am using Lumen version 5.3.3 and trying POST request with REST Easy extension in Firefox.

0

There are 0 best solutions below