Two controllers filtering for one page

Hi
I had two separate controllers wich worked fine. I’m trying now to display them on one page only but only the first is working. The second list display exaclty the same list of the first one. I tried to rename the second $tags as $tagstwo for example but it didn’t worked. So how to merge two controllers into one ?

My controller looks like this

<?php
return function($site, $pages, $page) {
  $projects = page('projects')->children()->visible();
  $tags = $projects->pluck('tags', ',', true);
	if($tag = param('rubrique')) {
	  	$projects = $projects->filterBy('tags', $tag, ',');
	}
  	return compact('projects', 'tags');
};
?>

<?php
return function($site, $pages, $page) {
  $projects = page('projects')->children()->visible();
  $tags = $projects->pluck('artiste', ',', true);
	if($tag = param('index')) {
	  	$projects = $projects->filterBy('artiste', $tag, ',');
	}
  	return compact('projects', 'tags');
};
?>

And in my template

<ul class="tagsrubriques">    
    <?php foreach($tags as $tag): ?>    
    <?php $p = kirby()->request()->params()->rubrique(); ?>
    <li>
        <a <?php e($tag == $p, 'class="active"') ?> href="<?php echo url('rubriques/' . url::paramsToString(['rubrique' => $tag])) ?>">
            <?php echo html($tag) ?>
        </a>
    </li>
    <?php endforeach ?>
</ul>
<h3>l'Index</h3>
<ul class="tagsrubriques">
    <?php foreach($tags as $tag): ?>    
    <?php $p = kirby()->request()->params()->index(); ?>
    <li>
        <a <?php e($tag == $p, 'class="active"') ?> href="<?php echo url('rubriques/' . url::paramsToString(['index' => $tag])) ?>">
            <?php echo html($tag) ?>
        </a>
    </li>
    <?php endforeach ?>
</ul>

You don’t need two controllers to achieve this, you can use a single controller while passing more variables to the template:

<?php

return function($site, $pages, $page) {
  $projects = page('projects')->children()->visible();

  $artiste = $projects->pluck('artiste', ',', true);
  $tags = $projects->pluck('tags', ',', true);

  $index = param('index');
  $rubrique = param('rubrique');

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

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

  return compact('projects', 'artiste', 'tags', 'index', 'rubrique');
};

Then you will have access to the $projects, $artiste, $tags, $index, and $rubrique variables on your template. Change your template accordingly.

1 Like

Thank you wery much, it’s working.
I post here the template solution

<h3>Les rubriques</h3>
    <ul class="tagsrubriques">    
        <?php foreach($tags as $tag): ?>    
        <?php $p = kirby()->request()->params()->rubrique(); ?>
        <li>
            <a <?php e($tag == $p, 'class="active"') ?> href="<?php echo url('rubriques/' . url::paramsToString(['rubrique' => $tag])) ?>">
                <?php echo html($tag) ?>
            </a>
        </li>
        <?php endforeach ?>
    </ul>
    <h3>l'Index</h3>
    <ul class="tagsrubriques">
        <?php foreach($artiste as $art): ?>    
        <?php $p = kirby()->request()->params()->index(); ?>
        <li>
            <a <?php e($art == $p, 'class="active"') ?> href="<?php echo url('rubriques/' . url::paramsToString(['index' => $art])) ?>">
                <?php echo html($art) ?>
            </a>
        </li>
        <?php endforeach ?>
    </ul>
1 Like

Great @Oziris!

Since the params are already beeing assigned to variables and returned from the controller, you can safely remove these two lines and use the variables instead:

  <h3>Les rubriques</h3>
    <ul class="tagsrubriques">    
        <?php foreach($tags as $tag): ?>    
-       <?php $p = kirby()->request()->params()->rubrique(); ?>
        <li>
-           <a <?php e($tag == $p, 'class="active"') ?> href="<?php echo url('rubriques/' . url::paramsToString(['rubrique' => $tag])) ?>">
+           <a <?php e($tag == $rubrique, 'class="active"') ?> href="<?php echo url('rubriques/' . url::paramsToString(['rubrique' => $tag])) ?>">
                <?php echo html($tag) ?>
            </a>
        </li>
        <?php endforeach ?>
    </ul>
    <h3>l'Index</h3>
    <ul class="tagsrubriques">
        <?php foreach($artiste as $art): ?>    
-       <?php $p = kirby()->request()->params()->index(); ?>
        <li>
-           <a <?php e($art == $p, 'class="active"') ?> href="<?php echo url('rubriques/' . url::paramsToString(['index' => $art])) ?>">
+           <a <?php e($art == $index, 'class="active"') ?> href="<?php echo url('rubriques/' . url::paramsToString(['index' => $art])) ?>">
                <?php echo html($art) ?>
            </a>
        </li>
        <?php endforeach ?>
    </ul>
1 Like

Thanks for your help :ok_hand: