I started writing with magento. I have to do some ajax request for my own php-script located somewhere in local pull. The questions are:
Where to store such php-scripts and with what address access them from ajax-request?
Just create a Controller that has an action that will handle the AJAX call. So if your module has a route of http://www.yoursite.com/yourscript, and you already have your IndexController, just put another action in there such as ajaxAction(). This action will simple print out the data you want to be consumed, and not render the rest of the page. Then you can point your AJAX call to http://www.yoursite.com/yourscript/index/ajax.
Related
I am working on a simple PHP site that involves needing to be able to forward a request made by the user to another page (note that I said forward, and not redirect). I am aware of how to redirect by manipulating the header variable, but I do not wish to do this, as explained below.
I am trying to write a very simple MVC-patterned mailing list app in PHP, drawing from my knowledge of the same in JSP. One of the things that I appreciated about JSP was that you could both forward or redirect a request. For my purposes, I need forward as I wish to keep the request parameters (whereas redirect will drop them).
Here is a description of what I wish to accomplish:
Retrieve input from a form (ie. /add.php)
Process the input in the page called by the form's action (ie. /process.php) and add a success message to the request object
Forward to another page (ie. /display.php) to display the success message in the request object
The only way I am aware of passing the request message to display is to add it to the request object and then access it from the forwarded page. However, the only way I have had success in transitioning to another page is through using the header method, which drops the request object (from what I can tell). I want to find a way to forward the request (object) to the new page, so that I can access the request variables from the new page.
Is there actually anyway to do this in PHP? Java's getRequestDispatcher.forward() is so nice, but I can't find an equivalent through searching. I've tried several similar questions, including the following, but I've never actually found one where both the question and the answer were what I wanted. Most of the answers seem to have something to do with cURL, but I don't want to actually retrieve a file, but simply forward a request in order to access the request object from another page.
Does PHP have an equivalent of Java's getRequestDispatcher.forward()?
Let me know if I should include anything else?
I believe you can do this with include. Before submitting the form just use, as inclusion, in main page:
include ("add.php"); - where the input forms are
after processing the information, include the display.php in the same way; using this, display.php will use same parameters from header, because is included in the same main page.
briefly: add.php, process.php and display.php will be modules for the mother page, but loaded in different state of form processing.
Hope it helps!
use curl with different method get,post. it will sent a request and also get back the response.
The most common method I see of passing messages to the end user from page to page is called session flashing.
This is when you store a variable temporarily in the session until it is read.
Assuming you already have sessions in use:
On process.php:
$_SESSION['message'] = 'Your data has been saved!';
On display.php:
if (isset($_SESSION['message'])) {
echo $_SESSION['message'];
unset($_SESSION['message']);
}
You could also store the entire Request object in the session.
So if I am aware, PHP provides just basic set of tools in this case. And there is nothing like "forward" in HTTP originally. It is just frameworks' abstraction/idea. There are two ways to achieve that: copying all params from request and doing new real HTTP request (with redirect) or internal forward: so framework would create fake request and call another controller and action without issuing a new physical HTTP request.
My repeated Ajax call deletes session flash data
I have a Javascript to make an Ajax call it calls my api every 5 seconds to update my user page.
I am returning a view with flash data which it has to keep for the next request but the Ajax call clears it and the next request is not completed.
Is there a way to complete it?
I can think to re-flash on an api call, but that seems weird. Any other way?
Since your ajax call needs access to the session information, you have to load the session, which will count as your access to the flash data. You will need your ajax call to reflash the data.
There is a reflash method on the session that will take care of this for you. You can either call it in the Controller method that handles your ajax call, or better yet, create a new middleware that reflashes session data, and attach the middleware to any route that needs this functionality.
Is there a way of creating a SOAP call in PHP that is not waiting for the called function to return?
I am calling a Magento Api function from my system to create a new product in the catalog. This can take quite a long time and I don't want my end user to wait until it is done.
I would do just a normal call, but don't block the user doing other stuff during the call. This way you can have your cake (the call) and eat it (get the results).
Most common method would be to do an AJAX call: you do the request via script to the PHP file, but the interface will be responsive / usable. If the user doesn't browse from the page you did the request at, you can even serve a response.
I'm pretty new to Symfony2 and am just wondering what would be the preffered way of doing this.
I have lots of admin actions that involve receiving form data and processing it. Naturally, when processing is finished, controller action sends return $this->redirect($this->generateUrl('.....'));.
Now since I have made a JavaScript that submits forms via ajax if browser supports it I need to modify my controller actions to return Response object containing json array but only if request was sent via ajax.
Is it possible to recognize if request sent to controller is AJAX request?
Where in directory tree is the preffered place to place class named ResponseHandler which would do the redirecting or returning json array based on type of request ? I realized Symfony2 is very strict about these things so I want to get it right from the start. Maybe there is even already bundled solution for this in it?
Update
I figured out I can use $this->getRequest->isXmlHttpRequest() in controller.
Question 2 still stands.
Where in directory tree is the preffered place to place class named ResponseHandler which would do the redirecting or returning json array based on type of request?
If you do an AJAX request you store the data in an parameter, a POST or GET parameter. You can access this parameter in the controller with:
// ...
public function finishAction()
{
// if you use a GET request
$data = $this->getRequest->query->get('my-get-parameter');
// and if you use a POST request
$data = $this->getRequest->request->get('my-get-parameter');
// ... do something with the data
}
However to answer your question:
The symfony core framework uses the Event Dispatcher component to trigger and attach events. The events are triggered everywhere in the Symfony code. You can attach a class to an event and when that event is triggered, you can change it. That way, you can modify the Response object.
Events that are thrown in the code can be found in a *Events class in that component. In this case, we want the REQUEST event. Read more on the event dispatcher and how to attach events in the documentation: Event Dispatcher Component and Symfony2 Framework specific documentation.
I realized Symfony2 is very strict about these things
Symfony really isn't strict about your directory structure. It delivers a default Standard Edition with the recommend structure, but you can change it to make it yours. And if a class is written in the PSR-0 standards symfony will load alle classes you need.
I have an application which will post a message on a friends wall using the Facebook.streamPublish() method, and this works perfectly. However, I want to also be able to save details about this post in my database.
All the information that needs to be stored is placed into hidden form fields and are all in place once the message has been sent, so I figure that all I need to do is redirect to a php file which will take this information, save it into the database and then redirect back to the main page.
The streamPublish call is:
Facebook.streamPublish(\'\', attachment, null, params.ids[curFriend]);
which I can follow with form.submit() which will cause the redirect to happen, but this submit() function gets called instantly, and doesn't wait for the streamPublish() popup to load, be used, and the action to be completed. How can I make my form.submit() not be called until the user has hit the 'publish' button in the popup which occurs?
StreamPublish also takes an optional callback function - http://developers.facebook.com/docs/?u=facebook.jslib.FB.Connect.streamPublish . Try setting your submit() function as the callback and it should get called once the user cancels the dialog or publishes it
I've not used the streampublish() function or the FB API, but I can guarantee that this is because the streampublish is handled by an ajax call, which is thus handled separately from the normal JS code chronology. So any code that should be executed when the ajax has reached a specific state needs to be added to the ajax statehandler, which the FB API may or may not give you access to.