404在POST但不在GET Slim appication上

时间:2015-06-24 22:54:38

标签: php post 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();

当我这样做www.example.com/movies/我正在考虑结果,

但是,当我做www.example.com/post/时,我找不到404页面。我还有别的事吗?我只是回声。任何人都可以告诉我如何解决这个问题

1 个答案:

答案 0 :(得分:0)

对于Slim中的POST路径,您必须向www.example.com/post/发送请求。在浏览器选项卡(GET请求)中加载该URL不会调用回调函数。

Slim docs说:

  

使用Slim应用程序的post()方法将回调函数映射到使用HTTP POST方法请求的资源URI。

您可以使用HTTP请求服务(例如https://www.hurl.it/

)彻底测试您的请求