2012-05-11 21 views
0

我希望所有Cakephp網址在用戶會話中都使用子域...所以如果在用戶會話中有一個條目「subdomain:user」,所有頁面將「用戶」作爲前綴:例如user.example.com,user.example.com/settings。在Cakephp的所有網址中添加一個子域

有沒有簡單的方法來做到這一點?

感謝,

kSeudo

+0

哪個蛋糕版? – tigrang

+0

2.0,你有什麼想法嗎? – kSeudo

+1

是的,但我必須在幾個小時內回答,上學 – tigrang

回答

6

有您可以使用的自定義路線類。在APP/Lib/Route中創建一個名爲UserSubdomainRoute.php的文件並放入它。

<?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); 
    } 

} 

對類的一種改進可能是使$ Request靜態。

不幸的是,在Cake 2.0中,沒有辦法設置defaultRotueClass,但是我在2.1+中添加了該功能,並且我不想告訴您升級,因此您將不得不手動將它指定爲所有您的第三PARAM像這樣的路線:

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

一定要在routes.php文件

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

的頂部添加如果升級到2.1及以上版本,你可以就在頂部添加此您routes.php

Router::defaultRouteClass('UserSubdomainRoute'); 

然後任何指定的路由將使用該路由類。

路線類的主要部分是_writeUrl方法。它檢查會話中是否存在subdomain密鑰集,否則使用www並構建完整的url以返回。

單挑:沒有測試過課,還在學校只是想給你一個跳躍的開始。它只是我的SubdomainRoute的修改版本,它的工作原理有點不同(它只用於匹配某個子域的路由,因爲前者在我的應用中匹配clients子域到我的ClientsAdminPanel插件,您可以在此處獲取:http://bin.cakephp.org/view/50699397因此,您可以看到這樣做了,以及如果你需要和UserSubdomainRoute組合我SubdomainRoute(以鏈接)

希望這有助於現在讓我知道,如果有任何問題

編輯:。 這裏的如何強制重定向 - 有些東西告訴我有更好的方法,如果我能想到它,我會更新。

​​
+0

這是非常感謝Tigrang。 雖然我仍然不是100%。我實現了cakeroute覆蓋,並在_writeUrl函數中獲得了子域的正確URL(例如customer.example.com),但似乎忽略了返回的子域。瀏覽器只需轉到原始域。 其奇怪的原因是,如果我將子域更改爲像'sfdsf'這樣的固定的東西,這顯示在瀏覽器中,但是當我從會話中設置子域時,我無法正常工作(我可以打印出「{$ protocol}:// { $ subdomain}。{$ domain} {$ url}「;並且它看起來很好 這裏有什麼想法嗎?再次感謝 – kSeudo

+0

所以'AuthComponent :: user('subdomain')'確實保存了正確的值,但瀏覽器地址仍然site.com沒有子域? – tigrang

+0

是的會話值是罰款:當我輸出: 「{$協議}://{{子域}。{$域} {$ url}」我得到但是這並不是瀏覽器顯示的地址,它真的很奇怪,順便我升級到2.1,因爲我只是開始了我的項目...... – kSeudo

0

好,如果你想這樣做對所有的鏈接可能是你用同樣的方式在網站上展示的所有您的網址,你使用類似$這個 - > HTML的「鏈接( '/嗒嗒/ ASD /', '等等'); ?

0

蛋糕/路由/路由

class SubdomainRoute extends CakeRoute { 
public function match($params) { 
    $subdomain = isset($params['subdomain']) ? $params['subdomain'] : null; 
    unset($params['subdomain']); 
    $path = parent::match($params); 
    if ($subdomain) { 
     $path = 'http://' . $subdomain . '.localhost' . $path; 
    } 
    return $path; 
} 
} 

當創建的鏈接,你可以做以下,使其他子域名指向的鏈接。

echo $this->Html->link(
    'Other domain', 
    array('subdomain' => 'test', 'controller' => 'posts', 'action' => 'add') 
); 
相關問題