Application.php 4 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 15 16 17 18 19 20

/**
 * Application is the base class for all application classes.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class Application extends \yii\base\Application
{
Qiang Xue committed
21 22 23 24
	/**
	 * @var string the default route of this application. Defaults to 'site'.
	 */
	public $defaultRoute = 'site';
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
	/**
	 * @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,
	 *
	 * ~~~
	 * array(
	 *     'offline/notice',
	 *     'param1' => 'value1',
	 *     'param2' => 'value2',
	 * )
	 * ~~~
	 *
	 * Defaults to null, meaning catch-all is not effective.
	 */
	public $catchAll;
44 45 46 47
	/**
	 * @var Controller the currently active controller instance
	 */
	public $controller;
48

Qiang Xue committed
49

Qiang Xue committed
50
	/**
51 52 53 54
	 * Handles the specified request.
	 * @param Request $request the request to be handled
	 * @return Response the resulting response
	 * @throws HttpException if the requested route is invalid
Qiang Xue committed
55
	 */
56
	public function handleRequest($request)
Qiang Xue committed
57
	{
Qiang Xue committed
58 59
		Yii::setAlias('@wwwroot', dirname($request->getScriptFile()));
		Yii::setAlias('@www', $request->getBaseUrl());
60

61 62 63 64 65 66
		if (empty($this->catchAll)) {
			list ($route, $params) = $request->resolve();
		} else {
			$route = $this->catchAll[0];
			$params = array_splice($this->catchAll, 1);
		}
67
		try {
Qiang Xue committed
68
			Yii::trace("Route requested: '$route'", __METHOD__);
Qiang Xue committed
69
			$this->requestedRoute = $route;
70 71 72 73 74 75
			$result = $this->runAction($route, $params);
			if ($result instanceof Response) {
				return $result;
			} else {
				$response = $this->getResponse();
				if ($result !== null) {
76
					$response->data = $result;
77 78 79
				}
				return $response;
			}
80 81 82
		} catch (InvalidRouteException $e) {
			throw new HttpException(404, $e->getMessage(), $e->getCode(), $e);
		}
Qiang Xue committed
83
	}
Qiang Xue committed
84

Qiang Xue committed
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
	private $_homeUrl;

	/**
	 * @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;
		}
	}

	/**
	 * @param string $value the homepage URL
	 */
	public function setHomeUrl($value)
	{
		$this->_homeUrl = $value;
	}

Qiang Xue committed
111 112 113 114 115 116 117 118 119
	/**
	 * Returns the request component.
	 * @return Request the request component
	 */
	public function getRequest()
	{
		return $this->getComponent('request');
	}

120 121 122 123 124 125 126 127 128
	/**
	 * Returns the response component.
	 * @return Response the response component
	 */
	public function getResponse()
	{
		return $this->getComponent('response');
	}

Qiang Xue committed
129 130 131 132 133 134 135 136 137
	/**
	 * Returns the session component.
	 * @return Session the session component
	 */
	public function getSession()
	{
		return $this->getComponent('session');
	}

138 139 140 141 142 143 144 145 146
	/**
	 * Returns the user component.
	 * @return User the user component
	 */
	public function getUser()
	{
		return $this->getComponent('user');
	}

Qiang Xue committed
147 148 149 150
	/**
	 * Returns the asset manager.
	 * @return AssetManager the asset manager component
	 */
151
	public function getAssetManager()
Qiang Xue committed
152
	{
153
		return $this->getComponent('assetManager');
Qiang Xue committed
154 155
	}

Qiang Xue committed
156 157 158 159 160 161 162 163 164 165 166
	/**
	 * Registers the core application components.
	 * @see setComponents
	 */
	public function registerCoreComponents()
	{
		parent::registerCoreComponents();
		$this->setComponents(array(
			'request' => array(
				'class' => 'yii\web\Request',
			),
167 168 169
			'response' => array(
				'class' => 'yii\web\Response',
			),
Qiang Xue committed
170 171 172
			'session' => array(
				'class' => 'yii\web\Session',
			),
173 174 175
			'user' => array(
				'class' => 'yii\web\User',
			),
176
			'assetManager' => array(
Qiang Xue committed
177 178
				'class' => 'yii\web\AssetManager',
			),
Qiang Xue committed
179 180
		));
	}
Qiang Xue committed
181
}