Can i hide controller and view name cake php 2?

2019-02-28 02:36发布

问题:

I am using cake php and due to some reason i want to hide controller and action name from the url . current url us like

http://192.168.1.31/home/this_is_test

where home is controller name and this_is_test is slug which is dynamic . i want the url like

http://192.168.1.31/this_is_test.

my routes.php is

Router::connect('/', array('controller' => 'home', 'action' => 'index'));
Router::connect('/dashboard', array('controller' => 'dashboard', 'action' => 'index'));
Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
Router::connect('/admin/login', array('controller' => 'users', 'action' => 'login', 'admin' => true));
Router::connect('/contents/*', array('controller' => 'contents', 'action' => 'view'));
Router::connect('/home/*', array('controller' => 'Home', 'action' => 'index'));

I have read a couple of solution after googling . also tried this in routes.php . but no luck

Router::connect(
    '/:query',
   array('controller' => 'Home', 'action' => 'index',1),
    array('query' => '[a-zA-Z]+')
);

anybody have idea about this if it is possible??

回答1:

Your solution

For static text try this:

Router::connect('/this_is_test', array(
      'controller' => 'home', 
      'action' => 'this_is_test OR any_other action name'
));

If it's dynamic

Router::connect('/:id', 
    array('controller' => 'home', 'action' => 'index'),
    array(
        'pass' => array('id'),
        array('id' => '[A-Za-z]')
    )
);

References: Cakephp2.x Route

I hope I knew what you really want to achieve. You can place the Route in the last position. Here is the reference .

Other option would be to use alias for your controller. So you call your controller some thing else and set a new name for your controller then call it in you Route.

If this doesn't work then you would need to write a bespoke Component in order to help you to do that.