CakePHP 2.0 Router::connect 问题,url 中没有可见的 id
我想要以下 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为您没有将
:id
参数放入路由字符串中,所以当您将其传递到助手中时,Cake 将不知道要做什么,这就是为什么它只是将其作为普通参数附加在网址。无法通过 URL 传递“隐藏”ID,最好的办法是公开它,或者在应用程序的另一端编写一些内容,根据您传递的用户名获取 ID(确保此列已编入索引且 url 安全)。我会简化你的路线:
并且不必费心将 ID 传递给助手。在你的个人资料操作中,你只会有这样的内容:
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:
And don't bother passing ID to the helper. In your profile action you'd just have something like this: