may be duplicate but I don't get any proper answer or help
actually I want to do like:
my current URL is : http://mysite.com/MyController/view/page1
but I want something like :http://mysite.com/MyController/page1
means I want to hide action name from URL.
I have used
Router::connect('/:controller/:id',array('action' => 'view'),array('id' => '[0-9]+'));
but its not working for me
below one working fine but
Router::connect('/:controller/*', array('action' => 'view'),array('id' => '[0-9]+'));
it applies for all controller but I wanted to apply for specific controller
use
Router::connect('/MyController/:id', array('controller' => 'MyController','action' => 'view'),array('id' => '[0-9]+'));
You can use Cake's routing to get this to work.
Add the following to your app/Config/routes.php
Router::connect('/Controller/page1', '/Controller/view/page1');
But you will have to add a route for every 'page'.
You can use a wildcard route to match everything starting with /Controller/:
Router::connect('/Controller/*', '/Controller/view/');
Or, without touching routes:
class FooController extends AppController
public function index($stub) {
$data = $this->findByStub($stub);
if (!$data) {
die('page not found');
}
$this->set('data', $data);
}
}
}
Which allows you to have urls such as /foo/page1
(The routine looks for a Foo with a stub field matching 'page1')
This works, but you will loose the benefit of reverse routing which means you can make links like this: $this->Html->link(array('controller'=>'foo', 'action'=>'view', 'page1'); which cake will automagically rewrite to produce: /foo/page1
use this code
Router::connect('/:controller/*', array('action' => 'view'),array('id' => '[0-9]+'));
Try the following code for your controller, Here am using GroupsController as an example
You add this in your app\Config\routes.php
Router::connect('/groups/:slugParam', array('controller' => 'groups', 'action' => 'index'), array('slugParam' => '[a-zA-Z0-9]+'));
This should redirect all requests of the form
http://www.site.com/groups/* to http://www.site.com/groups/index
( * => whatever comes after the controller name )
So now i have to alter my default index function in GroupsController to reflect this change
<?php
App::uses('AppController', 'Controller');
class GroupsController extends AppController {
public function index($id = null) {
//pr($this->request->params); this where all data is intercepted...
if(isset($this->request->params['slugParam']) && !empty($this->request->params['slugParam'])) {
// i have a slug field in groups databsae and hence instead of id am using slug field to identify the post.
$data = $this->Group->findBySlug($this->request->params['slugParam']);
$this->set('group', $data);
$this->render('/groups/view');
}
}
}
?>
use this code
Router::connect('/MyController/:id', array('controller' => 'MyController','action' => 'view'),array('id' => '[0-9]+'));