Message-ID: <250084884.3548.1485853600430.JavaMail.confluence@ip-10-127-227-164> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_3547_669921301.1485853600425" ------=_Part_3547_669921301.1485853600425 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html How to retrieve root location

How to retrieve root location

Version compatibility

This recipe is compatible with eZ Publish 5.2 = / 2013.09

 

=20 =20

EZP 5.2 / 2= 013.09

Description

Knowledge of the root location is important since it can be a starting p= oint for API queries, or even links to home page, but as eZ Publish can be = used for multisite de= velopment, and as such root location can vary.

By default, root location ID is 2, but as it can be easily = be changed by configuration, the best practice is to retrieve it dy= namically.

Retrieving ro= ot location ID

Root location ID can be retrieved easily from ConfigResolver. Parameter name is content.tr= ee_root.location_id.

In a controller
=20
<?php
namespace Acme\TestBundle\Controller;

use eZ\Bundle\EzPublishCoreBundle\Controller;


class DefaultController extends Controller
{
    public function fooAction()
    {
        // ...
 
        $rootLocationId =3D $this->getConfigResolver()->getParameter(=
 'content.tree_root.location_id' );
 
        // ...
    }
}
=20

Retrieving t= he root location

From a template

Root location is exposed in the glo= bal Twig helper.

Making a link to homepage
=20
<a href=3D"{{ path( ezpublish.rootLocation ) }}" title=3D"L=
ink to homepage">Home page</a>
=20

From a controller

eZ Publish 5.2+ / 2013.11+
=20
<?php
namespace Acme\TestBundle\Controller;

use eZ\Bundle\EzPublishCoreBundle\Controller;

class DefaultController extends Controller
{
    public function fooAction()
    {
        // ...

        $rootLocation =3D $this->getRootLocation();

        // ...
    }
}
=20
Before 5.2 / 2013.11
=20
<?php
namespace Acme\TestBundle\Controller;

use eZ\Bundle\EzPublishCoreBundle\Controller;

class DefaultController extends Controller
{
    public function fooAction()
    {
        // ...

        $rootLocationId =3D $this->getConfigResolver()->getParameter(=
 'content.tree_root.location_id' );
        $rootLocation =3D $this->getRepository()->getLocationService(=
)->loadLocation( $rootLocationId );

        // ...
    }
}
=20

 

 

------=_Part_3547_669921301.1485853600425--