简体   繁体   中英

Slim framework php GET and POST

I trying to figure how restful apis work in php. How does the code get the information passed for example from a javascript browser plugin (what I am working on). Is it through GET or POST? Can someone provide an example?

Just look at the example from Slims homepage:

$app->get('/hello/:name', function ($name) {
    echo "Hello, $name";
});

If you're doing POST:

$app->post('/hello/:name', function ($name) {
    echo "Hello, $name";
});

Any arguments that you didn't map to the URL are still available in $_GET or $_POST (eg. /hello/kitty/?kat=42 )

    <?php
    require 'vendor/autoload.php';//attaching the file.
    $app = new\Slim\Slim();//creating a instance of slim.

    /*get routes without parameter*/
    $app->get('/', function () {  
        echo "<center><b>Welcome To SLIM FrameWork-2</b></center><br>";
    });

    /*get routes with parameter*/
    $app->get('/first/:id', function ($id) {  
        echo "hello get route this is my $id program in slim";
    });

    /*post routes*/
    $app->post('/first', function () {  
        echo "hello post route";
    });

    //look in post we can't pass parameter through the URL bcz post can't take value from URL for give parameter use rest client app in chrome but this the resourse identifier will be same as above "/first" don't use "first/:id" bcz this is wrong way to pass parameter.
    $app->run(); // run

//now give the url like http://localhost/slim/index.php/ <-this is for get without parameter.
//http://localhost/slim/index.php/first/3 <-this is for get with parameter.
//http://localhost/slim/index.php/first  <-this is for post routes.

?> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM