How to remove action name from url in cakephp?

I am working on a cakephp project. I removed index.php from the URL using the .htaccess file, and now I want to remove the view name from the URL and add two more different parameters. Suppose I select a country and a city, then these two parameters should appear in the URL when they are selected.

The problem I am facing is that cakephp accepts

www.example.com/Controllername/viewname 

But my requirement is like this

 www.example.com/Controllername/param1/param2 

If I pass this path, it searches param1 as controller and param2 as view.

Initially, it should be:

 www.example.com/Controllername/ 
+6
source share
2 answers

In APP/routes.php :

 // www.example/com/Controllername Router::connect('/Controllername', array('controller'=>'Controllername', 'action'=>'index')); // www.example.com/Controllername/param1/param2 Router::connect('/Controllername/:param1/:param2', array('controller'=>'Controllername', 'action'=>'index'), array('pass' => array('param1', 'param2'))); 

and your controller:

 // set to null/a value to prevent missing parameter errors public function index($param1=null, $param2=null) { //echo $param1 . ' and ' . $param2; } 

When creating links:

 array('controller'=>'Controllername', 'action'=>'index', 'param1'=>'foo', 'param2'=>'bar'); 

The order of questions. Change paramX to whatever you want, i.e. country and town

Note that this does not apply: controllername/param1 - both must be present in this example.

There are other ways to achieve this.

+2
source

I think you should first ensure that the mod-rewrite module is enabled. You did not need to remove index.php from the url using .htaccess if mod_rewrite was enabled. Check how to enable it in your web server manual and by default .htaccess cakephp should be able to handle the rest of the routing for you.

After you enable the rewrite module, you can change the routes, as @Ross indicated in the previous answer, in APP / routes.php :

 // www.example/com/Controllername Router::connect('/Controllername', array('controller'=>'Controllername', 'action'=>'index')); // www.example.com/Controllername/param1/param2 Router::connect('/Controllername/:param1/:param2', array('controller'=>'Controllername', 'action'=>'index'), array('pass' => array('param1', 'param2'))); 
0
source

All Articles