Access method using Dash (-) in Cakephp URL 3

Asked

Viewed 179 times

2

I have a signature method: public function mostPopular() I’m trying to access it via URL as follows: products/most-popular but I am being directed to a Error page:

The action Most-popular is not defined in Productscontroller

but following the naming conventions of Cakephp 3.0 should not happen this error.

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();

Code of the mostpopular method():

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

  • If I’m not mistaken you won’t be able to access the method this way, you will only be able to access it if you are

  • @Andrébaill the problem is that according to the documentation of the way I did it is supported by Cakephp

  • Puts, the Laravel by default comes with this conversion :)

  • @Wallacemaxters by default Cake also has

  • 1

    I guess cakephp 2 wasn’t like this :)

  • @Wallacemaxters Cakephp 3 has changed a lot

  • @Ricardo can share the doc link where he saw?

Show 2 more comments

1 answer

0


I managed to get the CakePHP 3 use the Default behavior of dashed routes by changing the method parameter fallbacks for 'DashedRoute' by default the routes comes with $routes->fallbacks('InflectedRoute');

$routes->fallbacks('DashedRoute');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.