Page 1 of 1

[SOLVED] How to return application/json from module

Posted: Thu Feb 28, 2013 9:31 am
by cve
I want to return clean json response from my module action on the frontend for process it by backbonejs or jquery, my question is how can I do it? In my module directory named Recipes I have file: action.get_recipes.php which contains this:

Code: Select all

<?php
if (!isset($gCms)) exit;

$recipesPerPage = $this->GetPreference('recipes_per_page', 6);

$recipes = $this->prepareRecipes(array('status' => 1), array(), $recipesPerPage, 0);
$this->smarty->assign('recipes', $recipes);
$this->smarty->assign('get_recipes_url', $this->CreateFrontendLink( $id, $returnid, 'get_recipes', $contents='', $params=array('disable_theme' => true), $warn_message='',
					 $onlyhref=true, $inline=true, $addtext='', $targetcontentonly=false, $prettyurl='' ));
$this->smarty->assign('module_js_url', $this->getJsPath());

echo $this->ProcessTemplateFromDatabase((!empty($params['template']) ? $params['template'] : 'recipes_list'));
genrated link (get_recipes_url) is:

Code: Select all

http://local.mydomain/index.php?mact=Recipes,m1b0ee,get_recipes,1&m1b0eedisable_theme=1&m1b0eereturnid=18
which shows:

Code: Select all

</__body>
<a class="top-header" href="http://local.mydomain">Â </a>
<div class="top-menu"> 

  
<ul>
<ul>

<li><a href="http://local.mydomain/main/e-sklep"><span>E-SKLEP</span></a>


</li>

<li><a href="http://local.mydomain/produkty"><span>Produkty</span></a>


</li>

<li><a href="http://local.mydomain/firma"><span>Firma</span></a>


</li>

        <li class="menuactive"><a class="menuactive" href="http://local.mydomain/przepisy"><span>Przepisy</span></a>


</li>

<li><a href="http://local.mydomain/dieta-dr-budwig"><span>Dieta dr Budwig</span></a>


</li>

<li><a href="http://local.mydomain/kontakt"><span>Kontakt</span></a>


</li></ul></li>
</ul>
</div>



    <div class="top-section">
<div class="slider shadow"><img src="uploads/images/mydomain/slider_content_1.png" alt="" /></div>

<!-- start content -->
[{"id":"1","title":"Testowy przepis nr 1","photos":["http:\/\/local.mydomain\/uploads\/_CGSmartImage\/krajobrazy-lasy-1600-1200-38791-ad91520efd25a36d90ff1729de5d75f2.jpg","http:\/\/local.mydomain\/uploads\/_CGSmartImage\/schab z bratkiem pan duza-5218033963887f68c2076d681bcf650f.jpg"]},{"id":"2","title":"Testowy przepis nr 2","photos":["http:\/\/local.mydomain\/uploads\/_CGSmartImage\/krajobrazy-lasy-1600-1200-38791-ad91520efd25a36d90ff1729de5d75f2.jpg","http:\/\/local.mydomain\/uploads\/_CGSmartImage\/schab z bratkiem pan duza-5218033963887f68c2076d681bcf650f.jpg"]},{"id":"3","title":"Testowy przepis nr 3","photos":["http:\/\/local.mydomain\/uploads\/_CGSmartImage\/krajobrazy-lasy-1600-1200-38791-ad91520efd25a36d90ff1729de5d75f2.jpg","http:\/\/local.mydomain\/uploads\/_CGSmartImage\/schab z bratkiem pan duza-5218033963887f68c2076d681bcf650f.jpg"]}]
$this->json():

Code: Select all

public function json($params) {
        header('Content-type: application/json');
        exit(json_encode($params));
    }
I want to return clean json for that, how to do it?

I'm using 1.11.4.

Re: How to return application/json from module

Posted: Thu Mar 14, 2013 12:55 pm
by Dabnis
I am working on a module at present RESTServer that I am developing for use with the backbone.js MVC framework.

See this post for details & links.
http://forum.cmsmadesimple.org/viewtopi ... =6&t=65653

To help me get the functionality right for CMSMS I would appreciate any input you may have.
Jonathan

Re: How to return application/json from module

Posted: Thu Mar 14, 2013 2:16 pm
by calguy1000
1. Ajax URLS should have the showtemplate=false parameter appended so that the template associated with the page is not appended.
2. You should (for safety purposes) clear out all output buffers before sending your own headers and data.
3. You echo the json/xml data and then exit() the script.

i.e: (some js that does ajax, uses CGSimpleSmarty's {module_action_link}

Javascript code:

Code: Select all

<__script__ type="text/javascript">
$('#somediv').load('{module_action_link module=mymodule action=myaction param1-val1 urlonly=1 jsfriendly=1}&showtemplate=false');
</__script>
PHP code:

Code: Select all

$handlers = ob_list_handlers(); 
for ($cnt = 0; $cnt < sizeof($handlers); $cnt++) { ob_end_clean(); }
echo json_encode(array('data1'=>'val1','data2'=>'val2'));
exit;

Re: How to return application/json from module

Posted: Thu Mar 14, 2013 2:36 pm
by Dabnis
With regard to calguy's comments:

The showtemplate => false is taken care of within the routing / mapping process as described here : http://www.cmsms.dabnis.com/module-base ... ality.html
and the headers issue is partly resolved here http://www.cmsms.dabnis.com/controller-base.html via the respond function.

I will release a beta for testing by those who are interrested.
Thanks for your input.