How to remove action name from url in cakephp?

后端 未结 2 487
南笙
南笙 2020-12-12 02:41

I am working on cakephp project. I have removed index.php from URL using .htaccess file and now I want to remove view name from URL & add some other two varying paramet

2条回答
  •  甜味超标
    2020-12-12 03:07

    In your 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 generating links:

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

    Order matters. Change paramX to anything you want i.e. country and town

    note this does not cover: controllername/param1 - both must be present in this example.

    There are other ways to achieve this.

提交回复
热议问题