The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

Store object/array to be used later in the same request

Vinícius Borriello's Avatar

Vinícius Borriello

05 Jul, 2012 05:45 PM

Hello,

While developing a REST API I came up with a question: what would be the best way to store an object (serialized or not) to be used later in the same request, session, flash or another one?

I'm trying to store an object/array and hook a function to convert the object/array to the desired format (basically JSON or XML) to the end of the request. Nowadays it prints the serialized object/array to the output buffer and a function hooked to slim.after reads the response body, unserializes it and then prints it.

I'd like to suggestions of a more efficient way to do that, since it seems to consume more resources.

Thanks!
Vinícius

  1. 1 Posted by Vinícius Borrie... on 05 Jul, 2012 05:47 PM

    Vinícius Borriello's Avatar

    Guess this post is in the wrong section. Sorry.

  2. Support Staff 2 Posted by Brian Nesbitt on 06 Jul, 2012 12:50 AM

    Brian Nesbitt's Avatar

    Yes, its ok I moved it to the questions category :-)

    If you are really just looking to stuff an object/array some place to simply use the object to respond with then why not use a custom view and put the object/array to respond with in the view? The view would be responsible for rending the object/array in the right format. You can still go through the normal $app->render('json'); except the first argument (which is typically the template) you can use as the output type. You'll see in the example how the api consumer could easily specify it. You could use typical view data as well but since you have the custom view I like the explicit setResponseObject() function so the view data key name is controlled in the template rather than some uncontrolled string.

    This will respond to a URI like:
    http://domain.com/api/users/34 <= default to json
    http://domain.com/api/users/34/json
    http://domain.com/api/users/34/xml
    http://domain.com/api/users/34/php

    index.php:

    <?php
    require 'vendor/autoload.php';
    
    $app = new Slim();
    
    require 'ApiView.php';
    $app->view(new ApiView($app));
    
    $app->get('/api/users/:id(/:type)', function ($id, $type = 'json') use ($app) {
        $user = array("id" => $id, "name" => "Brian Nesbitt"); //$user = $dataStore->fetchUser($id);
        $app->view()->setResponseObject($user);
        $app->render($type);
    });
    
    $app->run();
    

    ApiView.php

    <?php
    class ApiView extends Slim_View {
        private $app;
    
        public function __construct($app) {
            parent::__construct();
            $this->app = $app;
        }
    
        public function getResponseObject() {
            return $this->getData('responseObject');
        }
        public function setResponseObject($obj) {
            $this->setData('responseObject', $obj);
        }
    
        public function render($template) {
            $obj = $this->getResponseObject();
    
            if (!isset($obj)) {
                return '';
            }
    
            $response = $this->app->response();
    
            switch($template) {
                case 'json':
                    $response['Content-Type'] = 'application/json';
                    return json_encode($obj);
                case 'xml':
                    $response['Content-Type'] = 'text/xml';
                    return '<putrealcode here="true" />';
                case 'php':
                    $response['Content-Type'] = 'application/php';
                    return serialize($obj);
            }
        }
    }
    
  3. 3 Posted by Vinícius Borrie... on 06 Jul, 2012 12:33 PM

    Vinícius Borriello's Avatar

    Thanks a lot Brian! That's really the best alternative I'd pick.

  4. Brian Nesbitt closed this discussion on 06 Jul, 2012 01:36 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac