Re: Little trick to return json

2014-01-23 Thread euromark
I didnt talk about requirements.
It is still good practice. See my post for details on that.


Am Donnerstag, 23. Januar 2014 10:28:05 UTC+1 schrieb Dakota:
>
> Euromark, you don't need to use .json in the url if the requested response 
> type is set to application/json (As long as you include the RequestHandler 
> component)
>
> On Wednesday, 22 January 2014 22:17:22 UTC+2, euromark wrote:
>>
>> You could cut down your code by almost have and drop every second line 
>> when following the documentation regarding JsonView:
>> http://book.cakephp.org/2.0/en/views/json-and-xml-views.html
>> No need to fight the framework and manually set response type and alike
>>
>> Also, I highly recommend using /users.json as request url. 
>> This way you also trigger a lot of Cake magic as well. And you are down 
>> to none of your code above, as CakePHP would do all that for you right away.
>> See www.dereuromark.de/2014/01/09/ajax-and-cakephp
>>
>>
>> Am Dienstag, 21. Januar 2014 16:59:11 UTC+1 schrieb Rubensvsx:
>>>
>>> Sorry for the poor english. 
>>> I created a piece of code that solved my problem in returning a JSON 
>>> object and helped me not to re-write all views. I await your feedback pros 
>>> and cons.
>>>
>>>  
>>>
 class AppController extends Controller {
 public $components = array('RequestHandler');

>>>  
>>>
>>> function beforeRender() {
 if ($this->RequestHandler->*isAjax*()) {
 $this->layout = '*ajax*';
 $this->RequestHandler->setContent('*json*', '
 *application/json*');
 $this->*viewVars *= json_encode($this->*viewVars*);
 *return false*;
 }
 }
 }
>>>
>>>
>>>
>>> If an Ajax request is performed automatically be returns a JSON object 
>>> with the query result.
>>>
>>> $.ajax({
 url: "http://localhost/aplication/users";,
 type: "POST",
 dataType: "*json*"
 });
>>>
>>>

-- 
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

--- 
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to cake-php+unsubscr...@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.


Re: Little trick to return json

2014-01-23 Thread Dakota
Euromark, you don't need to use .json in the url if the requested response 
type is set to application/json (As long as you include the RequestHandler 
component)

On Wednesday, 22 January 2014 22:17:22 UTC+2, euromark wrote:
>
> You could cut down your code by almost have and drop every second line 
> when following the documentation regarding JsonView:
> http://book.cakephp.org/2.0/en/views/json-and-xml-views.html
> No need to fight the framework and manually set response type and alike
>
> Also, I highly recommend using /users.json as request url. 
> This way you also trigger a lot of Cake magic as well. And you are down to 
> none of your code above, as CakePHP would do all that for you right away.
> See www.dereuromark.de/2014/01/09/ajax-and-cakephp
>
>
> Am Dienstag, 21. Januar 2014 16:59:11 UTC+1 schrieb Rubensvsx:
>>
>> Sorry for the poor english. 
>> I created a piece of code that solved my problem in returning a JSON 
>> object and helped me not to re-write all views. I await your feedback pros 
>> and cons.
>>
>>  
>>
>>> class AppController extends Controller {
>>> public $components = array('RequestHandler');
>>>
>>  
>>
>> function beforeRender() {
>>> if ($this->RequestHandler->*isAjax*()) {
>>> $this->layout = '*ajax*';
>>> $this->RequestHandler->setContent('*json*', '
>>> *application/json*');
>>> $this->*viewVars *= json_encode($this->*viewVars*);
>>> *return false*;
>>> }
>>> }
>>> }
>>
>>
>>
>> If an Ajax request is performed automatically be returns a JSON object 
>> with the query result.
>>
>> $.ajax({
>>> url: "http://localhost/aplication/users";,
>>> type: "POST",
>>> dataType: "*json*"
>>> });
>>
>>

-- 
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

--- 
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to cake-php+unsubscr...@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.


Re: Little trick to return json

2014-01-23 Thread Dakota
A far better, more scalable way of doing it (That is actually built into 
the framework):

*routes.php*

> Router::parseExtensions();

Router::setExtensions(array('json', 'xml'));

 
*AppController.php*

> class AppController extends Controller {

public $components = array('RequestHandler');

} 


*Any controller action that you want to JSONify:*

> public function index() {

$this->set('myData', $myData); 

$this->set('_serialize', 'myData');

} 


*Or, to match your original intentions, in AppController.php*

> class AppController extends Controller {
> public $components = array('RequestHandler');
>
 

function beforeRender() {

   $this->set('_serialize', array_keys($this->viewVars));
> }
> }


And then to make a request:

> $.ajax({
> url: "http://localhost/aplication/users";,
> type: "POST",
> dataType: "*json*" //or *xml* if you want xml data
> });


On Tuesday, 21 January 2014 17:59:11 UTC+2, Rubensvsx wrote:
>
> Sorry for the poor english. 
> I created a piece of code that solved my problem in returning a JSON 
> object and helped me not to re-write all views. I await your feedback pros 
> and cons.
>
>  
>
>> class AppController extends Controller {
>> public $components = array('RequestHandler');
>>
>  
>
> function beforeRender() {
>> if ($this->RequestHandler->*isAjax*()) {
>> $this->layout = '*ajax*';
>> $this->RequestHandler->setContent('*json*', '
>> *application/json*');
>> $this->*viewVars *= json_encode($this->*viewVars*);
>> *return false*;
>> }
>> }
>> }
>
>
>
> If an Ajax request is performed automatically be returns a JSON object 
> with the query result.
>
> $.ajax({
>> url: "http://localhost/aplication/users";,
>> type: "POST",
>> dataType: "*json*"
>> });
>
>

-- 
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

--- 
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to cake-php+unsubscr...@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.


Re: Little trick to return json

2014-01-22 Thread euromark
You could cut down your code by almost have and drop every second line when 
following the documentation regarding JsonView:
http://book.cakephp.org/2.0/en/views/json-and-xml-views.html
No need to fight the framework and manually set response type and alike

Also, I highly recommend using /users.json as request url. 
This way you also trigger a lot of Cake magic as well. And you are down to 
none of your code above, as CakePHP would do all that for you right away.
See www.dereuromark.de/2014/01/09/ajax-and-cakephp


Am Dienstag, 21. Januar 2014 16:59:11 UTC+1 schrieb Rubensvsx:
>
> Sorry for the poor english. 
> I created a piece of code that solved my problem in returning a JSON 
> object and helped me not to re-write all views. I await your feedback pros 
> and cons.
>
>  
>
>> class AppController extends Controller {
>> public $components = array('RequestHandler');
>>
>  
>
> function beforeRender() {
>> if ($this->RequestHandler->*isAjax*()) {
>> $this->layout = '*ajax*';
>> $this->RequestHandler->setContent('*json*', '
>> *application/json*');
>> $this->*viewVars *= json_encode($this->*viewVars*);
>> *return false*;
>> }
>> }
>> }
>
>
>
> If an Ajax request is performed automatically be returns a JSON object 
> with the query result.
>
> $.ajax({
>> url: "http://localhost/aplication/users";,
>> type: "POST",
>> dataType: "*json*"
>> });
>
>

-- 
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

--- 
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to cake-php+unsubscr...@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.


Re: Little trick to return json

2014-01-22 Thread stevensjn
This is an excellent idea. Thx for sharing.  Sent from my BlackBerry 10 smartphone on the TELUS network. From: RubensvsxSent: Wednesday, January 22, 2014 4:07 AMTo: cake-php@googlegroups.comReply To: cake-php@googlegroups.comSubject: Little trick to return jsonSorry for the poor english. I created a piece of code that solved my problem in returning a JSON object and helped me not to re-write all views. I await your feedback pros and cons. class AppController extends Controller {    public $components = array('RequestHandler');     function beforeRender() {        if ($this->RequestHandler->isAjax()) {            $this->layout = 'ajax';            $this->RequestHandler->setContent('json', 'application/json');            $this->viewVars = json_encode($this->viewVars);            return false;        }    }}If an Ajax request is performed automatically be returns a JSON object with the query result.$.ajax({  url: "http://localhost/aplication/users",  type: "POST",  dataType: "json"});



-- 
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
--- 
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscr...@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.




-- 
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
--- 
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscr...@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.


RE: Little trick to return json

2014-01-22 Thread Advantage+
Where is the magic? 

What's so special?

 

Simply spitting out the results in JSON……… and?

 

 

Dave Maharaj

Freelance Designer | Developer
Description: header_logo
www.movepixels.com  |    d...@movepixels.com  |  
709.800.0852

 

From: cake-php@googlegroups.com [mailto:cake-php@googlegroups.com] On Behalf Of 
Rubensvsx
Sent: Tuesday, January 21, 2014 12:29 PM
To: cake-php@googlegroups.com
Subject: Little trick to return json

 

Sorry for the poor english. 

I created a piece of code that solved my problem in returning a JSON object and 
helped me not to re-write all views. I await your feedback pros and cons.

 

 

class AppController extends Controller {
public $components = array('RequestHandler');

 

function beforeRender() {
if ($this->RequestHandler->isAjax()) {
$this->layout = 'ajax';
$this->RequestHandler->setContent('json', 'application/json');
$this->viewVars = json_encode($this->viewVars);
return false;
}
}
}

 

 

If an Ajax request is performed automatically be returns a JSON object with the 
query result.

 

$.ajax({
url: "http://localhost/aplication/users";,
type: "POST",
dataType: "json"
});

-- 
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
--- 
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to cake-php+unsubscr...@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.

-- 
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

--- 
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to cake-php+unsubscr...@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.
<>