user1032531
user1032531

Reputation: 26281

Setting Slim parameters in order to debug

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
});

Upvotes: 0

Views: 2770

Answers (3)

1f7
1f7

Reputation: 575

Slim3 Tracy gist increased to Slim3 Debug Bar package.

Slim3 Debug Bar More screenshots here.

And i agree - PHPStorm is better for debug, but quick discover vars easier in Tracy.

In the near future plan add Echo Console (JQuery Terminal) Slim3 Debug Bar Terminal example

Upvotes: 0

num8er
num8er

Reputation: 19372

If You need debug info in case of exception:

$app->config('debug', true);

also You can use Firebug + FirePHP as explained here: https://www.sitepoint.com/debug-php-firebug-firephp/

Tracy
but I like the interface of debugging so I found something like this: https://github.com/nette/tracy

checkout this gist: https://gist.github.com/1f7/b2d2846777e71f48e43a2ef7acc0a655 (it has little bugs in example, but You can handle it)

demo is here: https://nette.github.io/tracy/tracy-debug-bar.html

screenshot of my test implementation:

screenshot



but I really recommend You to use normal IDE like PHPStorm and debug Your app like a boss (:

watch: https://www.youtube.com/watch?v=rqDDJfG6ip4 or this: https://www.youtube.com/watch?v=_U8xrj0-PXU

p.s. for question of properly configuring Tracy, write in comment (;

Upvotes: 0

user1032531
user1032531

Reputation: 26281

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' />\n";
            }
        }
        return $html;
    }    

Upvotes: 0

Related Questions