Forum

Thread tagged as: Question, Problem

Help with routing and filtering

Hi Guys,

I am sure I had this working before, but I have either introduced an error or an error has crept in since upgrading Perch. I have a section for stories on the site and I am filtering this through various methods.
Here are the routes I have setup on the main stories page.

stories/[slug:story_slug] stories/sponsor/[slug:sponsor_slug] stories/artist/[slug:artist] stories/[slug:set]/[[slug:cat] stories/[slug:set]/[slug:cat]/[slug:subcat]

Here is my template;

<?php
if (!defined('PERCH_RUNWAY')) include($_SERVER['DOCUMENT_ROOT'].'/cms/runtime.php');

if (perch_get('story_slug')) {
    $result = perch_content_custom('Stories', [
        'template'          => 'stories/story',
        'filter'            => 'story_slug',
        'match'             => 'eq',
        'value'             => perch_get('story_slug'),
        'count'             => 1,
        'skip-template'     => true,
        'return-html'       => true,
    ]);

    PerchSystem::set_var('modified_date', $result[0]['date']);

    $name               = $result[0]['name'];
    $category           = $result[0]['diagnosis'];
    $class              = perch_content_custom('Stories', [
        'template'          => 'stories/_class',
        'filter'            => 'story_slug',
        'match'             => 'eq',
        'value'             => perch_get('story_slug'),
        'count'             => 1,
        'skip-template'     => true,
        'return-html'       => true,
    ]);


    // Get the post title and description to pass into the attributes extend to override the standard META data template.
    $meta_title         = 'Stories :: '.$name;
    $meta_description   = $result[0]['excerpt'];
}

else if (perch_get('cat')) {
    $catPath = perch_get('set') .'/'. perch_get('cat') .'/'. perch_get('subcat');

    $result = perch_category($catPath, [
        'template'          => '_category_title',
        'skip-template'     => true,
        'return-html'       => true,
    ]);

    // Get the post title and description to pass into the attributes extend to override the standard META data template.
    $meta_title         = 'Stories :: '.$result[0]['catTitle'];
    $meta_description   = strip_tags($result[0]['catTitle'].' - '.$result[0]['desc']);
}

else if (perch_get('artist')) {
    $result = perch_collection('Artists', array(
        'template'          => 'stories/_artist_header',
        'filter'=>array(
            array(
                'filter'=> 'artist_slug',
                'match'=> 'eq',
                'value'=> perch_get('artist'),
            ),
            array(
                'filter'=> 'artist_show',
                'match'=> 'eq',
                'value'=> 'show'
            ),
        ),
        'skip-template'     => true,
        'return-html'       => true,
    ));

    // Get the post title and description to pass into the attributes extend to override the standard META data template.
    $meta_title         = 'Stories :: Shoes Painted by '.$result[0]['artist_firstname'];;
    $meta_description   = strip_tags($result[0]['artist_bio'].' '.$result[0]['artist_quote']);
}

else if (perch_get('sponsor_slug')) {
    $result = perch_collection('Sponsors', array(
        'template'          => 'stories/_sponsor_header',
        'filter'            => 'sponsor_slug',
        'match'             => 'eq',
        'value'             => perch_get('sponsor_slug'),
        'skip-template'     => true,
        'return-html'       => true,
    ));

    // Get the post title and description to pass into the attributes extend to override the standard META data template.
    $meta_title         = 'Stories :: Shoes Sponsored by '.$result[0]['sponsor_name'];;
    $meta_description   = strip_tags($result[0]['sponsor_description']);

}


//Update the page attributes
perch_page_attributes_extend(array(
    'meta-title'        => $meta_title,
    'meta-description'  => $meta_description,
));


if(perch_get('story_slug') || perch_get('cat') || perch_get('artist') || perch_get('sponsor_slug')) {
//If there are no results then result in a 404 error page.
    if(count($result) < 2) {
        http_response_code(404);
        header('Location:'.PerchSystem::get_var('domain').'/404/');
        exit;
    }
}

perch_layout('global/header');
?>

    <main role="main">
        <?php if (perch_get('story_slug')) {
            echo $result['html'];
        ?>

        <div class="container">
            <div class="social-sharing">
                <h3>Share <?php echo $name; ?>’s Story</h3>
                <?php perch_layout('social/sharing', array(
                    'title'=>$meta_title));
                ?>
            </div>
        </div>
    </main>


    <section class="related-story // box--wavey // box--<?php echo $class['html']; ?>">
        <?php
            $result = perch_category($category[0], [
                'template'      => 'category_related_stories',
            ]);
        ?>

        <div class="container // related-story__stories">
        <?php
            perch_content_custom('Stories', array(
                'template'      => 'stories/_related',
                'category'      => $category[0],
                'sort'          => 'date',
                'sort-order'    => RAND
            ));
        ?>
        </div>
    </section>


    <?php
        } else if (perch_get('cat')) {
        $catPath = perch_get('set') .'/'. perch_get('cat') .'/'. perch_get('subcat');
    ?>

    <main role="main">
        <?php echo $result['html']; ?>

        <div class="container // teaser-page">
            <?php
                perch_content_custom('Stories', array(
                    'template'      => 'stories/_teaser',
                    'category'      => $catPath,
                ));
            ?>
        </div>
    </main>

    <?php
        } else if (perch_get('artist')) {
    ?>

    <main role="main">
        <?php echo $result['html']; ?>

        <div class="container // teaser-page">
        <?php
            perch_content_custom('Stories', array(
                'template'      => 'stories/_teaser',
                'filter'        => 'artist.artist_slug',
                'match'         => 'eq',
                'value'         => perch_get('artist'),
            ));
        ?>
        </div>
    </main>

    <?php
        } else if (perch_get('sponsor_slug')) {
    ?>

    <main role="main">
        <?php echo $result['html']; ?>

        <div class="container // teaser-page">
        <?php
            perch_content_custom('Stories', array(
                'template'      => 'stories/_teaser',
                'filter'        => 'sponsor.sponsor_slug',
                'match'         => 'eq',
                'value'         => perch_get('sponsor_slug'),
            ));
        ?>
        </div>
    </main>

    <?php } else { ?>

    <main role="main">
        <div class="box--wavey // box--base">
            <div class="container">
                <h1 class="box__title"><?php perch_pages_title(); ?></h1>
            </div>
        </div>

        <?php
            $page = intval($_GET["page"]);
            if ($page < 2) { $count = 8; } else { $count = 9; }

            perch_content_custom('Stories', [
                'template'              => 'stories/_combined',
                'sort'                  => 'date',
                'sort-order'            => 'DESC',
                'count'                 => $count,
                'paginate'              => true,
                'page-links'            => true,
                'page-link-style'       => all
            ]);
        ?>
    </main>

    <?php } ?>

<?php
    perch_layout('global/footer');
?>

However, now only the slug story and the categories form the diagnois (structured like this; stories/diagnosis/leukaemia/acute-lymphoblastic-leukaemia/) are working.

I just get 404 errors for Artist, Age Groups and Sponsors.

What have I got wrong? or is there a bug?

I also have this set in my runway.php file;

        'routing_tokens' => [
            'newsslug'          => '[1-2][0-9]{3}\-[0-9]{2}\-[0-3][0-9]-[a-z0-9\-]+',
            'story_slug'        => '[1-2][0-9]{3}\-[0-9]{2}\-[0-3][0-9]-[a-z0-9\-]+',
        ],

Although I am not sure when/how I should use routing tokens?

Thanks, Terry

Terry Upton

Terry Upton 0 points

  • 5 years ago

Think the issue might have been introduced when I added a new page in the CMS at /diagnosis and then added a routing to this as follows:

[slug:set]/[slug:cat]/[slug:subcat]

If I remove that, all my previous links work, but I need this to show the diagnosis category details pages as these contain information about the illness/diagnosis and when I use it under the stories/section it filters just those stories.

Any ideas?

Thanks, Terry

Drew McLellan

Drew McLellan 2638 points
Perch Support

Have you re-ordered the routes in the Routes section? Make sure the ones with the hard-coded segments like stories and artists are higher up so that they match before the more generic options.

Thanks Drew. That's got it. How come I've never noticed this section before. Doh. Good catch.

Cheers.