Since on using AJAX, js will mostly be the one responsible on the front end. With that in mind.
If we use guzzle the flow will be like the one below.
View(request)->Ajax->controller(call api)->api_controller(return response)->controller(return response)->view
If we don't use guzzle then it will be like this
View(request)->Ajax->api_controller(return response)->view
So, aint it better for ajax to call the api_controller instead of the view_controller first?
Related
In Laravel, I would like to know how to make ajax call internally without using the the route system. I'm not sure what other ways I can access internal methods, without routes.
The reason I'm looking for this is because the way the application is setup, routes always return html, even if a JSON response, because the framework is loaded inside the content area of a parent website (using different framework) which always had a header and footer(ugh, I know). I have another post about this, but lets please stick to the question.
I just need this framework to talk to itself, from the $.ajax() request, straight to the controller, bypassing the route, so maybe a JSONP, or some way to access methods directly without going overboard.
I know I did this sort of thing using FOSJsRouting from Symfony in a previous app to accomplish this sort of thing, but I had to make a wrapper to access the methods. I don't want that kind of elaborate setup if possible here.
Are there other ways?
When it comes to Ajax, it looks like Symfony (v. 2.7) and AngularJS (v. 1.4) don't go well together ;-)
I see two ways to make them cooperate - my question is: Which one is better?
Adapt AngularJS to be compatible with Symfony
Adapt Symfony to be compatible with AngularJS
For 1, this is the best instruction I found:
http://harmssite.com/2014/09/mixing-angular-and-symfony/
Advantage: Can use $form->handleRequest($request); in Symfony to get all form fields at once.
Disadvantage: Cannot set default value's for <input>'s, cause AngularJS overwrites them. Solution:
Set it in Angular's controller (i.e. in app.js). But if the value comes from the database (i.e. Symfony), how can you get it into app.js? Render the entire app.js file through Symfony's templating system? => Looks like overkill to me.
Set it inside the HTML/twig file with ng-init, as shown here:
AngularJS - Value attribute on an input text box is ignored when there is a ng-model used?
=> This is more a workaround than a solution ;-)
For 2, the major disadvantage is that you need to read each form field separately into Symfony, using $post = $this->getRequest()->getContent(); as shown here:
AngularsJS POST JSON data to Symfony2
I haven't tried this approach, so I don't know if there are further problems down the way ;-)
After fiddling around with this for a while, I finally came to a solution: Kick out AngularJS completely!
As far as I understand it now, AngularJS is only the way to go, if you are processing your forms completely (or at least mainly) on the client-side. I guess, that's what they mean by "Single Page Application (SPA)".
If you're using Symfony for form processing, AngularJS probably makes your life harder, not easier.
I'm now using plain jQuery for JavaScript, and everything works like a charm (including Ajax), much more hassle-free than AngularJS was.
If you just need to handle AJAX Requests from AngularJS to Symfony2 server, using classic forms (without Symfony2 form builder), you can use this bundle :
qandidate/symfony-json-request-transformer
Add it as a dependency in your composer.json and handle your requests (POST & other methods) in the same way as classic request.
You can See my post about this problem : Handling HTTP Requests from AngularJS to Symfony2
Other tip, to use AngularJS in your twig templates, add this in your angular app :
var app = angular.module('yourApp', [],
function($interpolateProvider){
$interpolateProvider.startSymbol('<%');
$interpolateProvider.endSymbol('%>');
});
This will change your start symbol from {{ to <% and your end symbol from }} to %>
In my case, use AngularJS make my work easier and me more productive, especially coupled to a Symfony back-end, but it's very personal, and depending on why/how angular is used.
i'm Trying to implement a web application and it's like a mix of some of twitter , facebook , pinterest features so the app is highly dependent on asynchronous calls to the server , any help with that i just want to know whether to use just ajax or there is some other magic do do better .
you will need ajax to hit server side code. And will be in need soon when you need to call javascript from serveide code ie reverse ajax.
By using DWR framework you can acomplish both of them.
Other wise using jquery's ajax functionality will be a great help.
In simple terms, how is push achieved through ajax? Is this done through the same object used for conventional ajax requests?
In simple terms, how is push achieved through ajax?
The general idea is to always have a request to the server "hanging", so the server can respond to it whenever it wants to "push" something to the client.
Is this done through the same object used for conventional ajax requests?
It can be, but you can emulate it with an iframe as well. This field is in very active development currently, so maybe WebSockets and the EventSource should be mentioned here as well.
Wikipedia has a simple definition of the Comet model, used to make AJAX push work
http://en.wikipedia.org/wiki/Reverse_Ajax
It might be the standard in HTML5
http://en.wikipedia.org/wiki/Server-sent_events
A simple logic is to get updateds from the server after a prefixed time interval. Usually this is done by setInterval() method of javascript and the jquery ajax is used to check the server database. You can visit this URL http://tinytute.com/wordpress/2013/08/02/create-a-simple-jquery-php-based-push-notification/
I am getting information from the browser using Javascript / HTML5 that I want to pass back into Sinatra and Ruby.
How can I pass this information back into Sinatra so that I can use it in other parts of my code?
If you want to pass any information from the client-side (JavaScript) to the server-side (Sinatra), you need to use AJAX or something like Comet. You should serialize, encode your data, send it using XMLHttpRequest and handle in the server-side code. After that, you may return an answer to your client-side.