milesj / utility

[Deprecated] A CakePHP plugin for common utility classes.
MIT License
69 stars 24 forks source link

Sitemap not working for me #17

Closed robksawyer closed 10 years ago

robksawyer commented 10 years ago

When I navigate to http://mysite.com/sitemap.xml, I'm getting the following error.

Controller class SitemapController could not be found.

For example: I've added the following to a controller.

public function _generateSitemap() {
        $sitemap = array(
            array(
                'loc' => array('controller' => 'cheeses', 'action' => 'view'),
                'changefreq' => 'hourly',
                'priority' => '1.0'
            )
        );

        $results = $this->Cheese->find('all', array(
                'conditions' => array(
                    'Cheese.active' => 1
                ),
                'fields' => array(
                    'id','name','modified'
                ),
                'recursive' => -1
            ));

        if ( $results ) {
            foreach ($results as $result) {
                $sitemap[] = array(
                    'loc' => array('controller' => 'cheeses', 'action' => 'view', $result['Cheese']['id']),
                    'lastmod' => $result['Cheese']['modified'],
                    'changefreq' => 'hourly',
                    'priority' => '1.0'
                );
            }
        }
        return $sitemap;
    }

And I've added the following to my bootstrap.

CakePlugin::load(array(
    'Utility' => array('bootstrap' => true, 'routes' => true),
...
));

Notes: I am using a custom Authenticate method for Auth and was thinking that this was having some effect on it, but still not sure.

$this->Auth->authenticate = array('Custom');
robksawyer commented 10 years ago

Update: I tried moving SitemapController to app/Controller/ and now I'm getting the following:

Component class FilterResultsComponent could not be found.
robksawyer commented 10 years ago

Got it working, but now I'm confused why it's not loading the routes.php from the Plugin.

Moved the _generateSitemap logic to each model instead of each controller. And had to update the SitemapController to the following:

    /**
     * Loop through active models and generate sitemap data.
     */
    public function index() {
        $models = App::objects('Model');
        $sitemap = array();

        // Fetch sitemap data
        foreach ($models as $model) {

            // Don't load AppModel's, Model's who can't be found
            if ( strpos($model, 'AppModel') !== false ) {
                continue;
            }

            $instance = ClassRegistry::init($model);

            if ( method_exists($instance, '_generateSitemap') ) {
                if ($data = $instance->_generateSitemap()) {
                    $sitemap = array_merge($sitemap, $data);
                }
            }
        }

        // Cleanup sitemap
        if ($sitemap) {
            foreach ($sitemap as &$item) {

                if (is_array($item['loc'])) {
                    if (!isset($item['loc']['plugin'])) {
                        $item['loc']['plugin'] = false;
                    }

                    $item['loc'] = h(Router::url($item['loc'], true));
                }

                if (array_key_exists('lastmod', $item)) {
                    if (!$item['lastmod']) {
                        unset($item['lastmod']);
                    } else {
                        $item['lastmod'] = CakeTime::format(DateTime::W3C, $item['lastmod']);
                    }
                }
            }
        }

        // Render view and don't use specific view engines
        $this->RequestHandler->respondAs($this->request->params['ext']);

        $this->set('sitemap', $sitemap);
    }

Updated the routes to and had to add them to the main app/Config/routes.php (Not loading from Plugin)

Router::connect('/sitemap', array('plugin' => 'utility', 'controller' => 'sitemap', 'action' => 'index', 'ext' => 'xml'));
Router::connect('/sitemap', array('plugin' => 'utility', 'controller' => 'sitemap', 'action' => 'index', 'ext' => 'json'));
milesj commented 10 years ago

Also, what CakePHP version?

milesj commented 10 years ago

Seems something changed in Cake routes, updated with changes.