rsanchez / resource_router

Resource Router for ExpressionEngine
MIT License
48 stars 12 forks source link

Routing based on channel short name #30

Closed akukral closed 8 years ago

akukral commented 8 years ago

Is it possible and if so how would I route based on the channel short name of a wild card using the function? Basically using Resource Router to handle a landing page like this code:

{if segment_1 != ''}

    {exp:channel:entries
        url_title="{last_segment}"
        disable="member_data|categories|pagination"
        dynamic="no"
    }

        {if channel_short_name == "single_pages"}
            {embed="site/_single_page"}
        {/if}

        {if channel_short_name == "microsites"}
            {embed="site/_microsite"}
        {/if}

        {if channel_short_name == "microsite_sub_pages"}
            {embed="site/_microsite_sub_page"}
        {/if}

    {/exp:channel:entries}

{/if}
akukral commented 8 years ago

Sorry, I figured it out.

akukral commented 8 years ago

I figured out how to filter the the url titles but how would I use the call back for check the channel_short_name if they are valid urls?

rsanchez commented 8 years ago

This should do it:

$config['resource_router'] = array(
    ':any' => function($router, $wildcard) {
        // use real channel ids here
        $single_pages_channel_id = 1;
        $microsites_channel_id = 2;
        $microsite_sub_pages_channel_id = 3;

        $channels = array(
            $single_pages_channel_id,
            $microsites_channel_id,
            $microsite_sub_pages_channel_id,
        );

        if ($wildcard->isValidUrlTitle(array('channel_id' => $channels))) {
            if ($wildcard->getMeta('channel_id') == $single_pages_channel_id) {
                $router->setTemplate('site/_single_page');
            } elseif ($wildcard->getMeta('channel_id') == $microsites_channel_id) {
                $router->setTemplate('site/_microsite');
            } elseif ($wildcard->getMeta('channel_id') == $microsite_sub_pages_channel_id) {
                $router->setTemplate('site/_microsite_sub_page');
            }
        } else {
            $router->set404();
        }
    },
);
akukral commented 8 years ago

Wow, that worked great! Thank you for your help! It is greatly appreciated.