Skip to content

Instantly share code, notes, and snippets.

@damaya
Created July 31, 2019 15:07
Show Gist options
  • Save damaya/8d63280af06fe6ee69b5853c3253bf97 to your computer and use it in GitHub Desktop.
Save damaya/8d63280af06fe6ee69b5853c3253bf97 to your computer and use it in GitHub Desktop.
How to use ez 5 controllers from legacy code
https://gist.github.com/lolautruche/5694727
AdminController.php
<?php
/**
* AcmeTestBundle/Controller/AdminController.php
*/
namespace Acme\TestBundle\Controller;
use eZ\Bundle\EzPublishCoreBundle\Controller;
/**
* Here is your Symfony controller, defined as a service (see service definition below)
**/
class AdminController extends Controller
{
public function myAdminAction( $myParam = null )
{
return $this->render(
'AcmeTestBundle::my_admin_action.html.twig',
array(
'some_var' => $myParam
)
);
}
}
my_admin_action.html.twig
{# AcmeTestBundle/Resources/views/my_admin_action.html.twig #}
<h1>We did it with a Symfony controller !</h1>
<h2>{{ some_var }}</h2>
services.yml
# AcmeTestBundle/Resources/config/services.yml
parameters:
my.admin.controller.class: Acme\TestBundle\Controller\AdminController
services:
# Defining this controller as a service so it can be called from the container
# in the legacy module
my.admin.controller:
class: %my.admin.controller.class%
calls:
# This setter is mandatory to be able to use all shorthand methods
- [setContainer, [@service_container]]
sftest.php
<?php
/**
* ezpublish_legacy/extension/myextension/modules/mymodule/sftest.php
*
* Simple module test to use Symfony
* This is your legacy module view
*/
$Module = $Params['Module'];
$Result = array();
$container = ezpKernel::instance()->getServiceContainer();
/** @var \Acme\TestBundle\Controller\AdminController $controller */
$controller = $container->get( 'my.admin.controller' );
$Result['content'] = $controller->myAdminAction( 'blabla' )->getContent();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment