0

I am working with ZF2 and trying to setup Route configuration that uses a colon separator.

For example, the web address could be www.example.com/namespace:subject and I want to send it to a specific controller, action with the two variables. I am trying to use a Regex since the colon ":" is a special character for segments. Is there a nice way to do this? Here is my route configuration:

'dataReqs' => array(
    'type' => 'regex',    
    'options' => array(
        'regex' => '/(?<namespace>[^:]+).(?<subject>[a-zA-Z0-9_-]+)',
        'defaults' => array(
            'controller' => 'Application\Controller\Data',
            'action'     => 'get',
        ),
        'spec' => '/%namespace%:%subject%',
    ),
),

EDIT: I want to use the colon as the prefix:resource format is commonly used in RDF syntax (http://www.w3.org/TR/2007/PR-rdf-sparql-query-20071112/#QSynIRI). For instance, a long uri like http://dbpedia.org/data/Semantic_Web with a @prefix dbp: http://dbpedia.org/resource/ may be referred in a document with dbp:Semantic_Web. So for my Linked Data server I could direct requests and include the prefix (namespace) and the resource name; eg http://myserver.com/dbp:Semantic_Web. While I am using the segment combinations /namespace/resource for now, it would be nice to handle a route with prefix:resource syntax.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
vberkel
  • 76
  • 6

2 Answers2

0

Do not use colon in your route. It isn't good practice, because colon is reserved character(see https://www.rfc-editor.org/rfc/rfc3986#section-2.2)

Community
  • 1
  • 1
kormik
  • 839
  • 8
  • 18
0

I'm inclined to agree with kormik. Why do you want to specify URL's in that way? What is wrong with the default behavior?

www.example.com/namespace/subject

eg:

www.example.com/somenamespace/10

or even:

www.exmple.com/namespace/namespace/subject/subject

eg

www.example.com/namespace/somenamespace/subject/10

you can easily grab these parameters in the controller like so:

$ns = $this->params()->fromRoute('namespace',0);
$subject = (int) $this->params->fromRoute('subject',0);

You would need to modify the route config also.

Aydin Hassan
  • 1,465
  • 2
  • 20
  • 41