Assign different domain for two controllers only

狂风中的少年 提交于 2020-01-14 18:57:28

问题


I am using the old Yii (v1), and I need to assign a different domain to two controllers only.

So I have a bunch of controllers - HomeController.php, CategoryController.php, GuestbookController.php, ShopController.php, ProfileController.php, all of them works with a domain aaaaa.com, but I need that controllers ShopController.php and ProfileController.php would work only with domain bbbbb.com.

P.S. When user clicks "My Profile" link or "Shop" the domain changes to bbbbb.com, when user clicks "Home", "Categorys", "Guestbook" the domain changes to aaaaa.com And when user enters URL aaaaa.com it goes to "Home" (HomeController.php) (as it is now) and when he enters bbbbb.com it goes to "Shop" (ShopController.php)

Its all one page with one Navigation bar.


回答1:


So you want different parts of your application to be served from two different domains that are both virtual hosts on the same server pointing to the same application.

You can easily do that with Yii's rewrite rules because you can specify full host when defining them.

This is an example from my application that is used for both the "happyanalytics" tool as for the "happyseotools" blog:

'urlManager'=>array(
    'urlFormat'=>'path',
    'urlSuffix'=>'/',
    'showScriptName'=>false,
    'rules'=>array(

        'http://'._HOST_HAPPYANALYTICS_.'/' => '/analytics/default/index',
        'http://'._HOST_HAPPYANALYTICS_.'/tour/' => '/analytics/default/tour',
        'http://'._HOST_HAPPYANALYTICS_.'/pricing/' => '/analytics/default/pricing',
        'http://'._HOST_HAPPYANALYTICS_.'/support/' => '/analytics/default/support',
        'http://'._HOST_HAPPYANALYTICS_.'/login/' => '/analytics/default/login',
        'http://'._HOST_HAPPYANALYTICS_.'/forgot_password/' => '/analytics/default/forgot_password',
        'http://'._HOST_HAPPYANALYTICS_.'/signup/' => '/analytics/default/register',
        'http://'._HOST_HAPPYANALYTICS_.'/dashboard/' => '/analytics/dashboard/index',
        'http://'._HOST_HAPPYANALYTICS_.'/dashboard/website/<id_website>/<action:(visits|keywords|pages|debug|realtime|configuration|trackingCode)>/' => '/analytics/dashboard/<action>',
        'http://'._HOST_HAPPYANALYTICS_.'/dashboard/website/<id_website>/' => '/analytics/dashboard/website',
        'http://'._HOST_HAPPYANALYTICS_.'/dashboard/<action>/*' => '/analytics/dashboard/<action>',


        'http://'._HOST_HAPPYSEOTOOLS_.'/' => 'site/index',
        'http://'._HOST_HAPPYSEOTOOLS_.'/about/' => 'site/about',
        'http://'._HOST_HAPPYSEOTOOLS_.'/contact/' => 'site/contact',
        'http://'._HOST_HAPPYSEOTOOLS_.'/smile.gif' => 'site/tracker',
        'http://'._HOST_HAPPYSEOTOOLS_.'/subscription/*' => 'site/subscription',


        'http://'._HOST_HAPPYSEOTOOLS_.'/blog/posts/<tag:.*?>/'=>'/blog/post/index',
        'http://'._HOST_HAPPYSEOTOOLS_.'/blog/'=>'/blog/post/index',
        'http://'._HOST_HAPPYSEOTOOLS_.'/blog/<slug>/'=>'/blog/post/view',

    ),
),

As you imagine I have previously defined the domains as constants to not have to repeat them all the time. Also there might be a cleaner solution but this one works :)



来源:https://stackoverflow.com/questions/28276773/assign-different-domain-for-two-controllers-only

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!