简体   繁体   中英

URL Parameter in GET Route Laravel

I have a route in api.php which look like this

Route::get('auth/logout/{token}', 'UserController@logout');

Then, I used Postman to check this API endpoint like this:

localhost:8000/api/v1/auth/logout?token=$2y$10$InjSk8VExH76wSyA3OE9a.jhR/3GhAkJdBE3EyQ3O.Z0kCe/r7wp y

But I just get a blank response in postman. It should show the message response and delete the data in the database. But it doesn't. Here is my logout() in UserController.php

public function logout($token){
    $current_token = Token::where('token', $token)->first();
    if($current_token){
        if(Token::where('token','=',$current_token)->delete()){
            return response()->json([
                'message' => 'Logout Success'
            ], 201);
        }
    }else{
        return response()->json([
            'message' => 'Unauthorized User'
        ], 401);
    }
}

Anyone can help my problem? I appreciate all of your approaches.

Edit...

When I send the dummy request like this and use dd($token) , Postman gives me "asdfasdf" .

localhost:8000/api/v1/auth/logout/asdfasdf

So, how do I send the token parameter in the URL using "?"

Edit 2...

I generate the token from bcrypted user_id from the database. In my case, I don't use any token generator plugin, or something else.

You have passed token with / in route and that is why you have had returns blank response.

As you said last in your question to pass token like ? . So you need pass token in header . In postman header section you have to pass key and value. Your key is token . Value is your token like 2y$10$InjSk8VExH76wSyA3OE9a.jhR/3GhAkJdBE3EyQ3O.Z0kCe/r7wpy .

Read this doc: https://learning.getpostman.com/docs/postman/sending-api-requests/requests/

For laravel api_ token:

Take a look with laravel official doc: https://laravel.com/docs/5.8/api-authentication

Yes, I finally know the reason why does the response in Postman is blank. UserController.php is a resource controller, but I added a custom login() method inside it. I should put the Route for the custom method above the route for the resource controller. It should look like this:

Route::get('auth/login','UserController@login');

Route::resource('auth', 'UserController');

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