【问题标题】:Slim framework php GET and POSTSlim 框架 php GET 和 POST
【发布时间】:2013-06-12 20:19:00
【问题描述】:

我试图弄清楚restful apis 在php 中是如何工作的。代码如何获取例如从 javascript 浏览器插件(我正在处理的内容)传递的信息。是通过GET还是POST?有人可以提供一个例子吗?

【问题讨论】:

    标签: php rest post get slim


    【解决方案1】:

    看看 Slims 主页上的例子:

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

    如果你在做 POST:

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

    您未映射到 URL 的任何参数在 $_GET 或 $_POST 中仍然可用(例如 /hello/kitty/?kat=42)

    【讨论】:

    • get 和 post 的安全性如何?
    • 这个问题太模糊了,无法回答。你在发布什么?输入数据去哪了?
    • @user2491588 get 和 post 传递的值来自用户,因此在未检查值并首先进行清理之前,切勿信任它们。
    【解决方案2】:
        <?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.
    
    ?> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-12
      • 2014-08-27
      • 2011-10-12
      相关资源
      最近更新 更多