[英]URL Parameter in GET Route Laravel
I have a route in api.php
which look like this我在
api.php
有一条路线,看起来像这样
Route::get('auth/logout/{token}', 'UserController@logout');
Then, I used Postman to check this API endpoint like this:然后,我使用 Postman 来检查这个 API 端点,如下所示:
localhost:8000/api/v1/auth/logout?token=$2y$10$InjSk8VExH76wSyA3OE9a.jhR/3GhAkJdBE3EyQ3O.Z0kCe/r7wp
y localhost:8000/api/v1/auth/logout?token=$2y$10$InjSk8VExH76wSyA3OE9a.jhR/3GhAkJdBE3EyQ3O.Z0kCe/r7wp
y
But I just get a blank response in postman.但我只是在 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
这是我在
UserController.php
中的logout()
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"
.当我像这样发送虚拟请求并使用
dd($token)
时, Postman 给了我"asdfasdf"
。
localhost:8000/api/v1/auth/logout/asdfasdf
So, how do I send the token
parameter in the URL using "?"
那么,如何使用
"?"
在 URL 中发送token
参数?
Edit 2...编辑 2...
I generate the token from bcrypted user_id
from the database.我从数据库中的 bcrypted
user_id
生成令牌。 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 .
所以你需要在header中传递令牌。 In postman header section you have to pass key and value.
在 postman header 部分中,您必须传递键和值。 Your key is token .
你的钥匙是token 。 Value is your token like
2y$10$InjSk8VExH76wSyA3OE9a.jhR/3GhAkJdBE3EyQ3O.Z0kCe/r7wpy
.价值是您的代币,例如
2y$10$InjSk8VExH76wSyA3OE9a.jhR/3GhAkJdBE3EyQ3O.Z0kCe/r7wpy
。
Read this doc: https://learning.getpostman.com/docs/postman/sending-api-requests/requests/阅读此文档: https://learning.getpostman.com/docs/postman/sending-api-requests/requests/
For laravel api_ token:对于 laravel api_token:
Take a look with laravel official doc: https://laravel.com/docs/5.8/api-authentication看看 laravel 官方文档: https://laravel.com/docs/5.8/api-authentication
Yes, I finally know the reason why does the response in Postman is blank.是的,我终于知道为什么Postman中的响应是空白的原因了。
UserController.php
is a resource controller, but I added a custom login()
method inside it. UserController.php
是资源 controller,但我在其中添加了自定义login()
方法。 I should put the Route
for the custom method above the route
for the resource
controller.我应该将自定义方法的
Route
放在resource
controller 的route
上方。 It should look like this:它应该如下所示:
Route::get('auth/login','UserController@login');
Route::resource('auth', 'UserController');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.