CakePHP(2.0)动态URL [英] CakePHP (2.0) Dynamic URLs

查看:150
本文介绍了CakePHP(2.0)动态URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究CakePHP 2.0,并希望将旧1.3项目转换为2.0。我将从头开始,因为在项目中有很多代码可以更好。

I'm currently looking into CakePHP 2.0 and wanting to convert old 1.3 projects to 2.0. I'm going to start from scratch because there's a whole lot of code in the projects that could be a lot better.

其中一个是动态URL,项目多语言,甚至URL变成所选择的语言。例如:

One of those things is the dynamic URLs, the projects multilingual and even the URLs change to the chosen language. Eg:

英语: / pages / new-article
荷兰语: / paginas / nieuw-artikel

这两个都会转到 PagesController :: display();

注意:网址可以更长,网页可以有子页面,那些也将添加到URL。例如: / pages / new-article / article-subpage

Note: the URLs can be way longer, pages can have subpages and those will be added to the URL too. Eg: /pages/new-article/article-subpage

现在,有一条路线,一切都去具体的行动。像*去到 PagesController :: index();

Now, the way I did it before is to have a route for everything going to a specific action. Like * going to PagesController::index();

但是这似乎减慢了应用程序,很多问题。

However this seems to slow the apps down and it brings a lot of problems along with it.

所以我的问题是,有更简单的方法吗?

So my question to you is, is there a simpler way to do this?

我不想硬编码任何东西,我应该能够将 / pages / article 更改为 / page / article ,而不需要更改代码。

I do not want to hardcode anything, I should be able to change /pages/article to /page/article without needing to change the code.

注意:如果你知道一个方法,在1.2或1.3,这也将是伟大的,2.0不是这个不同。

Note: If you know a way to do it in 1.2 or 1.3, that would also be great, 2.0 isn't that different.

推荐答案

很明显,CakePHP 1.3和2.0允许你创建自定义路由类。该文档位于以下文档中: http:/ /book.cakephp.org/2.0/en/development/routing.html?highlight=route#custom-route-classes

Well i figured it out, apparently CakePHP 1.3 and 2.0 allow you to create custom route classes. It's in the documentation here: http://book.cakephp.org/2.0/en/development/routing.html?highlight=route#custom-route-classes

所以基本上你需要要做的是用以下内容创建一个文件 APP / Lib / Routing / Route / UrlRoute.php

So basically what you need to do is create a file APP/Lib/Routing/Route/UrlRoute.php with the following contents:

class UrlRoute extends CakeRoute{

    public function parse($url){
        $params = parent::parse($url);

        # Here you get the controller and action from a database.

        // tmp
        $params['controller'] = 'pages';
        $params['action'] = 'index';

        return $params;
    }
}

Config / routes.php 您输入以下内容:

And in your APP/Config/routes.php you put the following:

App::import('Lib', 'Routing/Route/UrlRoute');
Router::connect('/*', array('controller' => 'tests', 'action' => 'index'), array('routeClass' => 'UrlRoute'));

我认为真正的挑战是获得通常传递给函数的参数。 func_get_args()现在返回域名后面的所有内容。并从数据库中检索URL,如果你使用额外的参数。可能需要缓存每个网址。

I think the real challenge is getting the arguments that usually get passed to the functions back to work. func_get_args() now returns everything behind the domain name. And retrieving the URL from the database if you're using extra params. Might have to cache each URL.

这篇关于CakePHP(2.0)动态URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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