【问题标题】:The Rest API not working in postman when requested by url当 url 请求时,Rest API 在邮递员中不起作用
【发布时间】:2017-11-24 05:58:21
【问题描述】:

我在控制台中通过这个 cmd 启动了服务器,它工作正常

php -S localhost:8000 -t public

当我像这样在邮递员中为流明调用 API 时:

localhost:8000/GitHub/twinfield/public/index.php/user

API 可用于插入,但当我这样调用时:

localhost:8000/GitHub/twinfield/user

错误如下:

(1/1) RoutesRequests.php(第 226 行)中的 NotFoundHttpException RoutesRequests.php 中的 Application->handleDispatcherResponse(array(0)) (第 164 行)在 Application->Laravel\Lumen\Concerns{closure}() 中 RoutesRequests.php(第 413 行)位于 Application->sendThroughPipeline(array(), object(Closure)) in RoutesRequests.php(第 166 行)在 Application->dispatch(null) 中 RoutesRequests.php(第 107 行)位于 Application->run() in index.php(行 28)

我在 routes/web.php 中的路由文件:

$router->get('/', function () use ($router) {
    //print_r('1234');die;
    return $router->app->version();
});

$router->get('user','userController@index');
$router->get('user/{id}','userController@getuser');      
$router->post('user','userController@createuser');
$router->post('user/{id}','userController@updateuser');    
$router->delete('user/{id}','userController@deleteuser');

我已经尝试在 public/index.php 中这样解决但没有成功。

$request = Illuminate\Http\Request::capture();
$app->run($request);

我正在使用 Xampp 在 localhost 中工作。数据库是MySQL,PHP版本是7.1。

【问题讨论】:

    标签: php laravel .htaccess postman lumen-5.4


    【解决方案1】:

    先生,您忘记了网址链接,或者可能会错过。它总是在我们启动服务器时(在 laravel 中,命令是 php artisan serve),它总是属于调用它的路径。

    就像您的情况一样,您的路径是流明所在的 xampp/htdocs/GItHub/twinfield(我想)。

    localhost:8000/GitHub/twinfield/public/index.php/user
    

    所以如果你这样打电话:

    localhost:8000/GitHub/twinfield/user
    

    因为您定义了路线,所以它永远不会起作用:

    $router->get('user','userController@index'); 
    $router->post('user','userController@createuser');
    

    这就是为什么您必须只使用路线名称和类型。打开 postman 并使用 type->get:

    添加这一行
    localhost:8000/user
    

    您将获得数据库中的所有用户。如果你用 type->post 添加这一行。

    localhost:8000/user
    

    您应该将正文与要保存的表格的字段一起发布。

    【讨论】:

      【解决方案2】:

      在尝试了许多解决方案后,我觉得我犯了一个愚蠢的错误。那是网址:

      localhost:8000/GitHub/twinfield/user
      

      因为新服务器是由命令启动的,所以它属于那个 url。所以我在邮递员中使用了这个 url,而不是 post。

      localhost:8000/user
      

      你知道它就像一个魅力。在这个解决方案中不需要任何其他东西。

      【讨论】:

        猜你喜欢
        • 2019-03-03
        • 2019-09-16
        • 2020-05-22
        • 2021-03-08
        • 2018-07-26
        • 2019-08-25
        • 2019-05-27
        • 2016-07-27
        • 2021-12-04
        相关资源
        最近更新 更多