Blame view

library/WebPlugin/Routes.php 3.87 KB
1 2 3 4 5 6 7 8
<?php

/**
 * Created by PhpStorm.
 * User: liuziyang
 * Date: 14-1-15
 * Time: 16:13
 */
whb authored
9
namespace WebPlugin;
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

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)
    {

    }
}