# Skip optional Route-Params

**URL:** <https://discourse.laminas.dev/t/skip-optional-route-params/3777>\
**Category:** Components & MVC\
**Tags:** laminas-router\
**Created:** [September 26, 2024, 8:21am UTC](https://discourse.laminas.dev/t/skip-optional-route-params/3777 "2024-09-26T08:21:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![MaSa759](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/masa759/32/1625_2.png) [@MaSa759](https://discourse.laminas.dev/u/MaSa759)\
**Post date:** [September 26, 2024, 8:21am UTC](https://discourse.laminas.dev/t/skip-optional-route-params/3777/1 "2024-09-26T08:21:41Z")

</div>

Hi,

I want to set up different languages for my application. Therefore I configured my routes like this:

```
[
    'type' => 'segment',
    'options' => [
        'route' => '[/:lang]/',
        'defaults' => [],
        'constraints' => [
             'lang' => '(de|en|fr)'
        ]
    ],
    'may_terminate' => true,
    'child_routes' => [...]
]

```

All other routes are child-routes.

My expectations are like this:

**default language (de)**  
/foo  
/bar

**english**  
/en/foo  
/en/bar

**french**  
/fr/foo  
/fr/bar

Unfortunately the current configuration throws an exception: _InvalidArgumentException: Missing parameter “lang”_ on generating the urls. If I set a default [‘lang’ =\> ‘de’] all generated links always have the default-language as part of the url

/de/foo  
/de/bar

Is there a way around that?

---

<div class="post-metadata">

**Author:** ![zigu007](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/zigu007/32/1753_2.png) [@zigu007](https://discourse.laminas.dev/u/zigu007)\
**Post date:** [September 28, 2024, 11:47am UTC](https://discourse.laminas.dev/t/skip-optional-route-params/3777/2 "2024-09-28T11:47:43Z")

</div>

I assume you are using `laminas-view` Url Helper? One way could be to override the url generation method.

---

<div class="post-metadata">

**Author:** ![MaSa759](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/masa759/32/1625_2.png) [@MaSa759](https://discourse.laminas.dev/u/MaSa759)\
**Post date:** [September 30, 2024, 5:32am UTC](https://discourse.laminas.dev/t/skip-optional-route-params/3777/3 "2024-09-30T05:32:19Z")

</div>

Redirects have the same issue. It is more a routing-problem, I guess.

---

<div class="post-metadata">

**Author:** ![MaSa759](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/masa759/32/1625_2.png) [@MaSa759](https://discourse.laminas.dev/u/MaSa759)\
**Post date:** [September 30, 2024, 5:32am UTC](https://discourse.laminas.dev/t/skip-optional-route-params/3777/4 "2024-09-30T05:32:26Z")

</div>

Customizing the routers assemble()-method would be my idea, but I don’t see how the router could be overwritten. Is there a way?

I found the same issue was mentioned before:

- [Optional parameters without default value and Child Routes · Issue #5224 · zendframework/zendframework · GitHub](https://github.com/zendframework/zendframework/issues/5224)
- [Segment route always renders optional params if route has childs · Issue #6697 · zendframework/zendframework · GitHub](https://github.com/zendframework/zendframework/issues/6697)

But the provided solution doesen’t work anymore. Is there really no out-of-the-box-solution for that? As it is a pretty common SEO-requirement.

Omitting the url-segment, if it matches the default-value would be in my opinion the expected behaviour. Because currently, it is not really optional - it is required (at least for the assambly).

The reason given for that behaviour ([Segment route always renders optional params if route has childs · Issue #6697 · zendframework/zendframework · GitHub](https://github.com/zendframework/zendframework/issues/6697)) is maybe better solved by adding _constraints_ or redesigning the url concept.

---

<div class="post-metadata">

**Author:** ![MaSa759](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/masa759/32/1625_2.png) [@MaSa759](https://discourse.laminas.dev/u/MaSa759)\
**Post date:** [October 1, 2024, 12:39pm UTC](https://discourse.laminas.dev/t/skip-optional-route-params/3777/5 "2024-10-01T12:39:24Z")

</div>

You convinced me, overriding \Laminas\View\Helper\Url and \Laminas\Mvc\Controller\Plugin\Url seems to be the way to go. Thanks!

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [October 1, 2024, 9:30pm UTC](https://discourse.laminas.dev/t/skip-optional-route-params/3777/6 "2024-10-01T21:30:25Z")

</div>

Please check the following library:

> **[GitHub - basz/SlmLocale: Auto detection of locale through different...](https://github.com/basz/SlmLocale)**
>
> Auto detection of locale through different strategies for Zend Framework 2

---

<div class="post-metadata">

**Author:** ![smozgur](https://avatars.discourse-cdn.com/v4/letter/s/ecc23a/32.png) [@smozgur](https://discourse.laminas.dev/u/smozgur)\
**Post date:** [October 2, 2024, 10:49pm UTC](https://discourse.laminas.dev/t/skip-optional-route-params/3777/7 "2024-10-02T22:49:14Z")

</div>

It works as you need if you use optional constraint for lang.  
The following is adapted from my route config that I use for multilingual web applications.

```auto
'routes' => [
    'home' => [
        'type' => Segment::class,
        'options' => [
            'route' => '[/:lang]/',
            'constraints' => [
                'lang' => '[de|en|fr]*'
            ],
            'defaults' => [
                'lang' => 'de',
                'controller' => Controller\IndexController::class,
                'action' => 'index',
            ],
        ],
        'may_terminate' => true,
        'child_routes' => [
            'test' => [
                'type' => Literal::class,
                'options' => [
                    'route' => 'test/',
                    'defaults' => [
                        'controller' => Controller\IndexController::class,
                        'action' => 'test',
                    ],
                ],
            ],
            'test-segment' => [
                'type' => Segment::class,
                'options' => [
                    'route' => ':action/[:id/]',
                    'constraints' => [
                        'id' => '[0-9]+',
                    ],
                    'defaults' => [
                        'controller' => Controller\IndexController::class,
                        'action' => 'test-segment',
                    ],
                ],
            ],
        ]
    ],
],

```

And controller actions:

```auto
    public function indexAction()
    {
        $lang = $this->params()->fromRoute('lang');
        print $lang;
        exit;
    }

    public function testAction()
    {
        $lang = $this->params()->fromRoute('lang');
        print $lang;
        exit;
    }

    public function testSegmentAction()
    {
        $lang = $this->params()->fromRoute('lang');
        $id = $this->params()->fromRoute('id', 0);
        print $lang;
        print '<br>';
        print $id;
        exit;
    }

```
