cakephp路由没有id? [英] cakephp routing without id?

查看:139
本文介绍了cakephp路由没有id?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在蛋糕中路由网址而不在网址中输入ID?

Is there any way to route urls in cake without having the ID in the url?

因此,不要使用www.mydomain.com/id/article-name I只是想要www.mydomain.com/article-name

So instead of www.mydomain.com/id/article-name I just want www.mydomain.com/article-name

我一直在关注。
http://book.cakephp.org/view/543 /传递参数到动作

推荐答案

当然。唯一的要求是,在URL中有足够的唯一信息来固定您想要的文章。如果 / article-name 在您的数据库中是唯一的,您可以使用它来查找您想要的特定记录。

Sure. The only requirement for this is that there's enough unique information in the URL to pin down the article you want. If /article-name is unique in your database, you can use it to find the specific record you want.

在config / routes.php:

In config/routes.php:

// ... configure all normal routes first ...

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

在controllers / articles_controller.php:

In controllers/articles_controller.php:

function view ($article_name) {
    $article = $this->Article->find('first', array(
        'conditions' => array('Article.name' => $article_name)
    ));
    ...
}

小心不要为产品命名它可以合法地出现在URL中,所以你不会遇到冲突。网址 http://example.com/pages 是指向产品的网页还是指向 array('controller'=>'pages ','action'=>'index')?为此,您还需要在 routes.php 中定义您的路由,以允许首先访问所有控制器,并且只有未定义的休息被管道您的 ArticlesController 。查看 Routes :: connect ,您可以指定可用于此目的的RegEx筛选器。

Be careful not to name your products like anything that could legitimately appear in the URL, so you don't run into conflicts. Does the URL http://example.com/pages point to the product 'pages' or to array('controller' => 'pages', 'action' => 'index')? For this purpose you'll also need to define your routes in routes.php in a way that allows all your controllers to be accessible first, and only the undefined rest gets piped into your ArticlesController. Look at the third parameter of Routes::connect, which allows you to specify a RegEx filter you could use for this purpose.

这篇关于cakephp路由没有id?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