Something like that would work.
But I would use a simpler and return early approach
(the second check is the inverse of the first and thus totally uncessary).
Am Donnerstag, 6. Februar 2014 15:07:21 UTC+1 schrieb advantage+:
>
> Thanks again for all your insight. Greatly appreciated.
>
>
>
> Since you seem to have quite a grasp on the topic I was hoping you might
> have some idea or point me where to look for this question.
>
>
>
> Since the site should work with and without JS enabled I am just starting
> small with a "Bookmark Add to favorites" type link. JS version click saved
> / and simple response alert "Added to favotites and the link Favorites
> (10)" now display "Favorites (11)" simply increase the counter.
>
>
>
> But how do you code the controller to reflect HTML or AJAX?
>
>
>
> Remove the $this->request->onlyAllow('ajax'); since we want HTTP and AJAX
>
>
>
> SNIP::
>
> add(){
>
> ………………………….
>
> $this->Bookmark->create();
>
> if ($this->Bookmark->save($this->request->data, true,
> $this->white)) {
>
>
>
> ??? detect how request was made ???
>
>
>
> if ($this->request->is('ajax'){
>
> $this->viewClass =
> 'Tools.Ajax';
>
> return the JSON
>
> }
>
>
>
> if (!$this->request->is('ajax'){
>
> return redirect to index
> regular HTML request
>
> }
>
> }
>
> }
>
>
>
> Does not seem very friendly, is this the correct way? If not what would be?
>
>
>
> Thanks again.
>
>
>
> Dave
>
>
>
> *From:* [email protected] <javascript:> [mailto:
> [email protected] <javascript:>] *On Behalf Of *euromark
> *Sent:* Thursday, February 06, 2014 9:08 AM
> *To:* [email protected] <javascript:>
> *Subject:* Re: DerEuroMark - Ajax and CakePHP Tutorial Question
>
>
>
> No that is totally what one would expect when browsing the json file.
>
> The thing is that this never happens with a normal user, as he executes
> AJAX which then calls this url.
>
> So never ever should it happen that a user really browses it directly
> (unless its a dev and he has a genuine reason to do so).
>
>
>
> Am Donnerstag, 6. Februar 2014 13:29:21 UTC+1 schrieb advantage+:
>
> I have read the post.
> The examples / links are directly on your site.
>
>
>
> If I do the same on my site I get the exact same thing. I'm only pointing
> out that if you access the .json via http you see the ugly json format
> screen. No error page (styled error page I mean).
>
> And sure odd of anyone is ever getting there by mistake a slim, it's still
> not something you would want on a website in production 1 would think. Does
> not look very professional if an error did occur and your end user / client
> was to see
>
> {
>
> - "name": "Not Found",
> - "url": "/sandbox/ajax_examples/country_provinces_ajax.json"
>
> }
>
> That’s all I was saying, not a criticism or anything on your part…..I said
> the post was excellent and well done.
> This is just what I came across in the process.
>
>
>
>
>
> *From:* [email protected] [mailto:[email protected]] *On
> Behalf Of *euromark
> *Sent:* Thursday, February 06, 2014 6:52 AM
> *To:* [email protected]
> *Subject:* Re: DerEuroMark - Ajax and CakePHP Tutorial Question
>
>
>
> Sounds like you didnt read the whole post.
>
> You probably skipped the "Last tips" part at the end - or it would be
> working
>
>
>
> Am Mittwoch, 5. Februar 2014 18:39:20 UTC+1 schrieb advantage+:
>
> Asking simply because if you go here for example:
>
>
>
>
> http://sandbox.dereuromark.de/sandbox/ajax_examples/country_provinces_ajax.jsonvia
> http you see:
>
>
>
> {
>
> - "name": "Not Found",
> - "url": "/sandbox/ajax_examples/country_provinces_ajax.json"
>
> }
>
>
>
> If there was no .json extension and accessed http you get the expected error
> page.
> Which is what I was expecting with the $this->request->onlyAllow('ajax'); but
> even accessing the .json (above link) you still end up with a result so the
> onlyAllow does not seem to *only allow ajax* since I can clearly see a result
> via http. It's just in JSON format.
>
> There is no error / exception throw caught. It still serves up the controller
> action. I was under the impression that if this controller method is accessed
> anyway other than "ajax" throw the exception but as you can see it's still
> accessible.
>
> Too me it just looks ugly when it's just a blank page and no explanation just
> a few brackets and text. (hence the point of customizing the error pages so
> it can match the look and feel of the site)
>
>
>
> That’s why I was asking about the removal of the .json extension so the end
> user will only know site.com/this/is/here and cake will know via the passed
> "ext" => 'json" param to use the correct view.
>
> Just not display the extension to the user.
>
>
>
> http://sandbox.dereuromark.de/sandbox/ajax_examples/country_provinces_ajax
>
>
>
> *CakePHP Sandbox App*
>
> *Not Found*
>
> *Error: *The requested address
> *'/sandbox/ajax_examples/country_provinces_ajax'* was not found on this
> server.
>
>
>
> Thanks again,
>
> Dave
> ------------------------------
>
> Author: dereuromark | github.com/dereuromark/cakephp-sandbox |
> Contact<http://sandbox.dereuromark.de/contact>
>
>
>
>
>
> *From:* [email protected] [mailto:[email protected]] *On
> Behalf Of *euromark
> *Sent:* Wednesday, February 05, 2014 1:43 PM
> *To:* [email protected]
> *Subject:* Re: DerEuroMark - Ajax and CakePHP Tutorial Question
>
>
>
> there is no need to remove it - but of course you can.
>
> it is good practice (that is outlined in the post if you read it) to
> always call the url with the correct extension of what you are expecting.
>
> rss => .rss
>
> xml => .xml
>
> ...
>
> and so for an ajax requestion with a json response it would be .json
>
>
>
> I would only call the url extension less if I was using a plain html
> response. That is also outlined in the post.
>
>
>
>
>
> Usually you would want a different layout to be rendered when using json
> instead of html.
>
> But yes, you can easily deactivate the subfolder here if necessary.
>
>
>
>
>
> Am Mittwoch, 5. Februar 2014 17:22:41 UTC+1 schrieb advantage+:
>
> First off great article!
>
> http://www.dereuromark.de/2014/01/09/ajax-and-cakephp/
>
>
> Very informative.
>
>
>
> My only question is there a way to remove the .json extension in the url?
> Or is it needed to differentiate between views?
>
>
>
> Only other thing I came across was my AppController isAuthorized() I have
> based on user logged in or not
>
> $this->layout = 'admin'; so initially I was getting errors
> /app/View/Layouts/json/admin.ctp', missing.
>
> But just added $this->layout = false; to solve that.
>
>
>
> Thanks,
>
> Dave
>
> --
> 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 [email protected].
> To post to this group, send email to [email protected].
> 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 [email protected].
> To post to this group, send email to [email protected].
> 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 [email protected] <javascript:>.
> To post to this group, send email to [email protected]<javascript:>
> .
> 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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.