Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams
  • $app->contentType('text/html; charset=utf-8');
  • header("Content-Type: application/json");
  • $app->response()->header('Content-Type', 'application/json;charset=utf-8');
  • I'm stuck... :-/

    I've downloaded the Slim/slim-skeleton via Composer.

    I need to return JSON in my Route.php :

    $app->get('/getStoresByBounds', function () use ($app, $stores) {
        $app->contentType('application/json');
        $myStores = $stores->getStores();
        $app->response()->write(json_encode($myStores));
        $app->stop();
                    Saw that in the documentation. But what is $oldResponse? I just need to send the response in the Route.
    – curly_brackets
                    Mar 3, 2016 at 9:52
                    Please check the Get Started section for that. The response is the second argument of the route handler. And now I think about your code... Are you using Slim 3? I assumed that because you never mentioned using an old version but your code attempts don't seem to match current versions.
    – Álvaro González
                    Mar 3, 2016 at 10:10
                    To @downvoter... Care to explain why a pointer to the specific documentation section that explains the subject is "not useful"?
    – Álvaro González
                    Mar 3, 2016 at 10:12
                    I'm using version 3, but I now see that the docs are only version 2... :-O Now I get why I'm so comfussed :-D
    – curly_brackets
                    Mar 3, 2016 at 10:29
    
    $app = new \Slim\App();
    $app->get('/getStoresByBounds', function (Request $request, Response $response) {
        $myStores = $stores->getStores();
        $response->getBody()->write(json_encode($myStores));
        $newResponse = $response->withHeader(
            'Content-type',
            'application/json; charset=utf-8'
        return $newResponse;
                    I can't use it in the route - it says "undefined class". Tried to use ($app) in the route, but it didn't work.
    – curly_brackets
                    Mar 3, 2016 at 9:53
                    When I try your ladder example, it still says that Slim is an undefined class. I use the latest version of Slim, version 3.2.1. In version 2.6.1 (my last project) there wasn't an issue... :-(
    – curly_brackets
                    Mar 3, 2016 at 10:09
                    Shouldn't I use version 3? It still has issues. Request is an undefined class... :-( I think I'm returning to version 2... :)
    – curly_brackets
                    Mar 3, 2016 at 10:23
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.