Routes.php 4 KB
<?php

/**
 * Created by PhpStorm.
 * User: liuziyang
 * Date: 14-1-15
 * Time: 16:13
 */
namespace Plugin;

use Yaf\Plugin_abstract;
use Yaf\Request_Abstract;
use Yaf\Response_Abstract;
use Yaf\Registry;
use Yaf\Dispatcher;
use Yaf\Loader;
class Routes extends Plugin_abstract
{
    /**
     *
     *
     * 在路由之前触发,这个是7个事件中, 最早的一个. 但是一些全局自定的工作, 还是应该放在Bootstrap中去完成
     * @param Request_Abstract $request
     * @param Response_Abstract $response
     * @return bool|void
     */
    public function routerStartup(Request_Abstract $request, Response_Abstract $response)
    {
        $_httpResult = explode('.', $request->getServer('HTTP_HOST'));
        $level = 'm';
        if (count($_httpResult) == 3) {
            list ($level, $domainName, $suffix) = $_httpResult;
        } else if (count($_httpResult) == 4) {
            $level = $_httpResult[0] . '.' . $_httpResult[1];
        }
        $_config = Registry::get('appConfig');
        $_modulesList = array();
        if (!empty($_config['application']['modules'])) {
            $_modules = $_config['application']['modules'];
            $_modulesList = explode(',', $_modules);
        }
        
        $newDomain = array(
            'guang.m'=>'guang'
        );
        if (isset($newDomain[strtolower($level)]) ) {
            $level = $newDomain[$level];
            $request->module = ucfirst(strtolower($level));
            
        } else if ($level != 'www' && in_array(ucfirst($level), $_modulesList, true)) {
            $request->module = ucfirst($level);
        } else {
            $request->module = 'Default';
            $level = 'Default';
        }
        $dispatcher = Dispatcher::getInstance();
        $app = $dispatcher->getApplication();
        $file = $app->getAppDirectory() . "/modules/" . ucfirst($level) . "/__init.php";
        if (file_exists($file)) {
            Loader::import($file);
        }
    }

    /**
     *
     *
     * 路由结束之后触发,此时路由一定正确完成, 否则这个事件不会触发
     * @param Request_Abstract $request
     * @param Response_Abstract $response
     * @return bool|void
     */
    public function routerShutdown(Request_Abstract $request, Response_Abstract $response)
    {

    }

    /**
     *
     *
     * 分发循环开始之前被触发
     * @param Request_Abstract $request
     * @param Response_Abstract $response
     * @return bool|void
     */
    public function dispatchLoopStartup(Request_Abstract $request, Response_Abstract $response)
    {

    }

    /**
     *
     *
     * 分发之前触发    如果在一个请求处理过程中, 发生了forward, 则这个事件会被触发多次
     * @param Request_Abstract $request
     * @param Response_Abstract $response
     * @return bool|void
     */
    public function preDispatch(Request_Abstract $request, Response_Abstract $response)
    {

    }

    /**
     *
     *
     * 分发结束之后触发,此时动作已经执行结束, 视图也已经渲染完成. 和preDispatch类似, 此事件也可能触发多次
     * @param Request_Abstract $request
     * @param Response_Abstract $response
     * @return bool|void
     */
    public function postDispatch(Request_Abstract $request, Response_Abstract $response)
    {

    }

    /**
     *
     *
     * 分发循环结束之后触发,此时表示所有的业务逻辑都已经运行完成, 但是响应还没有发送
     * @param Request_Abstract $request
     * @param Response_Abstract $response
     * @return bool|void
     */
    public function dispatchLoopShutdown(Request_Abstract $request, Response_Abstract $response)
    {

    }

    /**
     * @param Request_Abstract $request
     * @param Response_Abstract $response
     * @return bool|void
     */
    public function preResponse(Request_Abstract $request, Response_Abstract $response)
    {

    }
}