1
votes

i want do a rss, I followed this http://book.cakephp.org/3.0/en/views/helpers/rss.html. But the things not working correctly, because when access the router of the rss, it returns a controller error, saying that controller does not exists. My route is this:

/posts/index.rss

When do this request, it return a error of the controller not found.

The action index.rss is not defined in PostsController

I declared that "app" to accepts rss..My complete config/routes.php

use Cake\Core\Plugin;
use Cake\Routing\Router;
Router::defaultRouteClass('Route');

Router::scope('/', function ($routes) {

    Router::extensions(['json', 'xml', 'rss']);
    $routes->connect('/', ['controller' => 'Fronts', 'action' => 'index']);
    $routes->connect('/contact', ['controller' => 'Fronts', 'action' => 'contact']);
    $routes->connect(
        '/:controller/:action/:id-:slug', 
        [], 
        [
            'pass' => ['id', 'slug'], 
            'id' => '[0-9]+',
            'routeClass' => 'DashedRoute'
        ]
    );

    $routes->fallbacks('InflectedRoute');
});

Plugin::routes();

I also made the LoadComponent in :: initialize () of the controller

public function initialize()
{
    parent::initialize();
    $this->loadComponent('RequestHandler');
}

And my controller

class PostsController extends AppController
{
    ...
   public function index()
   {
    ...
        if($this->RequestHandler->isRss()) :
            $_rss = $this->Posts->find()->limit(20);
            $this->set(compact('_rss'));
           return;  
        endif;
    ...
   }
}

Whats is wrong?

Thankss..!!!

1
Really /plosts/index.rss? shouldn't it be /post/index.rss? - Oops D'oh
Sorry, I wrote it wrong :) ... is /post/index.rss - Lukas Man
You placed Router::extensions('rss'); in /config/routes.php? Any other error messages in /logs/? Did you create a view file in src/Template/Posts/rss/index.ctp? - Oops D'oh
Yes, put it...Router::extensions(['json', 'xml', 'rss']); - Lukas Man
The view also was created...only gives error not found the controller...in my debugger->Request show this plugin(null) controller Posts action index.rss _ext(null) pass(empty) isAjax(false) _ext should be _ext(xml) or _ext(rss) ? - Lukas Man

1 Answers

2
votes

You're defining the extension the wrong way, Router::extensions() is ment to define global extensions for all routes that are being connected after Router::extensions() has been invoked.

So, inside a scope, the call to Router::extensions() is too late, as it's the Router::scope() method, that, when invoked, reads the global extensions and passes them into the scope.

Either invoke Router::extensions() outside of the scope

Router::extensions(['json', 'xml', 'rss']);

Router::scope('/', function ($routes) {
    // ...
});

or use RouterBuilder::extensions() inside of the scope (note that this overrides the global extensions that the scope might have been inherited)

Router::scope('/', function (\Cake\Routing\RouteBuilder $routes) {
    $routes->extensions(['json', 'xml', 'rss']);
    // ...
});

See also Cookbook > Routing > Routing File Extensions