I have a signature method: public function mostPopular()
I'm trying to access it via URL
as follows: products/most-popular
however I'm being directed to an Error page:
The most popular action is not defined in ProductsController
However, following the CakePHP 3.0 naming conventions, this error should not happen.
Route file (routes.php)
<?php
use Cake\Core\Plugin;
use Cake\Routing\Router;
/**
* The default class to use for all routes
*
* The following route classes are supplied with CakePHP and are appropriate
* to set as the default:
*
* - Route
* - InflectedRoute
* - DashedRoute
*
* If no call is made to 'Router::defaultRouteClass', the class used is
* 'Route' ('Cake\Routing\Route\Route')
*
* Note that 'Route' does not do any inflections on URLs which will result in
* inconsistently cased URLs when used with ':plugin', ':controller' and
* ':action' markers.
*
*/
Router::defaultRouteClass('Route');
Router::scope('/', function ($routes) {
/**
* Here, we are connecting '/' (base path) to a controller called 'Pages',
* its action called 'display', and we pass a param to select the view file
* to use (in this case, src/Template/Pages/home.ctp)...
*/
$routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
// tendo pages após o dominio (sequido de qualquer coisa) será redirecionado para o controller pages,
// método display, qualquer coisa após o * será passado como parametro
/**
* ...and connect the rest of 'Pages' controller's URLs.
*/
$routes->connect('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
/**
* Connect catchall routes for all controllers.
*
* Using the argument 'InflectedRoute', the 'fallbacks' method is a shortcut for
* '$routes->connect('/:controller', ['action' => 'index'], ['routeClass' => 'InflectedRoute']);'
* '$routes->connect('/:controller/:action/*', [], ['routeClass' => 'InflectedRoute']);'
*
* Any route class can be used with this method, such as:
* - DashedRoute
* - InflectedRoute
* - Route
* - Or your own route class
*
* You can remove these routes once you've connected the
* routes you want in your application.
*/
$routes->fallbacks('InflectedRoute');
});
/**
* Load all plugin routes. See the Plugin documentation on
* how to customize the loading of plugin routes.
*/
Plugin::routes();
MostPopular () method code:
public function mostPopular()
{
$this->layout = false;
if($this->request->is('post'))
{
$productsQuantity = 4;
$order = 'DESC';
$column = 'visited';
$subCategoryId = $this->request->data['subCategory'];
$productsMostPopular = $this->Search->listProductsByTrend($subCategoryId, $productsQuantity, $column, $order);
//$this->set('productsMostPopular', $productsMostPopular);
//$this->set('_serialize',array('productsMostPopular'));
echo json_encode($subCategoryId);
}
}
NOTE: I'm using CakePHP 3.0