dpwdldgn43486 2015-06-24 22:54
浏览 94

404在POST上但在GET Slim应用程序上没有

    <?php
    /**
     * Step 1: Require the Slim Framework
     *
     * If you are not using Composer, you need to require the
     * Slim Framework and register its PSR-0 autoloader.
     *
     * If you are using Composer, you can skip this step.
     */
    require 'Slim/Slim.php';
    require 'routes/db.php';
    require 'routes/getmovies.php';
    require 'routes/getmovieaboverating.php';
    require 'routes/getid.php';


    \Slim\Slim::registerAutoloader();

    /**
     * Step 2: Instantiate a Slim application
     *
     * This example instantiates a Slim application using
     * its default settings. However, you will usually configure
     * your Slim application now by passing an associative array
     * of setting names and values into the application constructor.
     */
    $app = new \Slim\Slim();

    /**
     * Step 3: Define the Slim application routes
     *
     * Here we define several Slim application routes that respond
     * to appropriate HTTP request methods. In this example, the second
     * argument for `Slim::get`, `Slim::post`, `Slim::put`, `Slim::patch`, and `Slim::delete`
     * is an anonymous function.
     */

    // GET route
    $app->get(
        '/',
        function () {
          echo "<h1> HomeWork Assignment 2</h1>";
        }
    );
    $app->get(
        '/movies/',
        function () {
          getMovies();
        }
    );
    $app->get(
        '/movies/id/:movieid',
        function ($movieid) {
          getMovieDetail($movieid);
        }
    );
    $app->get(
        '/movies/rating/:rating',
        function ($rating) {
          getMoviesAboveRating($rating);
        }
    );

    // POST route
    $app->post(
        '/post/',function () use ($app){

            echo 'This is a POST route';
            //$json=$app->request->getBody();
            //$data=json_decode($json,true);
            //echo $data['name'];
            //echo $data['description'];
            //echo $data['rating'];
            //echo $data['url'];
        }
    );

    $app->put(
        '/put',
        function () {
            echo 'This is a PUT route';
        }
    );

    // PATCH route
    $app->patch('/patch', function () {
        echo 'This is a PATCH route';
    });

    // DELETE route
    $app->delete(
        '/delete',
        function () {
            echo 'This is a DELETE route';
        }
    );

    /**
     * Step 4: Run the Slim application
     *
     * This method should be called last. This executes the Slim application
     * and returns the HTTP response to the HTTP client.
     */
    $app->run();

When i do www.example.com/movies/ I am geting the result,

But, When i do www.example.com/post/ I am getting 404 Page not Found. Do I have to do any thing else? I am just doing echo. Can any one tell me how do I solve this issue

  • 写回答

1条回答 默认 最新

  • dpgkg42484 2015-06-24 23:07
    关注

    For a POST path in Slim, you have to POST a request to www.example.com/post/. Loading that URL in a browser tab (a GET request) won't invoke the callback function.

    Slim docs say:

    Use the Slim application's post() method to map a callback function to a resource URI that is requested with the HTTP POST method.

    You can test your requests thoroughly with a HTTP requesting service like https://www.hurl.it/

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器