Answer the question
In order to leave comments, you need to log in
How to remove part of the logic from the controller?
For example, we have a main page, which will contain different blocks (search, articles, registration, etc). Of course, the blocks will be repeated in other controllers. I need to somehow transfer the formation of variables / arrays passed to the template to generate these blocks into separate controllers (or other entities that I don't know). What I tried to do:
Create MainController
<?php
// /src/AppBundle/Controller/MainController.php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
class MainController extends Controller {
/**
* @Route("/", name="main")
*/
public function mainAction () {
$filter_form = $this->forward( 'app.test:testAction' );
return new Response ( json_encode(Array( 'filter_form' => $filter_form )) );
}
}
# app/config/services.yml
services: app.test: class: AppBundle\Controller\TestController
<?php
// /src/AppBundle/Controller/TestController.php
namespace AppBundle\Controller;
class TestController {
public function testAction () {
return 'STRING';
}
}
{"filter_form":{"headers":{}}}
{"filter_form":"STRING"}
Answer the question
In order to leave comments, you need to log in
1. Create a service class mySuperManager
2. Push all the logic into it
3. In the controller, ask mySuperManager to give the necessary data and pass them to the view or json or somewhere else)
Controller example:
An example of the mySuperService class method
In my case, mySuperManager immediately returns json, but in your situation, return what you need: an object, an array, a string, etc.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question