YII 的源碼分析(二)

WBOY
發布: 2016-08-08 09:32:45
原創
883 人瀏覽過

上一篇簡單分析了一下yii的流程,從創建一個應用,到螢幕上輸出結果。這次我來一個稍複雜一點的,重點在輸出上,不再是簡單的一行"hello world",而是要經過view(視圖)層的處理。

還是demos目錄,這次我們選擇hangman,一個簡單的猜字遊戲。老規則,還是從入口處開始看。

index.php:

<?<span>php

</span><span>//</span><span> change the following paths if necessary</span>
<span>$yii</span>=<span>dirname</span>(<span>__FILE__</span>).'/../../framework/yii.php'<span>;
</span><span>$config</span>=<span>dirname</span>(<span>__FILE__</span>).'/protected/config/main.php'<span>;

</span><span>//</span><span> remove the following line when in production mode
// defined('YII_DEBUG') or define('YII_DEBUG',true);</span>

<span>require_once</span>(<span>$yii</span><span>);
Yii</span>::createWebApplication(<span>$config</span>)->run();
登入後複製

和helloworld應用相比,這次多了main.php,開啟main看下原始碼:

<?<span>php

</span><span>return</span> <span>array</span><span>(
    </span>'name'=>'Hangman Game',
    'defaultController'=>'game',
    'components'=><span>array</span><span>(
        </span>'urlManager'=><span>array</span><span>(
            </span>'urlFormat'=>'path',
            'rules'=><span>array</span><span>(
                </span>'game/guess/<g:\w>'=>'game/guess',<span>
            )</span>,<span>
        )</span>,<span>
    )</span>,<span>
);</span>
登入後複製

在我們以後的實際專案中,也是經常要用到設定檔的,所以我覺得有必要了解yii的設定檔--main.php

'name'=>'這裡通常是定義網站的標題',也就是我們打開index.php時,網頁上顯示的標題。

'defaultController'=>'這裡是預設的控制器',也就是我們的index.php後面沒有指定控制器時系統採用的控制器,如果我們這裡沒有指出來,預設就是site

'components'=>'這裡是組件的參數,用多維數組進行配置。 ' 具體的參數可以查看yii手冊。

Yii::createWebApplication($config)->run(); 上次我們已經詳細分析過它了,這裡再簡單的走一遍:

CWebApplication.php -> CApplication.php -> __construct($config) :

<span>$this</span>-><span>preinit();

        </span><span>$this</span>-><span>initSystemHandlers();
        </span><span>$this</span>-><span>registerCoreComponents();

        </span><span>$this</span>->configure(<span>$config</span><span>);
        </span><span>$this</span>->attachBehaviors(<span>$this</span>-><span>behaviors);
        </span><span>$this</span>-><span>preloadComponents();

        </span><span>$this</span>->init();
登入後複製

上次我們沒有設定過程,所以$this->configure($config)什麼也沒做,但是這次有設定參數,所以我們進去看看yii做了哪些操作:

CApplication本身沒有實作configure方法,是繼承於CModule.php的:

    <span>public</span> <span>function</span> configure(<span>$config</span><span>)
    {
        </span><span>if</span>(<span>is_array</span>(<span>$config</span><span>))
        {
            </span><span>foreach</span>(<span>$config</span> <span>as</span> <span>$key</span>=><span>$value</span><span>)
                </span><span>$this</span>-><span>$key</span>=<span>$value</span><span>;
        }
    }</span>
登入後複製

程式碼非常簡單,就是把配置參數的鍵做為類別的屬性名,value做為類別的屬性值進行了擴充。完成這個過程就運行CApplication 上的run方法了。

    <span>public</span> <span>function</span><span> run()
    {
        </span><span>if</span>(<span>$this</span>->hasEventHandler('onBeginRequest'<span>))
            </span><span>$this</span>->onBeginRequest(<span>new</span> CEvent(<span>$this</span><span>));
        </span><span>register_shutdown_function</span>(<span>array</span>(<span>$this</span>,'end'),0,<span>false</span><span>);
        </span><span>$this</span>-><span>processRequest();
        </span><span>if</span>(<span>$this</span>->hasEventHandler('onEndRequest'<span>))
            </span><span>$this</span>->onEndRequest(<span>new</span> CEvent(<span>$this</span><span>));
    }</span>
