how to remove action name from URL in cakephp2

前端 未结 5 1218
终归单人心
终归单人心 2021-01-17 04:44

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

5条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-17 05:16

    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

    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');
                } 
        }
    }
    ?>
    

提交回复
热议问题