Skip to content

Instantly share code, notes, and snippets.

@bdunogier
Created December 2, 2015 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bdunogier/06e707fba0ba9f8bcb9a to your computer and use it in GitHub Desktop.
Save bdunogier/06e707fba0ba9f8bcb9a to your computer and use it in GitHub Desktop.
How to generate an absolute URI from a Location with eZ Platform
<?php
/**
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace BD\SandboxBundle\Command;
use eZ\Publish\Core\MVC\Symfony\Routing\RouteReference;
use eZ\Publish\Core\MVC\Symfony\SiteAccess;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
class SandboxCommand extends ContainerAwareCommand
{
protected function configure()
{
$this->setName('bd:sandbox');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$locationService = $this->getContainer()->get('ezpublish.api.service.location');
$routeRefGenerator = $this->getContainer()->get('ezpublish.route_reference.generator');
$requestStack = new RequestStack();
$request = new Request();
$requestStack->push($request);
$routeRefGenerator->setRequestStack($requestStack);
$this->getContainer()->get('ezpublish.siteaccess_router')->setSiteAccess(new SiteAccess('site'));
$router = $this->getContainer()->get('router');
$routeReference = $routeRefGenerator->generate($locationService->loadLocation(54));
$routeReference->set('siteaccess', 'other_site');
$output->writeln("url: " . $router->generate($routeReference, [], 'true'));
}
}
@bdunogier
Copy link
Author

In a web context, this should be sufficient:

$locationService = $this->getContainer()->get('ezpublish.api.service.location');
$routeRefGenerator = $this->getContainer()->get('ezpublish.route_reference.generator');
$router = $this->getContainer()->get('router');

$routeReference = $routeRefGenerator->generate($locationService->loadLocation(54));
$routeReference->set('siteaccess', 'other_site');

$link = $router->generate($routeReference, [], 'true');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment