Sunday, February 9, 2014

Re: CakePHP 2 Custom Route Class

Thanks!! Worked a treat :)

On Saturday, February 8, 2014 4:04:47 AM UTC+8, José Lorenzo wrote:
The casing of folders and files changed from 1.3 to 2.x

Please read the migration guide for a complete list of differences http://book.cakephp.org/2.0/en/appendices/2-0-migration-guide.html#file-and-folder-naming. You may also want to execute the automated upgrade shell that helps you with some of the migration tasks.

On Friday, February 7, 2014 3:51:53 PM UTC+1, Patrick Templeman Twells wrote:
Hi, in Cake 1.3 I had a custom route class set up which looked as follows:

/app/libs/routes/slug_route.php


<?php

    class SlugRoute extends CakeRoute
    {
        function parse($url)
        {
            $params = parent::parse($url);
            if (empty($params))
            {
                return false;
            }
           
            $slugs = Cache::read('dp_slugs');
            if (empty($slugs) || !array_key_exists($params['slug'], $slugs))
            {
                App::import('Model', 'DynamicPage');
                $DynamicPage = new DynamicPage();
                $pages = $DynamicPage->find('all', array(
                    'fields' => array('DynamicPage.url_slug'),
                    'recursive' => -1
                ));
                $slugs = array_flip(Set::extract('/DynamicPage/url_slug', $pages));
                Cache::write('dp_slugs', $slugs);
            }
           
            if (isset($slugs[$params['slug']]))
            {
                return $params;
            }
           
            return false;
        }

    }


?>

I've just set up a Cake 2 website and this just tells me: 

Route class not found, or route class is not a subclass of CakeRoute

Error: An Internal Error Has Occurred.

 I'm trying to find an example of a custom route for url_slugs that works but I can't get it working. Any ideas what I need to update to make it work? Based on what I've been able to find, I've updated it so that now the class is in /app/Lib/Routing/Route/slug_route.php and looks like this:

<?php
/**
 * Deal with URL Slugs
 */
App::uses('DynamicPage', 'Model');
App::uses('CakeRoute', 'Routing/Route');

class CategorySlugRoute extends CakeRoute {

    /**
     * Parse the URL
     * @param string $url
     * @return boolean
     */
    function parse($url) {
        $params = parent::parse($url);
        if (empty($params)) {
            return false;
        }

        // See if slugs are cached
        $slugs = Cache::read('dp_slugs');
        if (!$slugs) {
            // Get all slugs
            $DynamicPage = new DynamicPage();
            $slugs = $DynamicPage->find('list', array(
                'fields' => array('DynamicPage.url_slug'),
                'recursive' => -1
            ));

            Cache::write('dp_slugs', $slugs);
        }

        // Reverse slugs for easy comparison
        $slugs = array_flip($slugs);

        // See if dynamic pages have been passed
        if (!empty($params['pass'])) {
            $params['slug'] .= '/' . implode('/', $params['pass']);
        }

        // Match passed slug with Category slugs
        if (isset($slugs[$params['slug']])) {
            return $params;
        }

        return FALSE;
    }
}

in /app/config/routes.php I have this at the end of the file:

App::uses('SlugRoute', 'Routing/Route');
    Router::connect('/:slug', array('controller' => 'dynamic_pages', 'action' => 'view'), array('routeClass' => 'SlugRoute'));

But I still have the error. Any ideas would be greatly appreciated

Thanks!

Patrick

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.

No comments: