简体   繁体   English

CakePhp-在不同的URL上拆分视图和控制器

[英]CakePhp - Splitting Views and Controllers over different URLS

I have 2 sites. 我有2个网站。 www.xxx.com & www.yyy.com www.xxx.comwww.yyy.com

I have a CakePHP solution on www.xxx.com and it works fine. 我在www.xxx.com上有一个CakePHP解决方案,它工作正常。

I want to now make site www.yyy.com use the same controllers and DB as www.xxx.com . 我现在要使网站www.yyy.com使用与www.xxx.com相同的控制器和数据库。 BUT it is to have totally different CTPs and CSS. 但是它具有完全不同的CTP和CSS。 I can of course just copy the whole lot over to www.yyy.com but then I need to maintain 2 sets of code. 我当然可以将全部复制到www.yyy.com但是然后我需要维护2套代码。 I only want to maintain 2 different sets of CTPS and CSS. 我只想维护2套不同的CTPS和CSS。

So the questions are: 所以问题是:

  1. Can you split controllers and views across 2 different URLs? 您可以在2个不同的URL中拆分控制器和视图吗?
  2. Assuming the answer to 1 is yes. 假设答案为1。 Can you then have 2 different sites using the same controllers and DB but showing a totally different view & CSS? 然后,您能否拥有使用相同控制器和数据库但显示完全不同的视图和CSS的2个不同站点?
  3. What should I think about before doing this from your experience? 根据您的经验,在进行此操作之前我应该​​考虑些什么?

Thanks. 谢谢。

Your question is quite similar to a site that needs a desktop and a mobile version. 您的问题与需要台式机和移动版本的网站非常相似。 You could research the solutions used for that. 您可以研究用于此的解决方案。

We built something like this by creating an AppController that sets a variable in the beforeFilter() method (check the request domain in your case), and use this variable in a the render method to choose which layout and view files to use: 我们通过创建一个AppController来构建类似的东西,该AppController在beforeFilter()方法中设置了一个变量(在您的情况下检查请求域),并在render方法中使用此变量来选择要使用的布局和视图文件:

function render($action = null, $layout = null, $file = null) {                
    $view_file = false;
    $layout_file = false;

    // test for availability of layout does not work for null so set to default setting of null.
    if ($layout === null) {
        $layout = $this->layout;
    }

    // if not mobile: show desktop version.
    if (!$this->is_mobile) {
        $view_file = file_exists(VIEWS . $this -> name . DS . 'desktop/' . $file . '.ctp');
        $layout_file = file_exists(LAYOUTS . 'desktop/' . $layout . '.ctp');
    }

    return parent::render($action, ($layout_file ? 'desktop/' : '') . $layout, ($view_file ? 'desktop/' : '') . $file);
}

This way if is_mobile was false the views/layouts in a subdir 'desktop' were used instead of the default ones. 这样,如果is_mobile为false,则使用子目录“ desktop”中的视图/布局而不是默认视图/布局。 You can use the same system to create separate views/layouts for 2 domains. 您可以使用同一系统为2个域创建单独的视图/布局。

We do this by overriding render() instead of using afterFilter() which is sometimes also suggested. 我们通过重写render()而不是使用有时也建议使用的afterFilter()来实现。 I forgot the real reason but know that it was because we couldn't get it to work like we wanted using afterFilter() ;-) 我忘记了真正的原因,但是知道那是因为我们无法像使用afterFilter()那样使它正常工作;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM