向Cakephp中的所有网址添加子域 [英] Add a subdomain to all urls in Cakephp

查看:94
本文介绍了向Cakephp中的所有网址添加子域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望所有Cakephp网址都在用户会话中使用子域...因此,如果在用户会话中有条目 subdomain:user,则所有页面将以 user作为前缀:例如user.example.com,user.example.com / settings。

I want all Cakephp urls to use a subdomain if it is present in the users session... so if there is an entry 'subdomain:user' in the session of the user all pages will have 'user' as a prefix: Eg user.example.com, user.example.com/settings.

是否有一种简单的方法?

Is there an easy way to do this?

谢谢,

kSeudo

推荐答案

您可以使用一些自定义路由类。在APP / Lib / Route中创建一个名为UserSubdomainRoute.php的文件并将其放入。

There are custom route classes you could use. Create a file in APP/Lib/Route called UserSubdomainRoute.php and put this in it.

<?php
App::uses('AuthComponent', 'Controller/Component');
class UserSubdomainRoute extends CakeRoute {

/**
 * @var CakeRequest
 */
    private $Request;

/**
 * Length of domain's TLD
 *
 * @var int
 */
    public static $_tldLength = 1;

    public function __construct($template, $defaults = array(), $options = array()) {
        parent::__construct($template, $defaults, $options);

        $this->Request = new CakeRequest();
        $this->options = array_merge(array('protocol' => 'http'), $options);
    }

/**
 * Sets tld length
 *
 * @param $length
 * @return mixed void|int
 */
    public static function tldLength($length = null) {
        if (is_null($length)) {
            return self::$_tldLength;
        }

        self::$_tldLength = $length;
    }

/**
 * Writes out full url with protocol and subdomain
 *
 * @param $params
 * @return string
 */
    protected function _writeUrl($params) {
        $protocol = $this->options['protocol'];
        $subdomain = AuthComponent::user('subdomain');
        if (empty($subdomain)) {
            $subdomain = 'www';
        }
        $domain = $this->_getDomain();
        $url = parent::_writeUrl($params);

        return "{$protocol}://{$subdomain}.{$domain}{$url}";
    }

/**
 * Get domain name
 *
 * @return string
 */
    protected function _getDomain() {
        return $this->Request->domain(self::$_tldLength);
    }

}

对班级的一种改进可能是

One improvement to the class would probably be to make the $Request static.

不幸的是,在Cake 2.0中无法设置defaultRotueClass,但是我在2.1+中添加了该功能,但我没有想要告诉您进行升级,因此您将不得不为第三个参数中的所有路线手动指定它,如下所示:

Unfortunately in Cake 2.0 there is no way to set a defaultRotueClass, however, I added that feature in 2.1+ and I don't want to tell you to upgrade so you are going to have to manually specify it for all your routes in the third param like so:

Router::connect(..., ..., array('routeClass' => 'UserSubdomainRoute');

请确保添加在routes.php

Be sure to add at the top of routes.php

App::uses('UserSubdomainRoute', 'Lib/Route');

如果要升级到2.1+,可以在以下位置添加您的route.php顶部

If you do upgrade to 2.1+ you can just add this at the top of your routes.php

Router::defaultRouteClass('UserSubdomainRoute');

然后指定的所有溃败都将使用该路由类。

Then any routs specified after will use that route class.

路由类是 _writeUrl 方法。它检查会话中是否设置了子域密钥,否则使用 www 并构建完整的URL返回

The main part of the route class is the _writeUrl method. It checks to see if there is a subdomain key set in the session otherwise uses www and builds the full url to return.

注意:尚未测试课程,还在学校里只是想让您快速入门。这只是我SubdomainRoute的修改版本,其工作方式略有不同(它过去仅用于匹配设置了某个子域时的路由,例如,在我的应用中,ex将 clients 子域与我的 ClientsAdminPanel 插件。您可以在这里获取它: http: //bin.cakephp.org/view/50699397 因此,如果您需要 UserSubdomainRoute 和我的<$ c $的组合,就可以看到它是如何完成的c> SubdomainRoute (在链接中)。

Heads Up: Haven't tested the class, still at school just wanted to give you a jump start. It's just a modifed version of my SubdomainRoute which works a bit differently (it used to only match routes to when a certain subdomain is set, for ex in my app matches clients subdomain to my ClientsAdminPanel plugin. You can grab that here: http://bin.cakephp.org/view/50699397 So you can see how that's done as well if you need a combination of UserSubdomainRoute and my SubdomainRoute (in the link).

希望这对您有所帮助。让我知道是否有任何问题。

Hope this helps for now. Let me know if there are any problems.

编辑:
这是强制重定向的方法-告诉我有一种更好的方法。如果可以的话,我会进行更新。

Here's how to force a redirection - something tells me there's a better way. I'll update if I can think of it.

public function beforeFilter() {
     $subdomains = $this->request->subdomains();
     $subdomain = $this->Auth->user('subdomain');
     if (!empty($subdomain) && !in_array($subdomain, $subdomains)) {
        $this->redirect('http://' . $subdomain . '.site.com' . $this->request->here);
     }
}

这篇关于向Cakephp中的所有网址添加子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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