Message-ID: <969141406.4430.1485866462164.JavaMail.confluence@ip-10-127-227-164> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_4429_2097622755.1485866462164" ------=_Part_4429_2097622755.1485866462164 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html Siteaccess Matching

Siteaccess Matching

=20 =20

Siteaccess matching is done through eZ\Publish\MVC\SiteAccess\Ma= tcher objects. You can configure this matching and even devel= op custom matchers.

Configuration

You can configure siteaccess matching in your main ezpublish/con= fig/ezpublish.yml :

ezpublish.yml
=20
ezpublish:
    siteaccess:
        default_siteaccess: ezdemo_site
        list:
            - ezdemo_site
            - eng
            - fre
            - fr_eng
            - ezdemo_site_admin
        groups:
            ezdemo_site_group:
                - ezdemo_site
                - eng
                - fre
                - fr_eng
                - ezdemo_site_admin
        match:
            Map\URI:
                ezdemo_site: ezdemo_site
                fre: fre
                ezdemo_site_admin: ezdemo_site_admin
=20

 

You need to set several parameters:

ezpublish.siteaccess.default_siteaccess is the default = siteaccess that will be used if matching was not successful. This ensures t= hat a siteaccess is always defined.

ezpublish.siteaccess.list is the list of all available = siteaccesses in your website.

(optional) ezpublish.siteaccess.groups defines= which groups siteaccesses are member of. This is useful when you want to m= utualize settings between several siteaccesses and avoid config duplication= . Siteaccess groups are considered as regular siteaccesses as far as config= uration is concerned.

A siteaccess can be part of several groups.

A siteaccess configuration has always precedence on the group configurat= ion.

ezpublish.sitea= ccess.match= holds the matching configuration. It consists in a hash where the key is t= he name of the matcher class. If the matcher class doesn't start with a \, it will be considered = relative to e= Z\Publish\MVC\SiteAccess\Matcher (e.g. Map\Host will refer to eZ\Publish\MVC\SiteAccess\Matcher\Map\Host)

Every custom matcher can be specified with a fully qualified class name (e.g. \My\SiteAccess\Matcher<= /code>) or by a service identifier prefixed by @ (e.g. @my_matcher_service).

  • In the case of a fully qualified class name, the matching configu= ration will be passed in the constructor.
  • In the case of a service, it must implement eZ\Bundle\EzPub= lishCoreBundle\SiteAccess\Matcher. The matching configuration will b= e passed to setMatchingConfiguration().

Available matchers

Name Description Configuration Example
URIElement Maps a URI element to a siteaccess.
This is t= he default matcher used when choosing
URI matching in setup wizard.&nbs= p;

The element number you want to match (startin= g from 1).

=20
ezpublish:
    siteaccess:
        match:
            URIElement: 1
=20

Imp= ortant: When using a value > 1,
it will concatenate the ele= ments with _

URI: /ez= demo_site/foo/bar

Element number: 1
Matched siteaccess: ezdemo_site

=

Element number: 2
Matched siteaccess: ezdemo_site_foo 

URIText Matches URI using pre and= /or post sub-strings
in the first URI segment

The prefix and/or suffix (none = are required)

=20
ezpublish:
    siteaccess:
        match:
            URIText:
                prefix: foo
                suffix: bar
=20

URI: /fo= otestbar/my/content

Prefix: foo
Suffix: barMatched siteaccess: test 

HostElement Maps an element in the host name t= o a siteaccess.

The element number you want to = match (starting from 1).

=20
ezpublish:
    siteaccess:
        match:
            HostElement: 2
=20

Host name: www.example.com

Element number: 2
Matched site= access: example 

HostText Matches a siteaccess in the host n= ame,
using pre and/or post sub-strings.

The prefix and/or suffix (none = are required)

=20
ezpublish:
    siteaccess:
        match:
            HostText:
                prefix: www.
                suffix: .com
=20

Host name: www.foo.com

Prefix: www.
Suffix: .com
<= u>Matched siteaccess: foo 

Map\Host Maps a host name to a siteaccess.<= /td>

A hash map of host/siteaccess

=20
ezpublish:
    siteaccess:
        match:
            Map\Host:
                www.foo.com: foo_front
                adm.foo.com: foo_admin
                www.stuff.fr: bar_front
                adm.stuff.fr: bar_admin
=20
Map:
  • www.foo.com =3D> foo_front
  • admin.foo.com =3D> foo_admin 

Host name: www.example.com

Matched siteac= cess: foo_front

Map\URI Maps a URI to a siteaccess

A hash map of URI/siteaccess

=20
ezpublish:
    siteaccess:
        match:
            Map\URI:
                something: ezdemo_site
                foobar: ezdemo_site_admin
=20

URI: /so= mething/my/content

Map:

  • something =3D> ezdemo_site
  • foobar =3D> ezdemo_site_admin

Matched siteaccess: ezdemo_site

