Handle AJAX requests in shared directories / files

I want to transfer all my methods from the controller, which are executed at the request of AJAX, in order to separate the folder. For example, create a file UserBundle/Ajax/Ajax.phpand put all my AJAX request methods into this file.

Is this the right approach to separate ajax requests from regular HTTP requests? I can not find examples of how to do this. Is this possible in symfony? Should I expand Symfony\Bundle\FrameworkBundle\Controller\Controllerin this file Ajax.php? It will be normal that there will be two folders in the package Ajaxand Controllercontains the controllers, but first for the ajax request and the second for the normal HTTP request?

Do you know any architectural design for this problem?

+4
source share
2 answers

I donโ€™t think there are any problems for this, just make sure you correctly determine the routing path:

Annotation routing example:

# app/config/routing.yml
app_bundle:
    resource: "@AppBundle/Controller"
    type:     annotation
    prefix:   /

app_bundle_ajax:
    resource: "@AppBundle/Ajax"
    type:     annotation
    prefix:   /

Should I extend Symfony \ Bundle \ FrameworkBundle \ Controller \ Controller in this Ajax.php file?

This is not necessary, but Symfony\Bundle\FrameworkBundle\Controller\Controllerprovides you with great shortcuts, such as $this->json(...);since Symfony 3.1

+1
source

You can create a simple AjaxController as a service that will receive ajax requests. Then you can redirect each request to different controllers and they will return JsonResponces ().

0
source

Source: https://habr.com/ru/post/1655471/


All Articles