how to remove action name from URL in cakephp2

前端 未结 5 1234
终归单人心
终归单人心 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:05

    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

提交回复
热议问题