douci7521 2016-09-17 15:19
浏览 101

设置Slim参数以便进行调试

I enjoy using Slim, but am frustrated on how to debug it. Lets say I have the following routing. I can do a cURL request, and see the output, but instead I wish to go through the script line by line with my IDE debugger which happens to be NuShere's phpED. While I haven't quick figured it out, I am pretty sure I can make phpED do a POST request, but definitely not a PUT or DELETE request, so there is no point.

Is there any way to do so? I can force $_SERVER['REQUEST_URI'] and $_SERVER['REQUEST_METHOD'] to any value easy enough, and can also change the $_GET superglobal, but body data (i.e. $_POST) isn't so simple.

<?php
$app = new \Slim\Slim();
$app->get('/books/:id', function ($id) {
    //Show book identified by $id
});
$app->post('/books', function () {
    //Create book
});
$app->put('/books/:id', function ($id) {
    //Update book identified by $id
});
$app->delete('/books/:id', function ($id) {
    //Delete book identified by $id
});
$app->get('/toys/:id', function ($id) {
    //Show toy identified by $id
});
$app->post('/toys', function () {
    //Create toy 
});
$app->put('/toys/:id', function ($id) {
    //Update toy identified by $id
});
$app->delete('/toys/:id', function ($id) {
    //Delete toy identified by $id
});
  • 写回答

3条回答 默认 最新

  • doujie9252 2016-09-17 16:19
    关注

    Based on Blake's comments, I created the following method, and call it in a constructor. After each request, a new test file is created which can be debug.

        private function createTest($params,$method)
        {
            $inputs=print_r($params,1);
            $method=strtolower($method);
            $method2=($method=='put' || $method=='delete')?'post':$method;
            $html=$this->createTestInputs($params,null,null); //array, previous inputs, name prefix
            $html=<<<eod
    <p>URL: $_SERVER[REQUEST_URI]</p>
    <p>METHOD: $method</p>
    <p>INPUTS:<pre>$inputs</pre></p>
    <form action="$_SERVER[REQUEST_URI]" method="$method2">
        $html
        <input type="hidden" name="_METHOD" value="$method"/>
        <input type="submit" value="Test"/>
    </form>
    eod;
            file_put_contents($_SERVER['DOCUMENT_ROOT'].'/test.php', $html);
        }    
    
        private function createTestInputs($params,$html,$prefix)
        {
            foreach($params as $name=>$value) {
                if(is_array($value)) {
                    $html=$this->createTestInputs($value,$html,$name);
                }
                else {
                    $html.="<input name='".($prefix?$prefix.'['.$name.']':$name)."' type='hidden' value='$value' />
    ";
                }
            }
            return $html;
        }    
    
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)