Application.php 5.06 KB
Newer Older
Qiang Xue committed
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
Qiang Xue committed
5 6 7 8
 * @license http://www.yiiframework.com/license/
 */

namespace yii\web;
Qiang Xue committed
9

Qiang Xue committed
10
use Yii;
11
use yii\base\InvalidRouteException;
Qiang Xue committed
12 13

/**
14
 * Application is the base class for all web application classes.
Qiang Xue committed
15
 *
16
 * @property AssetManager $assetManager The asset manager component. This property is read-only.
17
 * @property string $homeUrl The homepage URL.
18 19 20 21
 * @property Request $request The request component. This property is read-only.
 * @property Response $response The response component. This property is read-only.
 * @property Session $session The session component. This property is read-only.
 * @property User $user The user component. This property is read-only.
22
 *
Qiang Xue committed
23 24 25 26 27
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class Application extends \yii\base\Application
{
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
    /**
     * @var string the default route of this application. Defaults to 'site'.
     */
    public $defaultRoute = 'site';
    /**
     * @var array the configuration specifying a controller action which should handle
     * all user requests. This is mainly used when the application is in maintenance mode
     * and needs to handle all incoming requests via a single action.
     * The configuration is an array whose first element specifies the route of the action.
     * The rest of the array elements (key-value pairs) specify the parameters to be bound
     * to the action. For example,
     *
     * ~~~
     * [
     *     'offline/notice',
     *     'param1' => 'value1',
     *     'param2' => 'value2',
     * ]
     * ~~~
     *
     * Defaults to null, meaning catch-all is not used.
     */
    public $catchAll;
    /**
     * @var Controller the currently active controller instance
     */
    public $controller;
55

56 57 58 59 60 61 62 63 64 65
    /**
     * @inheritdoc
     */
    public function preloadComponents()
    {
        parent::preloadComponents();
        $request = $this->getRequest();
        Yii::setAlias('@webroot', dirname($request->getScriptFile()));
        Yii::setAlias('@web', $request->getBaseUrl());
    }
Qiang Xue committed
66

67 68
    /**
     * Handles the specified request.
69 70
     * @param Request $request the request to be handled
     * @return Response the resulting response
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
     * @throws NotFoundHttpException if the requested route is invalid
     */
    public function handleRequest($request)
    {
        if (empty($this->catchAll)) {
            list ($route, $params) = $request->resolve();
        } else {
            $route = $this->catchAll[0];
            $params = array_splice($this->catchAll, 1);
        }
        try {
            Yii::trace("Route requested: '$route'", __METHOD__);
            $this->requestedRoute = $route;
            $result = $this->runAction($route, $params);
            if ($result instanceof Response) {
                return $result;
            } else {
                $response = $this->getResponse();
                if ($result !== null) {
                    $response->data = $result;
                }
92

93 94 95 96 97 98
                return $response;
            }
        } catch (InvalidRouteException $e) {
            throw new NotFoundHttpException($e->getMessage(), $e->getCode(), $e);
        }
    }
Qiang Xue committed
99

100
    private $_homeUrl;
Qiang Xue committed
101

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    /**
     * @return string the homepage URL
     */
    public function getHomeUrl()
    {
        if ($this->_homeUrl === null) {
            if ($this->getUrlManager()->showScriptName) {
                return $this->getRequest()->getScriptUrl();
            } else {
                return $this->getRequest()->getBaseUrl() . '/';
            }
        } else {
            return $this->_homeUrl;
        }
    }
Qiang Xue committed
117

118 119 120 121 122 123 124
    /**
     * @param string $value the homepage URL
     */
    public function setHomeUrl($value)
    {
        $this->_homeUrl = $value;
    }
Qiang Xue committed
125

126 127 128 129 130 131
    /**
     * Returns the request component.
     * @return Request the request component
     */
    public function getRequest()
    {
132
        return $this->get('request');
133
    }
Qiang Xue committed
134

135 136 137 138 139 140
    /**
     * Returns the response component.
     * @return Response the response component
     */
    public function getResponse()
    {
141
        return $this->get('response');
142
    }
143

144 145 146 147 148 149
    /**
     * Returns the session component.
     * @return Session the session component
     */
    public function getSession()
    {
150
        return $this->get('session');
151
    }
Qiang Xue committed
152

153 154 155 156 157 158
    /**
     * Returns the user component.
     * @return User the user component
     */
    public function getUser()
    {
159
        return $this->get('user');
160
    }
161

162 163 164 165 166 167
    /**
     * Returns the asset manager.
     * @return AssetManager the asset manager component
     */
    public function getAssetManager()
    {
168
        return $this->get('assetManager');
169
    }
Qiang Xue committed
170

171
    /**
172
     * @inheritdoc
173
     */
174
    public function coreComponents()
175
    {
176
        return array_merge([
177 178 179 180 181
            'request' => ['class' => 'yii\web\Request'],
            'response' => ['class' => 'yii\web\Response'],
            'session' => ['class' => 'yii\web\Session'],
            'user' => ['class' => 'yii\web\User'],
            'assetManager' => ['class' => 'yii\web\AssetManager'],
182
        ], parent::coreComponents());
183
    }
Qiang Xue committed
184
}