How to Access a Different Controller from Inside a Controller Symfony2

How to access a different controller from inside a controller Symfony2

You can define your controller as service, then get it in another controller.

In your services.yml define needed controller as a service:

services:
your_service_name:
class: YourCompany\YourBundle\Controller\YourController

Then in any controller you'll be able to get this service via container:

$yourController = $this->get('your_service_name');

There is some useful information about Controllers as Services in documentation

How to call controller function from other controller In symfony 3?

You can define your controller as service, then get it in another controller.

In your services.yml define needed controller as a service:

services:
service_name:
class: BundleName\Controller\YourControllerName

Then in any controller you'll be able to get this service via container:

$otherController = $this->get('service_name');
$otherController->methodName();

Symfony 2 - Call controller from another controller

$this->forward takes arguments in this order:

  1. Logical Name of controller action in string format i.e. 'AcmeHelloBundle:Hello:fancy'
  2. Parameters to be passed as request variables in array format i.e. array(
    'name' => $name,
    'color' => 'green',
    )

These parameters can be accessed in the controller using request access functions.

Symfony2 call controller from another controller

If you have a piece of logic that should be reused, it probably doesn't belong in the controllers. You should try moving it to a service, which is easy to do.

In src/BundleName/Resources/config/services.yml:

services:
service_name:
class: BundleName\Service\ServiceName
arguments: [@doctrine.orm.default_entity_manager]

Then, create BundleName\Service\ServiceName class (as shown in the docs) with the logic to be reused. An example below:

class ServiceName {

protected $entityManager;

public function __construct($entityManager) {
$this->entityManager = $entityManager;
}

public function addProduct($product) {
//Get the array, hydrate the entity and save it, at last.
//...
$entity = new Product();
//...
$this->entityManager->persist($entity);
$this->entityManager->flush($entity);
return $entity;

}

}

Then, in your actions, just call $this->get('service_name')->addProduct($array), or something like that.

Of course, if you want a controller action to be reused, you can use your controller as a service. I'd advice you to add a service layer, though.

How to use different controllers, depending on the user role, for the same route?

You can't do this with "route" declarations, since the route listener is executed with higher priority than the security listener. Both happen during the KernelEvents::REQUEST event, but routing comes before firewall.

When the route to controller mapping is being resolved, you do not have yet user information (which is why you can't simply attach another a listener and inject the user information on the Request object, so it's available to use in the route declaration for expression matching, for example).

Basically, one route, one controller. If you want to have diverging logic for these users, you'll have to apply it after you get into the controller.

Access to a different Controller

Using other controllers inside a controller is a sign of bad architecture. Usually, it means you have to split the controller into a service, which you can use everywhere, and a controller.

For instance, when you have a controller which has a parseAction which parses a file and you need to use that in another controller too, you must create a acme_demo.parser.the_file_type service (give it which name you want) and use that in both controllers:

// ...
class FirstController extends Controller
{
public function xxxAction()
{
$parser = $this->get('acme_demo.parser.the_file_type');

$data = $parser->parse(...);
}
}

// ...
class SecondController extends Controller
{
public function yyyAction()
{
$parser = $this->get('acme_demo.parser.the_file_type');

$data = $parser->parse(...);
}
}


Related Topics



Leave a reply



Submit