CakePHP 2.0 Router::connect 问题,url 中没有可见的 id

发布于 2024-12-18 04:25:24 字数 917 浏览 3 评论 0原文

我想要以下 SEO 网址,例如:

www.example.com/users/profile/webfacer

我不想使用唯一用户从数据库中获取。

我尝试在 AppController 中使用 Router 方法 connect 。但我意识到这是不可能的(或者现在不知道在它们也在routes.php中使用它没有帮助),如下所示:

    //in AppController
Router::connect('/users/profile/:name', 
    array(
        'controller' => 'users',
        'action'     => 'profile'
    ) ,
    array(
        'pass' => array('id', 'name'),
        'id'   => '[0-9]+'
    )
);

How can I regenerate this link (below the example) with this html link helper发送 id 但不在 url 中显示它:

$this->Html->link('webfacer',array(
       'controller'=>'users',
       'action'=>'profile',
       'id'=>1,
       'name'=>'webfacer'
));

这将输出 www.example.com/users/profile/username:webfacer 这意味着我的路由器不会出现在我的路由选项中。

有没有人遇到同样的问题并解决了这个问题?

I want following SEO url like:

www.example.com/users/profile/webfacer

I do not want to use the unique user to fetch from database.

I try to use the Router method connect in my AppController. but I realised that it isn't possible (or not knowing it right now to use it in their also used in routes.php does not helped) like this:

    //in AppController
Router::connect('/users/profile/:name', 
    array(
        'controller' => 'users',
        'action'     => 'profile'
    ) ,
    array(
        'pass' => array('id', 'name'),
        'id'   => '[0-9]+'
    )
);

How can I reproduce this link (below the example) with this html link helper to send the id but not show it in the url:

$this->Html->link('webfacer',array(
       'controller'=>'users',
       'action'=>'profile',
       'id'=>1,
       'name'=>'webfacer'
));

This would output www.example.com/users/profile/username:webfacer that mean my router doesn't appear to my route options.

Has anybody had the same issues and solved this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海拔太高太耀眼 2024-12-25 04:25:24

因为您没有将 :id 参数放入路由字符串中,所以当您将其传递到助手中时,Cake 将不知道要做什么,这就是为什么它只是将其作为普通参数附加在网址。无法通过 URL 传递“隐藏”ID,最好的办法是公开它,或者在应用程序的另一端编写一些内容,根据您传递的用户名获取 ID(确保此列已编入索引且 url 安全)。

我会简化你的路线:

//in AppController
Router::connect('/users/profile/:name', 
    array(
        'controller' => 'users',
        'action'     => 'profile'
    ) ,
    array('pass' => array('name'),
    )
);

并且不必费心将 ID 传递给助手。在你的个人资料操作中,你只会有这样的内容:

public function profile($name) {
    $user = $this->User->find('first', array('conditions' => array('name' => $name)));
}

Because you haven't put the :id argument in your route string, Cake won't know what to do when you pass it in the helper, that is why it's just appending it as a normal param in the URL. There is no way to pass a "hidden" id with the URL, you're best bet is to either expose it or at the other end of the app write something that fetches the ID based on the username you pass (make sure this column is indexed and url-safe).

I would just simplify your route to this:

//in AppController
Router::connect('/users/profile/:name', 
    array(
        'controller' => 'users',
        'action'     => 'profile'
    ) ,
    array('pass' => array('name'),
    )
);

And don't bother passing ID to the helper. In your profile action you'd just have something like this:

public function profile($name) {
    $user = $this->User->find('first', array('conditions' => array('name' => $name)));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文