drodsh7940
2016-12-17 17:01
浏览 118
已采纳

Laravel新API不起作用

I'm trying to learn Laravel (5.3.28) and I'm running into an issue with my very first api route not working. I've followed the Laravel docs and can create a new project and can navigate to the Laravel splash screen indicating it's working. I added the following route to routes/api.php to test if I can consume the end-point, but I get an error:

routes/api.php

Route::get('foo', function () {
    return 'Hello World';
});

error

NotFoundHttpException in RouteCollection.php line 161:

I have a dedicated CentOS box running XAMPP for my web server. the address to hit the end-point is http://10.0.0.200/test/api/public/foo.

I'm read that my .htaccess file should be edited, but the few examples I found match what I already have, so I'm a little lost on what to do.

Here is the output for php artisan route:list:

+--------+----------+----------+------+---------+--------------+
| Domain | Method   | URI      | Name | Action  | Middleware   |
+--------+----------+----------+------+---------+--------------+
|        | GET|HEAD | /        |      | Closure | web          |
|        | GET|HEAD | api/foo  |      | Closure | api          |
|        | GET|HEAD | api/user |      | Closure | api,auth:api |
+--------+----------+----------+------+---------+--------------+

图片转代码服务由CSDN问答提供 功能建议

我正在努力学习Laravel(5.3.28)并且我遇到了第一个问题 api路线不工作。 我已经关注了Laravel文档,可以创建一个新项目,并可以导航到Laravel启动画面,指示它正在工作。 我将以下路由添加到 routes / api.php 以测试我是否可以使用端点,但是我收到错误:

路由 /api.php

nn
Route::get('foo',function(){
返回'Hello World'; 
}); 
    
 
 

错误

  RouteCollection.php第161行中的NotFoundHttpException:
   
 
 

我有一个运行XAMPP的专用CentOS盒用于我的Web服务器。 命中终点的地址是 http://10.0.0.200/test/api/public/foo

我读到了我的< 代码> .htaccess 文件应该被编辑,但是我找到的几个例子与我已经拥有的相符,所以我对于该做什么感到有点迷失。

这里是 php artisan route的输出:list

  + -------- + ----------  + ---------- + ------ + --------- + -------------- + \ N | 域名| 方法|  URI | 名称| 行动| 中间件| 
 + -------- + ---------- + ---------- + ------ + --------  -  + -------------- + \ N |  |  GET | HEAD |  / |  | 关闭| 网络| 
 |  |  GET | HEAD |  api / foo |  | 关闭|  api | 
 |  |  GET | HEAD |  api / user |  | 关闭|  api,auth:api | 
 + -------- + ---------- + ---------- + ------ + ----  ----- + -------------- + 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanpie2414 2016-12-17 17:43
    已采纳

    10.0.0.200/test/api/public is the root URL I assume. If yes, then you need to hit 10.0.0.200/test/api/public/api/foo

    I think that your project lies in the folder test/api, what you need is to reconfigure apache ginx to route all requests going to 10.0.0.200/test/ , to start going to {webser root}/test/api/public . This is done in web server configuration files

    打赏 评论
  • douyou7102 2016-12-17 21:15

    Laravel uses the artisan commands to do several development tasks, such as running a built-in web server. You can do this by opening a new terminal inside the root directory of your project (where the artisan script is located) and run the following command :

    php artisan serve
    

    This should notice you with the URL of the built-in web server. You can then access it through your web browser. Generally, http://localhost:8000

    This way, you'll be able to access your route which heads to http://localhost:8000/foo and in which you should find the page that says Hello world as your return in your closure.

    打赏 评论

相关推荐 更多相似问题