Application.php 2.91 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 12
use yii\base\HttpException;
use yii\base\InvalidRouteException;
Qiang Xue committed
13 14 15 16 17 18 19 20 21

/**
 * 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
22 23 24 25 26
	/**
	 * @var string the default route of this application. Defaults to 'site'.
	 */
	public $defaultRoute = 'site';

Qiang Xue committed
27 28 29
	/**
	 * Processes the request.
	 * @return integer the exit status of the controller action (0 means normal, non-zero values mean abnormal)
30
	 * @throws HttpException if the request cannot be resolved.
Qiang Xue committed
31 32 33
	 */
	public function processRequest()
	{
Qiang Xue committed
34 35 36 37
		$request = $this->getRequest();
		Yii::setAlias('@wwwroot', dirname($request->getScriptFile()));
		Yii::setAlias('@www', $request->getBaseUrl());
		list ($route, $params) = $request->resolve();
38 39 40 41 42
		try {
			return $this->runAction($route, $params);
		} catch (InvalidRouteException $e) {
			throw new HttpException(404, $e->getMessage(), $e->getCode(), $e);
		}
Qiang Xue committed
43
	}
Qiang Xue committed
44

Qiang Xue committed
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
	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
71 72 73 74 75 76 77 78 79
	/**
	 * Returns the request component.
	 * @return Request the request component
	 */
	public function getRequest()
	{
		return $this->getComponent('request');
	}

Qiang Xue committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
	/**
	 * Returns the response component.
	 * @return Response the response component
	 */
	public function getResponse()
	{
		return $this->getComponent('response');
	}

	/**
	 * Returns the session component.
	 * @return Session the session component
	 */
	public function getSession()
	{
		return $this->getComponent('session');
	}

98 99 100 101 102 103 104 105 106
	/**
	 * Returns the user component.
	 * @return User the user component
	 */
	public function getUser()
	{
		return $this->getComponent('user');
	}

Qiang Xue committed
107 108 109 110
	/**
	 * Returns the asset manager.
	 * @return AssetManager the asset manager component
	 */
111
	public function getAssetManager()
Qiang Xue committed
112
	{
113
		return $this->getComponent('assetManager');
Qiang Xue committed
114 115
	}

Qiang Xue committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129
	/**
	 * Registers the core application components.
	 * @see setComponents
	 */
	public function registerCoreComponents()
	{
		parent::registerCoreComponents();
		$this->setComponents(array(
			'request' => array(
				'class' => 'yii\web\Request',
			),
			'response' => array(
				'class' => 'yii\web\Response',
			),
Qiang Xue committed
130 131 132
			'session' => array(
				'class' => 'yii\web\Session',
			),
133 134 135
			'user' => array(
				'class' => 'yii\web\User',
			),
136
			'assetManager' => array(
Qiang Xue committed
137 138
				'class' => 'yii\web\AssetManager',
			),
Qiang Xue committed
139 140
		));
	}
Qiang Xue committed
141
}