want to remove action name from url CakePHP

风格不统一 提交于 2019-11-29 17:33:24

When connecting routes using Route elements you may want to have routed elements be passed arguments instead. By using the 3rd argument of Router::connect() you can define which route elements should also be made available as passed arguments:

// SomeController.php

public function messages($phoneNumber = null) {
    // some code here...
}

// routes.php
Router::connect(
    '/messages/:id', // E.g. /messages/number
    array('controller' => 'messages', 'action' => 'messages'),
    array(
        // order matters since this will simply map ":id" 
        'id' => '[0-9]+'
    )
);

and you can also refer link above given by me, hope it will work for you.

let me know if i can help you more.

REST Routing

The example in the question looks similar to REST routing, a built in feature which would map:

GET    /recipes/123    RecipesController::view(123)

To enable rest routing just use Router::mapResources('controllername');

Individual route

If you want only to write a route for the one case in the question it's necessary to use a star route:

Router::connect('/messages/*', 
    array(
        'controller' => 'messages',
        'action' => 'messages'
    )
);

Usage:

echo Router::url(array(
    'controller' => 'messages',
    'action' => 'messages',
    823214
));
// /messages/823214

This has drawbacks because it's not possible with this kind of route to validate what comes after /messages/. To avoid that requires using route parameters.

Router::connect('/messages/:id',
    array(
        'controller' => 'messages',
        'action' => 'messages'
    ),
    array(
        'id' => '\d+',
    )
);

Usage:

echo Router::url(array(
    'controller' => 'messages',
    'action' => 'messages',
    'id' => 823214 // <- different usage
));
// /messages/823214
Farooq Bellard

in config/routes.php

$routes->connect('/NAME-YOU-WANT/:id',
        ['controller' => 'CONTROLLER-NAME','action'=>'ACTIOn-NAME'])->setPass(['id'])->setPatterns(['id' => '[0-9]+']
    );
Andrew K

You can use Cake-PHP's Routing Features. Check out this page.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!