登入後複製

我們前面說過,這裡只要關注 $this->processRequest(); 就可以了。運行的結果就是執行$this->runController('');  

    <span>public</span> <span>function</span> runController(<span>$route</span><span>)
    {
        </span><span>if</span>((<span>$ca</span>=<span>$this</span>->createController(<span>$route</span>))!==<span>null</span><span>)
        {
            </span><span>list</span>(<span>$controller</span>,<span>$actionID</span>)=<span>$ca</span><span>;
            </span><span>$oldController</span>=<span>$this</span>-><span>_controller;
            </span><span>$this</span>->_controller=<span>$controller</span><span>;
            </span><span>$controller</span>-><span>init();
            </span><span>$controller</span>->run(<span>$actionID</span><span>);
            </span><span>$this</span>->_controller=<span>$oldController</span><span>;
        }
        </span><span>else</span>
            <span>throw</span> <span>new</span> CHttpException(404,Yii::t('yii','Unable to resolve the request "{route}".',
                <span>array</span>('{route}'=><span>$route</span>===''?<span>$this</span>->defaultController:<span>$route</span><span>)));
    }</span>
登入後複製

由於url是index.php,後面沒有任何參數,所以都是走的預設控制器,也就是我們在main.php中設定的game. 所以$controller 就等於controllers/gameController.php, 通過上次的原始碼分析我們可以知道,在gameController.php中沒有init方法時,都是在走的父類別中定義的預設方法(實際上是一個空方法),

$controller->run($actionID<span>); == gameController</span>->run(''); gameController上没有实现run方法,于是又是去父类中找run
登入後複製

從class GameController extends CController 可以看出,父類別是CController , 找到對應的run方法:

<span>public</span> <span>function</span> run(<span>$actionID</span><span>)
    {
        </span><span>if</span>((<span>$action</span>=<span>$this</span>->createAction(<span>$actionID</span>))!==<span>null</span><span>)
        {
            </span><span>if</span>((<span>$parent</span>=<span>$this</span>->getModule())===<span>null</span><span>)
                </span><span>$parent</span>=Yii::<span>app();
            </span><span>if</span>(<span>$parent</span>->beforeControllerAction(<span>$this</span>,<span>$action</span><span>))
            {
                </span><span>$this</span>->runActionWithFilters(<span>$action</span>,<span>$this</span>-><span>filters());
                </span><span>$parent</span>->afterControllerAction(<span>$this</span>,<span>$action</span><span>);
            }
        }
        </span><span>else</span>
            <span>$this</span>->missingAction(<span>$actionID</span><span>);
    }</span>
登入後複製

前面已經分析過了,沒有指定時,都是預設參數。那麼此時的$actionID為空,actionID就是gameController定義的預設動作:public $defaultAction='play'; 

runActionWithFilters --->  runAction --> $action->runWithParams<br>这里的$action 需要从CAction -> CInlineAction中去找<br>
登入後複製
<span>public</span> <span>function</span> runWithParams(<span>$params</span><span>)
    {
        </span><span>$methodName</span>='action'.<span>$this</span>-><span>getId();
        </span><span>$controller</span>=<span>$this</span>-><span>getController();
        </span><span>$method</span>=<span>new</span> ReflectionMethod(<span>$controller</span>, <span>$methodName</span><span>);
        </span><span>if</span>(<span>$method</span>->getNumberOfParameters()>0<span>)
            </span><span>return</span> <span>$this</span>->runWithParamsInternal(<span>$controller</span>, <span>$method</span>, <span>$params</span><span>);
        </span><span>else</span>
            <span>return</span> <span>$controller</span>-><span>$methodName</span><span>();
    }</span>
