0

在 CakePHP 中我想做特定的路由规则(主要是出于美观和 SEO 的原因)。

因此,例如,我在代码中所做的事情是这样的:

Router::connect('/c/:uni.html', array('controller' => 'contents', 'action'=>'index'));

现在如果有人打电话

MY_SITE/anyController/anyAction

我不希望 CakePHP 去那里,即使这个 Controller 确实存在(当然还有动作......)

例子:

class AnyController extends AppController {
    var $helper = array('Html');

    /**
     * landingpage
     */
    private function anyAction() {
        //this action must not be called by /anyController/anyAction
                    //but only by my own defined route
    }

}
4

2 回答 2

2

正如@dogmatic69 指出的那样,复制内容的解决方案通常是一个“规范链接”标签。

在所有页面上放置这样的标签,您可以获得两全其美:保留 Cake 的默认路由,但将搜索引擎指向“正确”的 URL。

这是您在视图中插入规范链接标签的方式:

echo $this->Html->meta(
    'canonical',
    'http://example.com',
    array(
        'rel' => 'canonical',
        'type' => null,
        'title' => null,
        'inline' => false
     )
 );

除了http://example.com你可以使用 Cake 的url()函数,它可以将一个动作和一个控制器作为参数,并且总是返回你的自定义路由(这称为反向路由)。

$this->Html->url(array(
    'controller' => 'foo',
    'action' => 'bar'
));

把它们放在一起,这应该给你你想要的:

echo $this->Html->meta(
    'canonical',
    $this->Html->url(array(
        'controller' => $this->request->params['controller'],
        'action' => $this->request->params['action']
    )),
    array(
        'rel' => 'canonical',
        'type' => null,
        'title' => null,
        'inline' => false
     )
 );

这会将当前视图的控制器/操作的第一个(您的首选)路由放入规范链接标记中。

于 2012-12-23T11:56:38.930 回答
1

只需创建一条可以捕获所有内容的路线

// your other routes

Router::connect('/*', array('contoler' => '...);

正如评论中所指出的,这不是一个好主意。虽然它确实有用途。

于 2012-12-22T14:46:20.620 回答