简体   繁体   中英

Zend 2 Route Hostname regex

I want to catch my (optional) subdomain and domain via the route type Hostname in Zend 2.

I'm bad at regexing and unable to find something working correctly.

'type' => 'Hostname',
'options' => array(
     'route' => '[:subdomain.]:project_domain',
     'constraints' => array(
          'subdomain' => '??REGEX??',
          'project_domain' => '??REGEX??',
     ),
),
'may_terminate' => false,

example 1 : www.domain.tld => subdomain = www ; project_domain = domain.tld

example 2 : test.www.domain.tld => subdomain = test ; project_domain = www.domain.tld

example 3 : domain.tld => project_domain = domain.tld

If someone can help me for this 2 regex patterns, thank you !

I ended up with this:

'type' => 'Hostname',
'options' => array(
   'route' => '[:subdomain.]:project_domain',
   'constraints' => array(
       'subdomain' => '([a-zA-Z0-9-]+)?([^.])',
       'project_domain' => '(([a-zA-Z0-9-]+)\.)+([a-zA-Z0-9]+)',
   ),
),

It works with my use cases :

  • test.domain.tld
  • domain.tld
  • test1.sub.domain.tld
  • asd-asd.sub.do-main.tld

If someone find something not working with, don't hesite to comment.

Thank you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM