Problem with routing pattern

It is Book Design so i had to use urldecode($args) now it works.

I’ll try the language specific stuff an i’ll post the code if it works.

Do you use language codes for all languages?

Yes i’m using language codes for all languages.

You could then either use the (:any) placeholder

'pattern' => '(:any)/work',
'action   => function($langCode)

or be more specific

'pattern' => '(de|en|nl|es)/work',
'action   => function($langCode)

to fetch any of a given language code

The first version is more flexible if users can add languages, I’d go with the second if the set of languages is predefined and cannot be changed.

I’ve tried both solutions but they break my code. I’ve also tried to specify the language via

site()->visit('work', 'en');

but the are no more posts displayed.

The following pattern works for the english version:

'pattern' => 'en/work/category/(:any)',

This one breaks

'pattern' => '(en|de)/work/category/(:any)',

What do you mean with “they break my code”? It just doesn’t work? You get an error message?

When specifying the languages that way arguments returns a new entry which is the language-code.

array(2) { [0]=> string(2) "en" [1]=> string(11) "book-design" }

So i have to modifie my filter.

Right, yes, because all pattern placeholders are returned as arguments. Unsetting the first argument should do the trick.

I have figured out to get the arguments needed and transform it to Book+Design as i did before but

$work = $work->filterBy('type', $args, ',');

Doesn’t find any articles.

Without your complete code, I can’t really tell what’s missing… :thinking:

But it worked ok before the changes?

It works now.

I’ve made an error in my controller which looks like this now:

<?php
    
    # controllers/work.php
    
    return function($kirby, $page, $pages) {

        // fetch the basic set of pages
        $work = $kirby->collection('work');

        // fetch all types
        $types = $work->pluck('type', ',', true);

        // get arguments from router
        $args = $kirby->route()->arguments();

        // check if array has more than one entry
        if (sizeof($args) > 1) {

            // get second part of array as argument
            $args = array_slice($args, 1, 1);
            
            // convert the array to a string
            $args = implode('', $args);

            // remove dash
            $args = str_replace('-', ' ', $args);

            // uppercase the first character of each word
            $args = ucwords($args);

            // use argument to filter pages
            $work = $work->filterBy('type', $args, ',');
        }

        return compact('work', 'types');
    };

and my router:

		'routes' => [
			[
				'pattern' => '(en|de|fr)/work',
				'action' => function($langCode) {
					if($param = param('type')) {
						$param = str_replace('+', '-', strtolower($param));
  						go($language . '/work/category/' . $param);
					} else {
						return site()->visit('work', $langCode);
					}
				}
			],
			[
				'pattern' => '(en|de|fr)/work/category/(:any)',
				'action' => function($langCode) {
					return site()->visit('work', $langCode);
				}
			]
		]

It’s not complete yet but i get a better understanding of how the router works.

Thank so far :slight_smile: