CakePHP-重定向到控制器,不执行任何操作,也不执行段塞


CakePHP - Redirect to controller without action and without slug

使用CakePHP,我想从以下URL重定向:

http://www.example.com/songs/12345/Michael-Jackson-Billie-Jean

到此URL:

http://www.example.com/songs/12345

在新的URL中,控制器是songs,id是12345,隐含的操作是view,并且段塞(Michael-Jackson-Billie-Jean(被移除。

有没有可能使用routes配置文件来编程这个重定向,或者我需要在控制器或路由类中编程一个稍微高级一点的重定向?

我尝试了以下两种可能性,但都没有成功。在第一次尝试中,我最终得到了一个URL,URL中有view,URL末尾附加了参数:

Router::redirect(
    '/songs/:id/**',
    array('controller' => 'songs', 'action' => 'view'),
    array('persist' => array('id'))
);
// Redirects to http://www.example.com/songs/view/Michael-Jackson-Billie-Jean%2Fid%3A1286072880/id:view

在我的第二次尝试中,重定向几乎起作用,但字符串:id实际上在URL:中得到了响应

Router::redirect(
    '/songs/:id/**',
    '/songs/:id'
);
// Redirects to http://www.example.com/songs/:id

我想我可以把这个重定向写在root.htaccess文件中,但我更愿意把所有路由都放在一个地方;即CakePHP路由文件。

有人知道我如何重定向到http://www.example.com/songs/:id吗?其中:id是歌曲ID,而不是字符串:id?非常感谢。

使用以下路线获得简单解决方案:

Router::connect('/songs/*', array('controller' => 'songs', 'action' => 'view'),array('pass' => array('id')));

function view($id=null){   
         // $id will be song id
         // For example, http://www.example.com/songs/12345 here $id is 12345
}

不要使用.htaccess,它将很容易从routes.php.处理

干杯