Skip to content

Instantly share code, notes, and snippets.

@alexander-schranz
Last active December 29, 2021 15:19
Show Gist options
  • Save alexander-schranz/96eb3f61cad26ab6b74d to your computer and use it in GitHub Desktop.
Save alexander-schranz/96eb3f61cad26ab6b74d to your computer and use it in GitHub Desktop.
Custom sulu controller which load some data from a service
<?php
namespace App\Controller\Website;
use Sulu\Bundle\WebsiteBundle\Controller\WebsiteController;
use Sulu\Component\Content\Compat\StructureInterface;
/**
* Controller which needs a custom service to add data to render twig template.
*/
class SuluExtendController extends WebsiteController
{
public function indexAction(StructureInterface $structure, $preview = false, $partial = false)
{
$response = $this->renderStructure(
$structure,
[
// here you can add some custom data for your template
'myData' => $this->get('my_custom_service')->getMyData(),
],
$preview,
$partial
);
return $response;
}
/**
* Optional do something with the content or view data of the template.
*/
protected function getAttributes($attributes, StructureInterface $structure = null, $preview = false)
{
$attributes = parent::getAttributes($attributes, StructureInterface $structure, $preview);
// here you can add some custom data for your template based on exist structure attributes
$attributes['moreCustomData'] = 'Custom Data';
return $attributes;
}
public static function getSubscribedServices()
{
$subscribedServices = parent::getSubscribedServices();
// here you can require services you need
$subscribedServices['my_custom_service'] = 'your_service_id_or_class';
return $subscribedServices;
}
}
// use controller in template <controller>App\Controller\Website\SuluExtendController::indexAction</controller>
// the custom data is directly available under the myData variable in twig. {{ dump(myData) }} and {{ dump(moreCustomData) }}
<?php
namespace App\Controller\Website;
use Sulu\Bundle\WebsiteBundle\Controller\DefaultController;
use Sulu\Component\Content\Compat\StructureInterface;
/**
* A simplier controller just adding some attributes to twig template.
*/
class SuluExtendController extends DefaultController
{
protected function getAttributes($attributes, StructureInterface $structure = null, $preview = false)
{
$attributes = parent::getAttributes($attributes, StructureInterface $structure, $preview);
// here you can add some custom data for your template based on exist structure attributes
$attributes['moreCustomData'] = 'Custom Data';
return $attributes;
}
}
// use controller in template <controller>App\Controller\Website\SuluExtendController::indexAction</controller>
// the custom data is directly available under the myData variable in twig. {{ dump(moreCustomData) }}
<?php
namespace App\Controller\Website;
use Sulu\Bundle\WebsiteBundle\Controller\WebsiteController;
use Sulu\Component\Content\Compat\StructureInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* Controller which needs a custom service to add data to render twig template.
*/
class SuluExtendController extends WebsiteController
{
public function indexAction(Request $request, StructureInterface $structure, $preview = false, $partial = false)
{
$myData = null;
if ($request->query->get('parameter')) {
$myData = $this->get('my_custom_service')->getMyData()
}
$response = $this->renderStructure(
$structure,
[
// here you can add some custom data for your template
'myData' => $myData,
],
$preview,
$partial
);
return $response;
}
public static function getSubscribedServices()
{
$subscribedServices = parent::getSubscribedServices();
// here you can require services you need
$subscribedServices['my_custom_service'] = 'your_service_id_or_class';
return $subscribedServices;
}
}
// use controller in template <controller>App\Controller\Website\SuluExtendController::indexAction</controller>
// the custom data is directly available under the myData variable in twig. {{ dump(myData) }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment