rsanchez / resource_router

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

Simple Routes not working #14

Closed siebird closed 10 years ago

siebird commented 10 years ago

Hey Rob,

Having an issue getting simple routes to work properly. All routes work until RR gets to the warehouse-specials route, this route and the :any route are getting set with the site/index or 404 template. Do you have any ideas on why this happening? In global template prefs I have the native TR turned off, strict urls off, 404 page set to correct template.

Screen shot of entries in pages channel:

http://d.pr/i/1acav

RR configs:

$config['resource_router'] = array(
    'videos/:any' => 'site/video',
    'videos' => 'site/videos',
    'products/index-by-model' => 'site/products_index',
    'products/:category_url_title' => 'site/products_category',
    'products/:category_url_title/:category_url_title' => 'site/products_category',
    'products/:category_url_title/:category_url_title/:category_url_title' => 'site/products_category',
    'products/:category_url_title/:any' => 'site/product',
    'products/:category_url_title/:category_url_title/:any' => 'site/product',
    'products/:category_url_title/:category_url_title/:category_url_title/:any' => 'site/product',
    'products' => 'site/products_home',
    'warehouse-specials' => 'site/in-stock', // in pages channel, but has unique template
    ':any' => function($router, $wildcard) {
        $where = array(
            'status' => 'open',
            'channel' => 'pages'
        );

        // is the current page from the pages channel?
        if ($wildcard->isValidEntryId($where)) {
            $router->setTemplate('site/page');
        // if not, set a 404 as all page above are covered & routed properly
        } else {
            $router->set404();
        }
    },
);
siebird commented 10 years ago

I'm curious too, to know if the products routing could be done more efficiently with maybe a callback?

siebird commented 10 years ago

Got it working–the pages module was turned on. Turned it off and now they work.

rsanchez commented 10 years ago

Pages module / Structure URLs take precedence over Resource Router. This is by design. I'll add a note of it in the docs.

rsanchez commented 10 years ago

First of all, I think how you have it set up is perfectly acceptable. But you asked how you could do your products section in one callback:

$config['resource_router'] = array(
    'products/:category_url_title/:all' => function($router) {
        if ( ! $router->wildcard(1)->isValidCategoryUrlTitle()) {
            return $router->set404();
        }

        if ($router->wildcard(2)->isEmpty()) {
            $router->setGlobal('product_cat_url_title', $router->wildcard(1));
            return $router->setTemplate('site/products_category');
        }

        $isValidCat = $router->wildcard(2)->isValidCategoryUrlTitle(array(
            'parent_id' => $router->wildcard(1)->getMeta('cat_id'),
        ));

        if ( ! $isValidCat) {
            $router->setGlobal('product_url_title', $router->wildcard(2));
            return $router->setTemplate('site/product');
        }

        if ( ! $router->wildcard(3)) {
            $router->setGlobal('product_cat_url_title', $router->wildcard(2));
            return $router->setTemplate('site/products_category');
        }

        $isValidCat = $router->wildcard(3)->isValidCategoryUrlTitle(array(
            'parent_id' => $router->wildcard(2)->getMeta('cat_id'),
        ));

        if ( ! $isValidCat) {
            $router->setGlobal('product_url_title', $router->wildcard(3));
            return $router->setTemplate('site/product');
        }

        if ( ! $router->wildcard(4)) {
            $router->setGlobal('product_cat_url_title', $router->wildcard(3));
            return $router->setTemplate('site/products_category');
        }

        $router->setGlobal('product_url_title', $router->wildcard(4));
        return $router->setTemplate('site/product');

    },
);

This example assumes you have the latest version, 1.0.6, installed.

siebird commented 10 years ago

Rob, thanks for the example! This definitely helps for someone that is not addon developer. In your opinion, which way is more efficient: callback or the 6 lines of routes?

rsanchez commented 10 years ago

My advice is to use whatever's going to be most readable to you (or the next dev) 6 months from now. I don't think there's much of a performance difference between the two. The callback does a bit more than the first approach, in that it actually can validate whether the cat in segment 1 is actually the parent of the cat in segment 2, etc.

siebird commented 10 years ago

I appreciate the help and explanations. Thanks man!