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.
Related
I think this is one of the more complex tricks to get right and therefore I have decided to elicit the help of the very knowledgeable people on StackOverflow. My scenario is as follows: I have two entities, a user and an account. A user is always linked to an account upon registration (and depending on the type of user, might be linked to more than one account. Upon registration the function saveUser() is called (via ajax from frontend) and the submitted form data is retrieved from the Request Object. This data is then passed to the function saveAccount($data) (which is called in the saveUser() function) in the form of a parameter and the account is created (sometimes called more than once with different data sets to create various accounts), which is linked to the user.
Now I want to create an account from my admin panel without creating a user, so I want to call saveAccount($data) directly via ajax (from frontend) and pass the form data to it as a PARAMETER (instead of retrieving it in the function via the Request Object), so that I can use the same saveAccount($data) function and that I do not have to create a saveAccount() which works the the Request variable. Does this make sense? Is this possible? If so, how would I go about doing this?
I did not post any code, as I did not see the need for it, this is more a conceptional problem, but if you require the code that I have thus far or if anything is unclear I will be happy to elaborate.
There should not be any saveAccount method, you just rely on relationships between entities, i.e. on a setAccount method, or to a addAccount one in case you need to add an entity to a Collection.
Then Doctrine will take care of saving and persisting everything.
For saving data, I would always rely on a RESTful interface [which you can create easily via FOSRestBundle], and send everything via ajax no matter what; you'll end up with a smoother interface and more maintainable code.
For instances where a controller function can be called either via AJAX with form data or internally by a another controller function the following solution works:
public function saveAccount($data = null)
{
if (empty($data)) $data = $this->getRequest()->request->all();
...
}
Then you can pass an array to the controller function in the same format as your form data array and it will use that data if passed to the function, otherwise it will retrieve the REQUEST (form) data.
I am using some variables in a Yii controller action. Now I want to validate it in another action of the same controller where I am getting the server response. I don't want to use session method. Is there any other way to implement my goal?
one of my action is request and the other is server response handling action.
I need to run some code prior to checking if a user's credentials are correct. Currently I'm achieving this with a custom event listener that fires on the kernel.request event and checks if the requested URL matches security.yml's check_path setting. But this is inefficient since it runs on every request. I'm aware of the onSecurityInteractiveLogin event, but I believe that fires after a successful login attempt. Does anyone know if there's a pre login event, or where I could dispatch a custom event myself?
So, there's no 'official' pre-login event. But thankfully it's not hard to set one up since Symfony2 is so extendable. The trick is to use your own service to handle authentication.
Symfony uses this class when using a login form:
Symfony\Component\Security\Http\Firewall\UsernamePasswordFormAuthenticationListener
If you override the security.authentication.listener.form.class parameter (originally defined in Symfony\Bundle\SecurityBundle\Resources\config\security_listeners.xml) you can use a custom listener that extends UsernamePasswordFormAuthenticationListener.
All that's left to do is override the attemptAuthentication() method to dispatch the custom event.
(Actually you also need to store the event dispatcher as a class property in __construct())
This method should work with other authentication methods - all you'd need to do is modify the appropriate listener (ie BasicAuthenticationListener, X509AuthenticationListener, etc.)
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.
All my previous projects have had this workflow on Contact pages
User submits form
Controller gets $_POST details
Controller validates details (and sets error messages if necessary)
Controller sends email
Controller redirects to thanks page
Is this the standard workflow?
I used to validate everything in controllers, and then did some more reading and they recommended against it. Therefore, should I send the $_POST details to a helper type object and let it do all the work (validation/sending)?
In controller we should only check validation. The main validation should be on model before operations with DB.
The controller file need to check & validate the user input data.
After getting & accumulating all the data, it needs to transfer the data to the Model file for checking with the database (if needed) & then need to do some other works from here (like setting sessions / cookies, or sending mails, or firing hooks, ...). However, the control must come back to the same controller method, as all the previous model functionalities must be fired by a method call, from the same controller method.
The proper view method must be called now, and then the output must be rendered to the console.
Hope it helps.
Validation is typically performed in the Model, not in the Controller.
This is because data structures are usually defined in the Model and it is best to compare the acquired data immediately before manipulation (i.e. inserting into a database, etc.).