Map\Port Maps a port to a siteaccess

A has map of Port/siteaccess

=20
ezpublish:
    siteaccess:
        match:
            Match\Port:
                80: foo
                8080: bar
=20

URL: http://ezpublish.dev:8080/my/content

<= p>Map:

  • 80: foo
  • 8080: bar

Matched siteaccess: bar

Regex\Host Matches against a regexp and extra= ct a portion of it

The regexp to match against and the captured element to use

=20
ezpublish:
    siteaccess:
        match:
            Regex\Host:
                regex: "^(\\w+_sa)$"
                # Default is 1
                itemNumber: 1
=20

Host name: example_sa

regex: ^(\\w+)_sa$
ite= mNumber: 1

Matched siteaccess: example

Regex\URI Matches against a regexp and extra= ct a portion of it

The regexp to match again= st
and the captured element to use

=20
ezpublish:
    siteaccess:
        match:
            Regex\URI:
                regex: "^/foo(\\w+)bar"
                # Default is 1
                itemNumber: 1
=20


URI: /fo= otestbar/something

regex: ^/foo(\\w+)bar
itemNumb= er: 1

Matched siteaccess: test 

 

Compound siteaccess= matcher

The Compound siteaccess matcher allows to combine several matchers toget= her, which is performed using the legacy host_uri= .

For that, the following matchers can be used in your siteaccess match:

ezpublish.yml
=20
ezpublish:
    siteaccess:
        default_siteaccess: ezdemo_site
        list:
            - ezdemo_site
            - eng
            - fre
            - fr_eng
            - ezdemo_site_admin
        groups:
            ezdemo_site_group:
                - ezdemo_site
                - eng
                - fre
                - fr_eng
                - ezdemo_site_admin
        match:
            Map\URI:
                ezdemo_site: ezdemo_site
                fre: fre
                ezdemo_site_admin: ezdemo_site_admin
            # The following mimics host_uri for 2 different configuration.
            Compound\LogicalAnd:
                my_compound_rule_1:
                    # Nested matchers, with their configuration.
                    # No need to precise their matching values (true will s=
uffice).
                    matchers:
                        Map\URI:
                            the_front: true
                        Map\Host:
                            ezpublish.dev: true
                    # The siteaccess to match in the end
                    match: ezdemo_site=20
                my_compound_rule_2:
                    matchers:
                        Map\URI:
                            the_admin: true
                        Map\Host:
                            ezpublish.dev: true
                    match: ezdemo_site_admin
            # Matching hosts as host: siteaccess
            Map\Host:
                ezpublish.dev: ezdemo_site
                admin.ezpublish.dev: ezdemo_site_admin
=20

Matching by request h= eader

It is possible to define which siteaccess to use by setting a X-= Siteaccess header in your request. This can be useful for REST req= uests.

In such case, X-Siteaccess must be the siteacce= ss name (e.g. ezdemo_site).

Matching by env= ironment variable

It is also possible to define which siteaccess to use directly via an EZPUBLISH_SITEACCESS environment variable.

This is recommended if you want to get performance gain= since no matching logic is done in this case.

You can define this environment variable directly from your web server c= onfiguration:

Apache VirtualHost example
=20
# This configuration assumes that mod_env is activated
<VirtualHost *:80>
    DocumentRoot "/path/to/ezpublish5/web/folder"
    ServerName example.com
    ServerAlias www.example.com
    SetEnv EZPUBLISH_SITEACCESS ezdemo_site
</VirtualHost>
=20

This can also be done via PHP-FPM configuration file, if you use i= t. See PHP-FPM docu= mentation for more information.

Note about precedence

 The precedence order for siteaccess matching is the following (the= first matched wins):

  1. Request header
  2. Environment variable
  3. Configured matchers

 

URILexer and sema= nticPathinfo

In some cases, after matching a siteaccess, it is neecessary to modify t= he original request URI. This is for example needed with URI-based matchers= since the siteaccess is contained in the original URI and it is not part o= f the route itself.

The problem is addressed by analyzing this URI and by modifying= it when needed through the URILexer interface.

URILexer interface
=20
/**
 * Interface for SiteAccess matchers that need to alter the URI after match=
ing.
 * This is useful when you have the siteaccess in the URI like "/<siteac=
cessName>/my/awesome/uri"
 */
interface URILexer
{
    /**
     * Analyses $uri and removes the siteaccess part, if needed.
     *
     * @param string $uri The original URI
     * @return string The modified URI
     */
    public function analyseURI( $uri );
    /**
     * Analyses $linkUri when generating a link to a route, in order to hav=
e the siteaccess part back in the URI.
     *
     * @param string $linkUri
     * @return string The modified link URI
     */
    public function analyseLink( $linkUri );
}
=20

Once modified, the URI is stored in the semanticPathinfo request attribute, and the original pathinfo is not modified.

 

------=_Part_4429_2097622755.1485866462164--