【问题标题】:Getting 404 page not found error while hit api url from postman从邮递员点击 api url 时出现 404 页面未找到错误
【发布时间】:2019-07-14 01:42:10
【问题描述】:

这是我的控制器

  public function show($id)
    {
         echo $data=Poll_model::find($id);
        // echo $data=DB::select('Select * form tbl_poll where id=?',[$id]);
        //echo "string";

    }

我从邮递员那里点击了这个网址 http://localhost/total/public/api/1

显示 404 错误页面未找到

但是

当我点击http://localhost/total/public/api

它 100% 正常工作并显示我的数据。

这是我的路线列表。

【问题讨论】:

  • 请为show功能显示您的路线
  • 现在检查一下。 @AddWebSolutionPvtLtd
  • 您能从路线文件中出示您的路线声明吗?
  • @MihirBhende 我就是这样做的。 Route::resource('/','Poll');

标签: laravel api postman


【解决方案1】:

请为 show 方法更新您的路线并尝试:

Route::get('poll/{id}/show', 'App\Http\Controllers\Poll@show');

然后你的网址就像:

http://localhost/total/public/api/poll/1/show

【讨论】:

    【解决方案2】:

    因为您的路线声明为:

    Route::resource('/','Poll');

    Laravel 将创建主 url 结构为http://localhost/total/public/api/的所有路由

    如果你想拥有一个名为 poll 的模块,那么它就是资源路由:

    `Route::resource('polls','\App\Http\Controllers\Poll');`
    

    这会给你这样的网址:

    GET `http://localhost/total/public/api/polls`
    GET `http://localhost/total/public/api/polls/create`
    POST`http://localhost/total/public/api/polls`
    GET `http://localhost/total/public/api/polls/{poll}`
    GET `http://localhost/total/public/api/polls/{poll}/edit`
    PUT/PATCH `http://localhost/total/public/api/polls/{poll}`
    DELETE`http://localhost/total/public/api/polls/{poll}`
    

    我建议将控制器命名为 PollController 而不仅仅是 Poll,因为很容易知道它是一个控制器类。

    请参阅documentation 以获得更多帮助。

    【讨论】:

      猜你喜欢
      • 2020-01-25
      • 2019-11-20
      • 2019-08-05
      • 2018-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-12
      • 1970-01-01
      相关资源
      最近更新 更多