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

Validate format and Content Type

ntan72's Avatar

ntan72

03 Aug, 2012 08:29 AM

Hi,

i am having routes that accept as format json or xml

Example
$app->post('/object.:format', function($format) use ($app)

and i also have default condition for the format param
Slim_Route::setDefaultConditions(array( format => (json|xml) ));

Now i am trying to find a way to validate Content Type according to given format

What do i mean
When access object.json the Request Headers must be application/json and the Body contains a json string
When access object.xml the Request Headers must be application/xml and the Body contains a xml string

So far what i do is to validate inside the route

    $contentType = $app->request()->getContentType();
if($format=="json")
{
    if($contentType=="application/json")
    {
        $params = $app->request()->getBody();
        $params=json_decode($params,TRUE);
        if(is_array($params)){ $params=current($params); };

        var_dump($params);
    }
    else
    {
        unset($error);
        $error["code"]=400;
        $error["message"]="Bad Request";
        my_error_response($format,$error,"users");
    }
}

    if($format=="xml")
{
    if($contentType=="application/xml")
    {
        $params = $app->request()->getBody();
        $xmlfile = $params;
        $ob= simplexml_load_string($xmlfile);
        $json  = json_encode($ob);
        $params=json_decode($json,TRUE);
        if(is_array($params)){ $params=current($params); };
        var_dump($params);
    }
    else
    {
        unset($error);
        $error["code"]=400;
        $error["message"]="Bad Request";
        my_error_response($format,$error,"users");
    }
}

I am looking for a more inteligeng and oops way of achive that because i have almost 100 routes and if need to change anything will really hard to maintance

Thanks

  1. 1 Posted by ntan72 on 03 Aug, 2012 10:09 AM

    ntan72's Avatar

    After a lot of search i found that can implement througt

    $app->add(new Slim_Middleware_ContentTypes());

    but the problem here is that i dont have access to Route so i can get the params with

    $this->app->route()->getParams();

    How can i have access to Route inside Slim_Middleware_ContentTypes ?

  2. Support Staff 2 Posted by Brian Nesbitt on 03 Aug, 2012 07:34 PM

    Brian Nesbitt's Avatar

    Maybe you can move down the chain a bit and use a hook like slim.before.dispatch ? At that point the request has been parsed and the route parameters are setup.
    http://www.slimframework.com/documentation/stable#hooks-default

  3. 3 Posted by ntan72 on 07 Aug, 2012 10:05 AM

    ntan72's Avatar

    Its seems to working now.

    Thanks for your reply

  4. Support Staff 4 Posted by Brian Nesbitt on 07 Aug, 2012 12:45 PM

    Brian Nesbitt's Avatar

    Good stuff

  5. Brian Nesbitt closed this discussion on 07 Aug, 2012 12:45 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