【问题标题】:Laravel API routes not working with PostmanLaravel API 路由不适用于 Postman
【发布时间】:2019-04-24 18:43:23
【问题描述】:

我的 route/api.php 有这些路由:

Route::post('/signup' , 'UserApiController@signup');
Route::post('/logout' , 'UserApiController@logout');
Route::post('/verify' , 'UserApiController@verify');

但是当我尝试像这样从 Postman 访问时,它显示找不到对象:

localhost/my_webiste/api/signup

这里是userapicontroller注册函数:

public function signup(Request $request)
{
    $this->validate($request, [
            'social_unique_id' => ['required_if:login_by,facebook,google','unique:users'],
            'device_type' => 'required|in:android,ios',
            'device_token' => 'required',
            'device_id' => 'required',
            'login_by' => 'required|in:manual,facebook,google',
            'first_name' => 'required|max:255',
            'last_name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'mobile' => 'required',
            'password' => 'required|min:6',
        ]);

    try{

        $User = $request->all();

        $User['payment_mode'] = 'CASH';
        $User['password'] = bcrypt($request->password);
        $User = User::create($User);

        return $User;
    } catch (Exception $e) {
         return response()->json(['error' => trans('api.something_went_wrong')], 500);
    }
}

这里是localhost/mywebsite/api/signup的post请求的邮递员输出:

    <title>Object not found!</title>
    <link rev="made" href="mailto:postmaster@localhost" />
   <h1>Object not found!</h1>
   The requested URL was not found on this server.
   If you entered the URL manually please check your
spelling and try again.

【问题讨论】:

  • 显示你的邮递员请求
  • 您是使用php artisan 还是在apache 内部运行您的api?通常当您运行php artisan serve 时,它托管在默认端口8000。因此您需要在邮递员中使用端口号。
  • 我直接在邮递员中发帖请求
  • @Nandan 尝试使用协议http://localhost/mywebsite/api/signup的URL

标签: laravel api


【解决方案1】:

确保在您的邮递员中添加标题 accept = application/json。

【讨论】:

    【解决方案2】:

    您的代码是正确的。好像你在 url 中缺少 public

    localhost/my_webiste/public/api/signup
    

    【讨论】:

    • 我也试过这个,但是找不到错误 404,这里是 api.php 的中间件受保护的函数 mapApiRoutes() { Route::group([ 'middleware' => 'api', 'namespace' => $this->namespace, 'prefix' => 'api/user', ], function ($router) { require base_path('routes/api.php'); }); }
    • 您使用 api/signup 而不是 api/user 请求。删除命名空间和前缀,然后尝试您是否可以访问您的控制器方法。我认为通过一一检查来调试您的路由组出了什么问题会更好。
    猜你喜欢
    • 2020-07-04
    • 2019-09-20
    • 1970-01-01
    • 2013-05-13
    • 2015-02-05
    • 2020-05-15
    • 2017-12-12
    • 1970-01-01
    • 2016-09-07
    相关资源
    最近更新 更多