du9537 2013-12-12 07:07
浏览 40
已采纳

Yii控制台与不同的配置

In the web application we use same code and modules with different configs like:

in index.php file app will decide, wchin config to turn:

switch($_SERVER['HTTP_HOST']){
    default:
        $yii=$webRoot.'/framework/yiilite.php';
        $config = $webRoot.'/protected/config/main.php';
    break;
        case 'someurl.com':
              ...
        break;
        ...
}

But, how can I do it with console application? The reason in that I use different databases ant etc.

is it possible to do something like this:

$ ./protected/yiic --application=myappname [all defined commands as default]

in the code a

--application

will set with which console config to work

more explanation

my answer to @Joe Miller But the problem is, how choose theme? I did in the files foloowings:

in protectes/yiic

$__appId = null;
for( $__i=1,$__max=count($argv); $__i<$__max; ++$__i ) {
    if ( strpos($argv[$__i],'--appid',0) === 0 ) {
        $__appId = substr($argv[$__i], 8);
        unset($argv[$__i]);
    }
}

require_once(dirname(__FILE__).'/yiic.php');

and in protected/yiic.php

$__appIdsList = array(
    'my_site_1',
    'my_site_2',
    'my_site_3',
    'my_site_4',
);

$yiic=dirname(__FILE__).'/../framework/yiic.php';
$config=dirname(__FILE__).'/config/console_'.$__appId.'.php';

require_once($yiic);

and it works and it catchs that config file what I need

./protected/yiic --appid=my_site_1

bu when I`m trying to do migrate

./protected/yiic --appid=my_site_1 migrate

the app cant recognize comman and gives me migrates help list

And final conslusion (I solved it)

I`d like to add transperent console command without affecting it to other execution of builtin console commands and custom console commands.

Another requirement is, solve this issue on a low-level approach, without inheritance or overloading other classes or methods.

So, my solution is:

in protected/yiic

#!/usr/bin/env php
<?php

$__appId = null;
for( $__i=1,$__max=count($argv); $__i<$__max; ++$__i ) {
    if ( strpos($argv[$__i],'--appid',0) === 0 ) {
        $__appId = substr($argv[$__i], 8);
        unset($argv[$__i]);
        unset($_SERVER['argv'][$__i]);
        $argv = $_SERVER['argv'] = array_values($argv);
    }
}

require_once(dirname(__FILE__).'/yiic.php');

and in /protected/yiic.php

<?php

// change the following paths if necessary
$__appIdsList = array(
    'app_1',
    'app_2',
);

$yiic=dirname(__FILE__).'/../framework/yiic.php';
$config=dirname(__FILE__).'/config/console_'.$__appId.'.php';

if ( !is_file($config) ) {
    die("Error: There is no or wrong parametr appid. Please set parametr or correct. Example -appid={application_name}
\tThe list of available appid:
\t\t - ".implode("
\t\t - ", $__appIdsList));
}

require_once($yiic);

and now it is possible to set param "appid" in any place of command line, like

./protected/yiic  migrate --appid=app_1

and it acts only in that app what we need

PS: in any case, thanks @Joe Miller

  • 写回答

3条回答 默认 最新

  • dsoxcj7276 2014-09-16 12:58
    关注

    I think, I founded more confortable solution! It`s more easy, and solved all my requirements.

    in the file

    protected/yiic.php
    

    I write:

    ...
    $yiic=dirname(__FILE__).'/../lib/framework/yiic.php';
    
    if ( strpos(__FILE__,{first/place}) !== false ) {
        $config=dirname(__FILE__).'/config/first_config.php';
    } elseif ( strpos(__FILE__,{second/place}) !== false ) {
        $config=dirname(__FILE__).'/config/second_plase.php';
    } else {
        // by default
        $config=dirname(__FILE__).'/config/console.php'; 
    }
    require_once($yiic);
    ...
    

    where {first/place},{second/place} - a part of the project`s path. For example:

    Your first project is placed in:

    /var/www/aproject/first_one
    

    and the second one on the

    /var/www/aproject/second_one
    

    than you checks will be:

    // for first porject
    strpos(__FILE__,'aproject/first_one') !== false
    

    and etc.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?