# Question about routing

**URL:** <https://forum.getkirby.com/t/question-about-routing/16896>\
**Category:** Questions\
**Tags:** v3\
**Created:** [January 16, 2020, 8:33pm UTC](https://forum.getkirby.com/t/question-about-routing/16896 "2020-01-16T20:33:00Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![portobien](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/portobien/32/7162_2.png) [@portobien](https://forum.getkirby.com/u/portobien)\
**Post date:** [January 16, 2020, 8:33pm UTC](https://forum.getkirby.com/t/question-about-routing/16896/1 "2020-01-16T20:33:00Z")

</div>

Hello,

I am currently building a website for an architectural studio.  
I am doubting about the right way to build the portfolio section.

My idea:

**Projects**  
[example.com/projects](http://example.com/projects)

**Projects category overview**  
[example.com/projects/category](http://example.com/projects/category)

**Project (single)**  
[example.com/project](http://example.com/project)

So then I want/should make a re-route for a single project to ged rid of the /projects/category part of the URL. This is better for SEO because the project is not so deep in the url (less folders). But is this even possible?

Thanks you for your answer!

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [January 16, 2020, 8:37pm UTC](https://forum.getkirby.com/t/question-about-routing/16896/2 "2020-01-16T20:37:15Z")

</div>

How many projects are we talking about? Hundreds? Thousands?

---

<div class="post-metadata">

**Author:** ![portobien](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/portobien/32/7162_2.png) [@portobien](https://forum.getkirby.com/u/portobien)\
**Post date:** [January 16, 2020, 9:23pm UTC](https://forum.getkirby.com/t/question-about-routing/16896/3 "2020-01-16T21:23:52Z")

</div>

I think around 150/200 projects and about 8 categories.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [January 16, 2020, 9:25pm UTC](https://forum.getkirby.com/t/question-about-routing/16896/4 "2020-01-16T21:25:09Z")

</div>

Then you would probably be more flexible without the category pages, and assigning the categories via a select/multiselect field.

But to answer your original question: yes, routing would be possible with the structure above.

---

<div class="post-metadata">

**Author:** ![portobien](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/portobien/32/7162_2.png) [@portobien](https://forum.getkirby.com/u/portobien)\
**Post date:** [January 16, 2020, 9:26pm UTC](https://forum.getkirby.com/t/question-about-routing/16896/5 "2020-01-16T21:26:32Z")

</div>

And then filter with the tags collection method?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [January 16, 2020, 9:26pm UTC](https://forum.getkirby.com/t/question-about-routing/16896/6 "2020-01-16T21:26:54Z")

</div>

Yes, or filter with routes.

> **[Filtering via routes](https://getkirby.com/docs/cookbook/content/filter-via-route)**
>
> Learn how to filter using routes instead of parameters

---

<div class="post-metadata">

**Author:** ![portobien](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/portobien/32/7162_2.png) [@portobien](https://forum.getkirby.com/u/portobien)\
**Post date:** [January 16, 2020, 9:27pm UTC](https://forum.getkirby.com/t/question-about-routing/16896/7 "2020-01-16T21:27:12Z")

</div>

Allright thank you for your help!

---

<div class="post-metadata">

**Author:** ![portobien](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/portobien/32/7162_2.png) [@portobien](https://forum.getkirby.com/u/portobien)\
**Post date:** [January 18, 2020, 6:39pm UTC](https://forum.getkirby.com/t/question-about-routing/16896/8 "2020-01-18T18:39:43Z")

</div>

I am not able to get it working.  
What I want to achieve is:

**Projects**  
[example.com/projecten](http://example.com/projecten)

**Projects category overview**  
[example.com/projecten/category](http://example.com/projecten/category) for example [example.com/projecten/interior](http://example.com/projecten/interior)

**Project (single)**  
[example.com/projecten/name-project](http://example.com/projecten/name-project)

I have:

Controller (same name as page template):  
projects.php

```
<?php

return function ($page, $category) {

$projects = $page->children()->listed();

if ($category) {
    $projects = $projects->filterBy('category', ',');
}

return [
    'projects' => $projects
];

};

```

config.php

```
<?php

return [
    'debug' => true,
    'routes' => [
        [
            'pattern' => 'projecten/(:any)',
            'action' => function ($category) {
                if ($page = page('projecten/' . $category)) {
                    return $page;
                } else {
                    return page('projecten')->render([
                        'category' => $category
                    ]);
                }

            }
        ]
    ]
];

```

and as template for projecten:

```
<?php snippet('header') ?>

    <div id="hero-and-body">
        <section id="page-body">
            <div id="portfolio-grid" class="isotope-grid portfolio-container style-column-3 fitrows clearfix text-light">

                <?php foreach ($page->children()->listed() as $project): ?>

                    <div class="isotope-item portfolio-item">
                        <div class="portfolio-item-inner item-inner">
                            <a href="<?= $project->url() ?>" class="thumb-hover overlay-color scale text-light">
                                <?php if ($image = $project->image()) : ?>
                                    <?= $project->images()->findBy("template", "cover")->crop(900, 506) ?>
                                        <?php endif ?>

                                            <div class="overlay-caption hidden">
                                                <span class="caption-sub portfolio-category"><?= $project->location() ?></span>
                                                <h4 class="caption-name portfolio-name"><strong><?= $project->headline() ?></strong></h4>
                                            </div>
                            </a>
                        </div>
                    </div>

                    <?php endforeach ?>

            </div>

        </section>
    </div>

    <?php snippet('footer') ?>

```

Blueprint:

```
  category:
    label: Categorie
    type: multiselect
    min: 1
    max: 8
    options:
      - Example 1
      - Example 2
      - Example 3
      - Example 4
      - Example 5
      - Example 6
      - Example 7
      - Example 8
```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [January 18, 2020, 6:48pm UTC](https://forum.getkirby.com/t/question-about-routing/16896/9 "2020-01-18T18:48:30Z")

</div>

> [@portobien](#):
>
> $projects = $projects-\>filterBy(‘category’, ‘,’);

That line is not quite complete:

```auto

$projects = $projects->filterBy('category', $category, ',');

```

---

<div class="post-metadata">

**Author:** ![portobien](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/portobien/32/7162_2.png) [@portobien](https://forum.getkirby.com/u/portobien)\
**Post date:** [January 18, 2020, 6:58pm UTC](https://forum.getkirby.com/t/question-about-routing/16896/10 "2020-01-18T18:58:06Z")

</div>

Thanks for your respons!  
I am able to see the page now but still seeing al the projects (from different categories). Do I have the page template as well?

[example.com/projecten](http://example.com/projecten)  
[example.com/projecten/interior](http://example.com/projecten/interior)  
Show the same projects.

Thanks!

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [January 18, 2020, 7:01pm UTC](https://forum.getkirby.com/t/question-about-routing/16896/11 "2020-01-18T19:01:33Z")

</div>

You are not using the `$projects` variable in your template!

> [@portobien](#):
>
> \<?php foreach ($page-\>children()-\>listed() as $project): ?\>

This cannot work as expected.

```auto
<?php foreach ($projects as $project): ?>

```

---

<div class="post-metadata">

**Author:** ![portobien](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/portobien/32/7162_2.png) [@portobien](https://forum.getkirby.com/u/portobien)\
**Post date:** [January 18, 2020, 8:12pm UTC](https://forum.getkirby.com/t/question-about-routing/16896/12 "2020-01-18T20:12:16Z")

</div>

Sorry, I don’t understand what to do.  
If I change $project to $projects it still not works.

Do I have to filter by category? I only do understand how to filter for a specific category, not for all.

`$projects = $projects->filterBy('category', $interior, ',');`

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [January 18, 2020, 8:49pm UTC](https://forum.getkirby.com/t/question-about-routing/16896/13 "2020-01-18T20:49:44Z")

</div>

What is `$interior`, and could you post your revised files again together with your folder structure.

---

<div class="post-metadata">

**Author:** ![portobien](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/portobien/32/7162_2.png) [@portobien](https://forum.getkirby.com/u/portobien)\
**Post date:** [January 19, 2020, 3:56pm UTC](https://forum.getkirby.com/t/question-about-routing/16896/14 "2020-01-19T15:56:34Z")

</div>

$interior is one of the categories I use in my blueprint dropdown (for categories). Given your response, I suspect that this is also incorrect.

This is my page template:

```
<?php snippet('header') ?>
    <div id="hero-and-body">
        <section id="page-body">

            <div id="portfolio-grid" class="isotope-grid portfolio-container style-column-3 fitrows clearfix text-light">

                <?php foreach ($projects as $project): ?>

                    <div class="isotope-item portfolio-item">
                        <div class="portfolio-item-inner item-inner">
                            <a href="<?= $project->url() ?>" class="thumb-hover overlay-color scale text-light">
                                <?php if ($image = $project->image()) : ?>
                                    <?= $project->images()->findBy("template", "cover")->crop(900, 506) ?>
                                        <?php endif ?>

                                            <div class="overlay-caption hidden">
                                                <span class="caption-sub portfolio-category"><?= $project->location() ?></span>
                                                <h4 class="caption-name portfolio-name"><strong><?= $project->headline() ?></strong></h4>
                                            </div>
                            </a>
                        </div>
                    </div>

                    <?php endforeach ?>

            </div>

        </section>
    </div>

    <?php snippet('footer') ?>

```

Folder structure:

Site

- accounts (not relevant children)
- blueprints (not relevant children)
- cache (not relevant children)
- config
  - config.php

- controllers
  - project.php
  - projects.php
  - search.php

- plugins (not relevant children)
- sessions (not relevant children)
- snippets
  - header
  - footer

- templates
  - default.php
  - home.php
  - projects.php
  - project.php
  - search.php
  - error.php

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [January 19, 2020, 4:31pm UTC](https://forum.getkirby.com/t/question-about-routing/16896/15 "2020-01-19T16:31:41Z")

</div>

Oh, sorry for not clearly expressing myself, when I say site structure I mean the structure in your content folder…

Oh, and I also need the controller…

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [January 19, 2020, 5:06pm UTC](https://forum.getkirby.com/t/question-about-routing/16896/16 "2020-01-19T17:06:38Z")

</div>

Not related to the original problem, but this piece of code in your template doesn’t make sense:

```auto
<?php if ($image = $project->image()) : ?>
  <?= $project->images()->findBy("template", "cover")->crop(900, 506) ?>
<?php endif ?>

```

What’s the point of the if statement here? It has no relation with the code inside the if-statement. What it should probably be:

```auto
<?php if ($image = $project->images()->findBy('template', 'cover')) : ?>
  <?= $image->crop(900, 506) ?>
<?php endif ?>

```

---

<div class="post-metadata">

**Author:** ![portobien](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/portobien/32/7162_2.png) [@portobien](https://forum.getkirby.com/u/portobien)\
**Post date:** [January 19, 2020, 6:24pm UTC](https://forum.getkirby.com/t/question-about-routing/16896/17 "2020-01-19T18:24:32Z")

</div>

Folder Content:

- 1\_projecten
  - 1\_greenhouse
    - greenhouse.jpg
    - gereenhouse.jpg.txt
    - project.txt

  - 2\_boticelli
  - 3\_lencester

- 2\_nieuws
- 3\_over
- error
- site.txt

Controller

```
   <?php

    return function ($page, $category) {

    $projects = $page->children()->listed();

    if ($category) {
        $projects = $projects->filterBy('category', $category, ',');
    }

    return [
        'projects' => $projects
    ];

};
```

---

<div class="post-metadata">

**Author:** ![portobien](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/portobien/32/7162_2.png) [@portobien](https://forum.getkirby.com/u/portobien)\
**Post date:** [January 19, 2020, 6:25pm UTC](https://forum.getkirby.com/t/question-about-routing/16896/18 "2020-01-19T18:25:08Z")

</div>

You are totally right. Thanks!  
I think it’s because of my limited PHP skills. Sorry!

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [January 19, 2020, 6:36pm UTC](https://forum.getkirby.com/t/question-about-routing/16896/19 "2020-01-19T18:36:10Z")

</div>

Ok, but anyway, if you put together the template, the controller and the route, the code should work as expected.

If it still doesn’t, please send me a download link to the project via PM and I can have a look. That will be faster than trying to find the issue here.

---

<div class="post-metadata">

**Author:** ![portobien](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/portobien/32/7162_2.png) [@portobien](https://forum.getkirby.com/u/portobien)\
**Post date:** [January 19, 2020, 6:58pm UTC](https://forum.getkirby.com/t/question-about-routing/16896/20 "2020-01-19T18:58:55Z")

</div>

I think you missed the close parenthesis ‘)’  
-\>findBy(‘template’, ‘cover’)) : ?\>

```
          <?php if ($image = $project->images()->findBy('template', 'cover')) : ?>
          <?= $image->crop(900, 506) ?>
          <?php endif ?>
```

[Next page](https://forum.getkirby.com/t/question-about-routing/16896.md?page=2)
