title: Zend Framework 2 segment route required segment
body: <p>I am trying to make a route (or two) that will let me invoke two different actions with the below URL formats.</p>
  
  <pre><code>mydomain.com/profile
  mydomain.com/profile/1234/something
  </code></pre>
  
  <p>For the second format, the <code>1234</code> should be a required integer value, while <code>something</code> should be an optional string. The first format is simple by using a literal route. I thought I could add a segment child route for the second format, but I cannot get it to work. I tried to leave out the first format and only do the second one with a segment route, but I wasn't successful in that either.</p>
  
  <p>Here is what I tried:</p>
  
  <pre><code>'profile' =&gt; array(
      'type' =&gt; 'Zend\Mvc\Router\Http\Literal',
      'options' =&gt; array(
          'route' =&gt; '/profile',
          'defaults' =&gt; array(
              'controller' =&gt; 'User\Controller\User',
              'action' =&gt; 'profile'
          )
      ),
      'child_routes' =&gt; array(
          'profile_view' =&gt; array(
              'type' =&gt; 'Zend\Mvc\Router\Http\Segment',
              'options' =&gt; array(
                  'route' =&gt; '/:code[/:username]',
                  'constraints' =&gt; array(
                      'code' =&gt; '[0-9]*',
                      'username' =&gt; '[a-zA-Z0-9_-]*'
                  ),
                  'defaults' =&gt; array(
                      'controller' =&gt; 'User\Controller\User',
                      'action' =&gt; 'view_profile'
                  )
              )
          )
      )
  )
  </code></pre>
  
  <p>For <code>mydomain.com/profile</code>, I get the following errror:</p>
  
  <pre><code>Fatal error: Uncaught exception 'Zend\View\Exception\RuntimeException' with message 'No RouteMatch instance provided'
  </code></pre>
  
  <p>For <code>mydomain.com/1234/something</code>, I get the following error:</p>
  
  <pre><code>Fatal error: Uncaught exception 'Zend\Mvc\Router\Exception\InvalidArgumentException' with message 'Missing parameter "code"'
  </code></pre>
  
  <p>The <a href="http://framework.zend.com/manual/2.1/en/modules/zend.mvc.routing.html#zend-mvc-router-http-segment" rel="nofollow">manual states the following</a>:</p>
  
  <blockquote>
    <p>If a segment is optional, it should be surrounded by brackets. As an
    example, “/:foo[/:bar]” would match a “/” followed by text and assign
    it to the key “foo”; if any additional “/” characters are found, any
    text following the last one will be assigned to the key “bar”.</p>
  </blockquote>
  
  <p>Is that not exactly what I am doing? The above errors remain the same if I comment out the constraints.</p>
  
  <p>What am I missing here? Thanks in advance.</p>