Multisite

Witam. To mój pierwszy post tutaj.

Niedawno wpadłem na ten framework i pomyślałem, że może warto się nim zainteresować. Początkowe przykłady wydają się dość ciekawe. To tyle tytułem wstępu :)

Zastanawiam się nad czymś takim. Istnieją przykładowo takie domeny:

www.domena.pl

forum.domena.pl

blog.domena.pl

Czy da się to tak zrobić by to działało na jednej instalacji Yii. Na przykładowym serwerze katalogi są takie:

/domains/domena.pl/public_html

/domains/domena.pl/public_html/forum

/domains/domena.pl/public_html/blog

Macie propozycje jak to wykonać? Forum i blog to miałyby być oddzielne aplikacje ale mające część wspólną (np. baza userów).

Hi, with help of modules you can do it.

The directory structure could look like this:




- /domains/domena.pl/public_html

  - protected

    - controllers

    - models

    - views

    - modules

      - blog

        - controllers

        - views

      - forum

        - controllers

        - views



In config you can manage all possible url rules:




'components' => array(

   ...

   'urlManager' => array(

      'rules' => array(


         'http://www.domena.pl' => 'site/index',

         'http://www.domena.pl/contact' => 'site/contact',

         ...


         'http://blog.domena.pl' => 'blog/site/index',

         ...


         'http://forum.domena.pl' => 'forum/site/index',        

         ...


      ),

   ),

   ...

),



Also take a look at these cookbooks: #1, #2

Sorry for english, i translated your post with google :)

Thanks very much :) I must redirect all subdomain to index.php in .htaccess?

Well, I guess you run everything under the same ip address. So in the .htaccess you must only make sure every request gets redirected to the index.php entry-script.




RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . index.php [L]



Hi,

I had similar problem.

My application has few modules and base part with components, extensions and models for all modules.

My application handle domain in index.php.

Its not too pretty solution but I want load separated config per module and my modules has similar url rules.

My index.php file




$yii=dirname(__FILE__).'/../yii-1.1.0.r1700/yii.php';

defined('YII_DEBUG') or define('YII_DEBUG',true);

require_once($yii);


function getDomainName() {

        $http = ($secure = (isset($_SERVER['HTTPS']) && !strcasecmp($_SERVER['HTTPS'],'on'))) ? 'https' : 'http';

        if (isset($_SERVER['HTTP_HOST'])) {

                $hostInfo=$http.'://'.$_SERVER['HTTP_HOST'];

        } else {

                $hostInfo=$http.'://'.$_SERVER['SERVER_NAME'];

                $port=$_SERVER['SERVER_PORT'];

                if (($port!=80 && !$secure) || ($port!=443 && $secure))

                        $hostInfo.=':'.$port;

        }

        $matches = array();

        preg_match("/^(?<protocol>(http|https):\/\/)(?<www>(www\.|))((?<domain>.+\.[a-z]+))$/", $hostInfo, $matches);

        return trim($matches['domain']);

}


switch (getDomainName()) {

	case 'subdomain1.pl':

		$config = 'subdomain1';

		break;

        default:

                exit;

                break;

}

Yii::createWebApplication(dirname(__FILE__).'/protected/config/'.$config.'.php')->run();




function getDomainName() based on http://www.yiiframework.com/doc/cookbook/55/

and I have main config file




return array(

	'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',	

	'import'=>array(

		'application.models.*',

		'application.components.*',

	),	

	'components'=>array(

		'urlManager'=>array(

			'urlFormat'=>'path',

			'showScriptName'=> false,

		),

	),

	'params'=>array(),

);



and configs for all subdomains (one per subdomain)




$main = include 'main.php';


$hc = array(

	'name'=>'subdomain1',

	'defaultController'=>'index',

	'modules'=>array(

		'subdomain1'=>array(

			'layout'=>'subdomain1',

			'defaultController'=>'index',

		),

	),	

	'components'=>array(

		'urlManager'=>array(

                    .....

		     ),

		),

	),

	'params'=>array(),

);


return CMap::mergeArray($main, $hc);




Modules in my application are not exactly modules but separated application with one common part.