0

symfony1.4フレームワークで開発されたウェブサイトがあります。このウェブサイトは複数のドメインを持つことができるはずです。各ドメインには、特別なホームページとその他すべてがあります。実際には、ドメインは各アクションのパラメータである必要があり、それに応じて、アクションはデータベースからデータを取得して表示します。

たとえば、私たちについてのページがあります。私たちについての内容をabout_usテーブルに保存します。このテーブルにはwebsite_idがあります。ウェブサイト情報はウェブサイトテーブルに保存されます。これを仮定します:

website (id, title, domain)
about_us (id, content, website_id)

ウェブサイトの内容:

(1, 'foo', 'http://www.foo.com') and (2, 'bar', 'http://www.bar.com')

about_usの内容:

(1, 'some foo', 1) and (2, 'some bar', 2)

問題は、このようにできるように、Symfonyプロジェクトをどのように構成すればよいかということです。ドメインをパラメーターとして取得し、それをSymfonyアクションで使用するには?

4

2 に答える 2

1

sfRouteを拡張する独自のルートクラスを作成できます。このルートは、すべてのリクエストに「ドメイン」パラメータを追加します。

//apps/frontend/lib/routing/myroute.class.php

class myRoute extends sfRoute
{

    public function matchesUrl($url, $context = array())
    {
        // first check if it is a valid route:
        if (false === $parameters = parent::matchesUrl($url, $context))
        {
           return false;
         }

        $domain = $context['host'];

        // add the $domain parameter:
        return array_merge(array(
            'domain' => $domain
            ), $parameters);
    }
}

Routing.yml(例):

default_module:
  class: myRoute
  url:   /:module/:action/:id
  ...

アクションでは、次のドメインを取得します。

 $request->getParameter('domain');
于 2012-11-03T22:45:49.803 に答える
1

これを行うには多くの方法があります。sfFrontWebControllerを拡張し、dispatch()メソッド内にコードを追加することができます。

# app/myapp/config/factories.yml
all:
  controller:
    class: myController


// lib/myController.class.php
class myController extends sfFrontWebController
{
    public function dispatch()
    {
        $selectedSite = SiteTable::retrieveByDomain($_SERVER['HTTP_HOST']); // Example

        if (!$selectedSite) {
            throw new sfException('Website not found');
        }

        // Store any site value in parameter
        $this->context->getRequest()->setParameter('site_id',$selectedSite->getId());

        parent::dispatch();
    }
}
于 2012-11-06T21:09:06.487 に答える