登入後複製

走了這麼多過程,和hello world的流程是差不多的。根據上次的分析可以知道,這裡執行了

$controller->$methodName<span>(); 也就是GameController->actionPlay()<br>到此,我们本节的重点才真正开始:<br></span>
登入後複製
    <span>public</span> <span>function</span><span> actionPlay()
    {
        </span><span>static</span> <span>$levels</span>=<span>array</span><span>(
            </span>'10'=>'Easy game; you are allowed 10 misses.',
            '5'=>'Medium game; you are allowed 5 misses.',
            '3'=>'Hard game; you are allowed 3 misses.',<span>
        );

        </span><span>//</span><span> if a difficulty level is correctly chosen</span>
        <span>if</span>(<span>isset</span>(<span>$_POST</span>['level']) && <span>isset</span>(<span>$levels</span>[<span>$_POST</span>['level'<span>]]))
        {
            </span><span>$this</span>->word=<span>$this</span>-><span>generateWord();
            </span><span>$this</span>->guessWord=<span>str_repeat</span>('_',<span>strlen</span>(<span>$this</span>-><span>word));
            </span><span>$this</span>->level=<span>$_POST</span>['level'<span>];
            </span><span>$this</span>->misses=0<span>;
            </span><span>$this</span>->setPageState('guessed',<span>null</span><span>);
            </span><span>//</span><span> show the guess page</span>
            <span>$this</span>->render('guess'<span>);
        }
        </span><span>else</span><span>
        {
            </span><span>$params</span>=<span>array</span><span>(
                </span>'levels'=><span>$levels</span>,
                <span>//</span><span> if this is a POST request, it means the level is not chosen</span>
                'error'=>Yii::app()->request->isPostRequest,<span>
            );
            </span><span>//</span><span> show the difficulty level page</span>
            <span>$this</span>->render('play',<span>$params</span><span>);
        }
    }</span>
登入後複製
<span>显然走的是else的逻辑,重点请看</span> $this->render('play',$params); 这个render方法这么面熟,很多框架中都有类似的方法,比如discuz,smarty,CI 等等. 纵观yii框架,rnder 在它整个MVC模式中,是V得以实现的重要骨干。所以有必要把它翻个底朝天。<br>在CController.php中有这个方法:
登入後複製
    <span>public</span> <span>function</span> render(<span>$view</span>,<span>$data</span>=<span>null</span>,<span>$return</span>=<span>false</span><span>)
    {
        </span><span>if</span>(<span>$this</span>->beforeRender(<span>$view</span><span>))
        {
            </span><span>$output</span>=<span>$this</span>->renderPartial(<span>$view</span>,<span>$data</span>,<span>true</span><span>);
            </span><span>if</span>((<span>$layoutFile</span>=<span>$this</span>->getLayoutFile(<span>$this</span>->layout))!==<span>false</span><span>)
                </span><span>$output</span>=<span>$this</span>->renderFile(<span>$layoutFile</span>,<span>array</span>('content'=><span>$output</span>),<span>true</span><span>);

            </span><span>$this</span>->afterRender(<span>$view</span>,<span>$output</span><span>);

            </span><span>$output</span>=<span>$this</span>->processOutput(<span>$output</span><span>);

            </span><span>if</span>(<span>$return</span><span>)
                </span><span>return</span> <span>$output</span><span>;
            </span><span>else</span>
                <span>echo</span> <span>$output</span><span>;
        }
    }</span>
登入後複製

當我們echo $output=$this->renderPartial($view,$data,true);的時候,就發現,此時的$output已經就拿到我們最終的結果了。它對應的檔案是views/game/play.php

也就是我們在index.php上最後看到的內容了。由於本次渲染比較簡單,所以程式經過的流程也較少,但是從原始碼可以看到,裡面邊進行了許多的處理,例如主題什麼的。本次就先分析到這件事。晚安!

<br>
登入後複製

以上就介紹了YII 的源碼分析(二),包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板