How to remove action name from url in cakephp?

后端 未结 2 462
南笙
南笙 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.

    0 讨论(0)
  • 2020-12-12 03:13

    I think you should first make sure that mod-rewrite module is enabled. You shouldn't have had to remove index.php from the url using .htaccess if mod_rewrite were enabled. Check how to enable it in the manual of your webserver and the default .htaccess of cakephp should be able to handle the rest of the routing for you.

    After you have enable rewrite module, you can modify the routes as pointed out by @Ross in the previous answer in you 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 讨论(0)
提交回复
热议问题