As the question says. I'd like to dispatch to an sfDoctrineRoute from within another action, but I'd like to preserve the current request. That way I'm not redirecting and losing the current request info....
Have a look at the forward() method in case it's what you're looking for:
http://www.symfony-project.org/api/1_4/sfAction
As a side note, I'm sure you have your reasons for wanting this but I'd probably approach so that whatever you need from that other action is contained in the models. This way when you call it, you can pass the request parameters to it and "preserve" the current request - wherever in your application you're calling it from. If you really need to call another action, then I'd go ahead push the request to it fully. You can always redirect/forward and pass the request parameters with it.
Related
We have a CORS REST API. It's clean. It works. It's almost perfect. But...
I'm trying to implement an endpoint with request specific parameters. This endpoint should accept POST, PUT, LINK and DELETE requests. And I'm not sure how to implement these parameters.
An example:
A user wants to DELETE a model. There are two scenario's: one where the model is deleted and nothing else happens, and one where the model is deleted + a notification email is sent out.
Initially I implemented this parameter in a header called "X-NOTIFY-OWNER". This worked since it could be added to any of the 4 actions. But now we'd like to get rid of that header, since it's too specific to this single endpoint.
What whould be the best place to put this parameter? A query parameter sounds cleanest (since DELETE and LINK technically don't need a body), but query parameters should be used to filter content. A parameter in the request body would work too, and seems to be the prefered method; but it means sending a body with the DELETE and LINK actions...
Any thoughts on best practice in this case?
I would stick to a query string, DELETE is supposed to ignore the body and only read the URL so it makes sense to use a query string here.
You should use an URL parameter. As you stated, they should be used to filter output and an e-mail can be considered to be output.
I would recommend setting up a new endpoint for the cleanest solution.
example.com/endpoint
example.com/endpointAndNotify
You could either:
Setup the notify endpoint to extend the base endpoint and then add the notification logic to the notify action.
Abstract out the shared logic from both actions, update each action to extend the base class and then add the specific notification logic to the notify action
This way both endpoints stay clean and concise and if you define a standard for this endpoint, any other endpoints that need notification logic can use the same standard.
I'm a semi newbie so please bear with me... Note, I don't know either jQuery or Json at this point
In my MVC project (I'm not using a framework but the project combines a front controller with an MVC), I have:
1) a Controller, which sends some parameters to a DAO. The DAO runs a MySQL query and sends back to the Controller an array of articles.
2) a View layer where I want the user to be able to click a button to move from article to article. The way I'm proposing to do that is by a javascript Ajax call to get the next article in the array generated in the Controller.
My question is: what should be the URL called by the Ajax function? Obviously it cannot call the Controller (or can it?). Should I add a class of dedicated Ajax content vessels that the Controller would instantiate with the array? I have difficulties seeing how the View would find the right URL... Should the Controller pass the parameters to the View and let it request the query?
The XHR (also known as AJAX) calls are no different at controller level, then classical browser requests. The difference is only in what you expect to receive in response.
This means that, if you have fully realized views (no just dumb templates), the type of request should be important only to the views. You can easily distinguish them by adding extensions:
http://foo.in/user/list - simple request
http://foo.in/user/list.json - XHR request
The difference gets recognized mostly at the routing mechanism, which them sets specific details on the Request instance. When controller sees that Request instance has a isXHR flag, it tells the view: "Respond to this with something, that is not full HTML page".
Basically, the same controllers should handle both the normal and XHR calls. In fact, you do not care about, what type of request it is. Only whether you need to produce html, xml or json in the response.
P.S.: model layer should be completely unaffected by the type of requests.
From the list of above posts I assume you must know the Ajax syntax to call a method while editing articles.
How to do so is as follows:
1. Initially define an action inside your controller which servers your purpose (May be editing your articles in this context.)
2. Through ajax method specify the Controller and the action which you wants to call. (
At this juncture it should be Articles -- Controller, EditArticle -- Action).
The control automatically navigates to the particular action method.
Regards
Pavan.G
Depends on the framework you use. But generally:
You can use the Controller with sending a "flag" (in a GET variable for example), that it is a AJAX query, and then exit the function, but having different Controllers for AJAX queries are considered a nicer route :) Anyways, something similar to this:
function page() {
if($_GET["is_ajax"] == "1") {
// return the AJAX query
return;
}
// go on with showing the page
}
Hope this helps!
Should MVC Ajax requests be mapped the same way as regular requests in a MVC framework?
For example: Say I want to check and see if a username is available via ajax and have a method on a controller that performs this scope of work. Do I simply add a route that maps the ajax request to a particular controller method, or is there a better way to achieve this?
Thanks in advance.
When you are asking a ajax call to fetch data or information you pass the url to that page. But in case of MVC the url becomes controller/function_name. So in url of the ajax call it should be controller/function_name. And since this kind of request is handled by MVC you don't need to worry about it.
They use the same point of entry and flow.
Ajax or not, it's just an request needed of routing.
I have a self-rolled MVC framework that I am building, and up to this point have managed to avoid the need for any AJAX calls. Now, however, I'd like to create a real-time updating feed.
My question is, where are the handlers for the ajax calls usually stored in an MVC? Should I store them in the same controller that is involved in making the call?
For example, if my domain www.example.com/browse/blogs (browse is the controller, blogs is the method) is making an AJAX call for an updated list of blogs, would the call simply be to www.example.com/browse/update_list or something?
OR, so it be to a separate AJAX-only controller? www.example.com/ajax/update_blogs
How do you do it?
Best practice would be to disregard the fact it's an AJAX request entirely and to only concern yourself with what controller your AJAX request is pertinent to. If you were to have a catch-all AJAX controller you'd likely be grouping apples to pears, so to speak.
The main difference is that for AJAX requests you will likely need to avoid setting any layout (and more than likely view) data. This can easily be remedied by having a method in your parent Controller class which checks for valid AJAX requests:
protected function isAjax()
{
return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&
$_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest');
}
I'd say an Ajax request is exactly the same as a non-Ajax one : it works exactly the same way, actually, from a point of view of HTTP Protocol.
The only difference is that you are returning some non-formated data, as JSON or XML (hey, this is the same as generating an ATOM feed ^^ ), or only a portion of an HTML page.
So, I would treat those as any other "normal" HTTP request, and place them the way I would for non-Ajax requests.
A semi-alternate idea might be to have only one action in your controlller : /browse/blogs -- and always call that one.
But, it would detect if it's being via an Ajax request or not, and would :
return a full page if called via a "normal" request
or return only some data (or a portion of the page) if called via an Ajax request
Note : that's not a "wild" idea ; Zend Framework, for instance, provides some stuff to facilitate that (see 12.8.4.3. ContextSwitch and AjaxContext )
Even though you're not using asp.net MVC, I'd recommend you look through the nerd dinner tutorial, specifically the AJAX section. it will help answer some of your design questions.
They have a separate action on the same controller.
http://www.wrox.com/WileyCDA/Section/id-321793.html
I'm trying to access the final xhtml output right before it's sent to the browser as a string. The postDispatch() methods of the actions and plugins seem to be too early to do this. When I step through Zend_Controller_Front::dispatch() method using a debugger, I can access the desired output as a string right before $this->_response->sendResponse() is called at the very end by adding a watch expression $this->getResponse()->getBody(). However, there seems to be no dedicated hook to tap into right there. I need the final response body as a string in order to send it to Prince XML to generate a pdf. Does anybody know an elegant way to do this?
Thanks, Adrian
The Zend_Controller_Front plugin hooks are the following (from here):
routeStartup() is called before Zend_Controller_Front calls on the router to evaluate the request against the registered routes.
routeShutdown() is called after the router finishes routing the request.
dispatchLoopStartup() is called before Zend_Controller_Front enters its dispatch loop.
preDispatch() is called before an action is dispatched by the dispatcher. This callback allows for proxy or filter behavior. By altering the request and resetting its dispatched flag (via Zend_Controller_Request_Abstract::setDispatched(false)), the current action may be skipped and/or replaced.
postDispatch() is called after an action is dispatched by the dispatcher. This callback allows for proxy or filter behavior. By altering the request and resetting its dispatched flag (via Zend_Controller_Request_Abstract::setDispatched(false)), a new action may be specified for dispatching.
dispatchLoopShutdown() is called after Zend_Controller_Front exits its dispatch loop.
So dispatchLoopShutdown() is your hook to go - it's the last thing Zend_Controller_Front::dispatch() does before returning or sending the response.
Another option could be, even though they were designed for something completely different, to use Zend_View filters. These filters can be added to the Zend_View-instance and are called in Zend_View::render(). A filter is simply an instance of a class that provides a filter($buffer)-method that returns the filtered $buffer. But using this interface for something not related to filtering the ouptut, seems not to be the correct way actually.
I personally think, that a dispatchLoopShutdown()-plugin will be the way to go.
Here a sample on how to use view filters
http://matthewturland.com/2008/06/04/output-filters-in-zend_view/