duandu5846 2014-03-10 14:38
浏览 40
已采纳

Laravel路线和控制器测试

so I am trying to test some routes and controller functions that I have created. The other routes that return views work fine, and I was told by my professor that I could test the routes by just putting a vardump in the controller function they are pointing to.

Now, this doesn't really make sense to me, because the functions never return any view or ui data, they just return JSON responses.

Am I correct that there is no way to test routes like that using vardumps? Just confused here.

  • 写回答

1条回答 默认 最新

  • dqy0707 2014-03-10 15:57
    关注

    When developing REST API's with Laravel (which I tend to return JSON from by default), I use the Postman client for testing. A var_dump() is pretty ugly when looking at the response if you have xdebug installed (which I do), so I tend to use print_r instead.

    For example, just this morning testing something out, I added this line in my controller:

    print_r($files); exit;
    

    And this is what postman showed me:

    enter image description here

    You can also use var_dump if you choose to. If you don't include an exit() statement, the application will continue to run and depending and how you have things setup, you may get more than you were looking for. For me, it is just easier to dump what I need and stop the application so I can quickly see what I need to.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?