When using the multisite feature, it is sometimes useful to be able to generate cross-links between the different sites.
This allows you to link different resources referenced in the same content repository, but configured independently with different tree roots.
Usage
{# Linking a location #} < a href = "{{ url( %27ez_urlalias%27%2c%20%7b%27locationId%27_%2042%2c%20%27siteaccess%27_%20%27some_siteaccess_name%27%7d.html) }}" >{{ ez_content_name( content ) }}</ a > {# Linking a regular route #} < a href = "{{ url( _some_route_name_%2c%20%7b_siteaccess__%20_some_siteaccess_name_%7d.html) }}" >Hello world!</ a > |
namespace Acme\TestBundle\Controller; use eZ\Bundle\EzPublishCoreBundle\Controller as BaseController; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; class MyController extends BaseController { public function fooAction() { // ... $location = $this ->getRepository()->getLocationService()->loadLocation( 123 ); $locationUrl = $this ->generateUrl( $location , array ( 'siteaccess' => 'some_siteaccess_name' ), UrlGeneratorInterface::ABSOLUTE_PATH ); $regularRouteUrl = $this ->generateUrl( 'some_route_name' , array ( 'siteaccess' => 'some_siteaccess_name' ), UrlGeneratorInterface::ABSOLUTE_PATH ); // ... } } |
Important
url()
Twig helper).Troubleshooting
- The first matcher succeeding always wins, so be careful when using catch-all matchers like
URIElement
. - If passed siteaccess name is not a valid one, an
InvalidArgumentException
will be thrown. - If matcher used to match the provided siteaccess doesn't implement
VersatileMatcher
, the link will be generated for the current siteaccess. - When using
Compound\LogicalAnd
, all inner matchers must match. If at least one matcher doesn't implementVersatileMatcher
, it will fail. - When using
Compound\LogicalOr
, the first inner matcher succeeding will win.
Under the hood
To implement this feature, a new VersatileMatcher
was added to allow siteaccess matchers to be able to reverse-match.
All existing matchers implement this new interface, except the Regexp based matchers which have been deprecated.
The siteaccess router has been added a matchByName()
method to reflect this addition. Abstract URLGenerator and DefaultRouter
have been updated as well.
Note
SiteAccessRouterInterface
.
1 Comment
pr pr
Hi, in the PHP code example, since you meant to generate absolute URL's, you need to pass the constant UrlGeneratorInterface::ABSOLUTE_URL to
$this
->generateUrl
instead ofUrlGeneratorInterface::ABSOLUTE_PATH.