开发者

Different data sets per subdomain in Cakephp?

I am writing a personal bookmarks application and am looking for a way to have totally different content between subdomains.

I have written the application and it works fine, but have yet to implement multiple collections. I have the following models:

  • Tag (unused so far)
  • Bookmark
  • Subject

Bookmarks are categorized in subjects and I'm planning to allow tagging in the future and see if that helps me manage my bookmarks more easily.

The current problem I have is that I'd like to separate bookmarks as a whole. I want to use subdomains like webdevelopment.bookmarks.local, languages.bookmarks.local, linux.bookmarks.local that work with an entire own set of domains and bookmarks.

I am considering adding a new model called Set (short for "bookmark sets") and defining sets based on the subdomain.

According to that plan I'd have to re开发者_StackOverflow社区write all $this->...->find-queries in the entire App to contain the condition "set_id" = $SubdomainBasedSetid".

While it wouldn't be that much work, I was wondering if it could be done smarter, maybe that Cake would only see the relevant bookmark set per subdomain.


well, your solution is right. But instead of using subdomains, you can use prefix, so you don't have check and set yourself. Since you use subdomains, I assume that these sets are fixed or rarely change. So you don't really need a sets table, just use the set name directly in your bookmark record, so you don't have to convert between name and id.


According to that plan I'd have to rewrite all $this->...->find-queries in the entire App to contain the condition "set_id" = $SubdomainBasedSetid".

As all models extend AppModel, it is possible to edit all queries there before they happen (ie. DRY). :)

// app/app_model.php
class AppModel extends Model {

    public function beforeFind($queryData) { // old query
        // make changes
        return $queryData; // new query
    }

However, if you don't want this functionality for all models (or even if you do for now), a better place might be a behavior as this allows you to pick and choose where and when it is loaded:

// app/models/behaviors/subdomain.php
class SubdomainBehavior extends ModelBehavior {

    protected $_defaults = array('field' => 'Site.subdomain');

    public function setup(&$model, $config = array()) {
        $this->settings[$model->alias] = array_merge($this->_defaults, $config);
    }

    public function beforeFind(&$model, $queryData) {
        $domain = $_SERVER['SERVER_NAME'];
        $subdomain = substr($domain, 0, strpos($domain, "."));
        $queryData['conditions'][$this->settings['field']] = $subdomain;
        return $queryData;
    }

}

// app/app_model.php
class AppModel extends Model {

    $actsAs = array('Subdomain' => array('field' => 'Set.slug'));

}
  • Tag (unused so far)

To save reinventing the wheel, you may want to look at CakeDC's tags and utils plugins (the latter contains SluggableBehavior which will help with generating friendly URL parts, such as the subdomains).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