diff --git a/framework/YiiBase.php b/framework/YiiBase.php index d57c92d..3de9bd8 100644 --- a/framework/YiiBase.php +++ b/framework/YiiBase.php @@ -63,7 +63,7 @@ class YiiBase */ public static $classPath = array(); /** - * @var yii\base\Application the application instance + * @var yii\console\Application|yii\web\Application the application instance */ public static $app; /** diff --git a/framework/base/Application.php b/framework/base/Application.php index 468494c..5fd0401 100644 --- a/framework/base/Application.php +++ b/framework/base/Application.php @@ -398,8 +398,6 @@ class Application extends Module public function registerDefaultAliases() { Yii::$aliases['@app'] = $this->getBasePath(); - Yii::$aliases['@entry'] = dirname($_SERVER['SCRIPT_FILENAME']); - Yii::$aliases['@www'] = ''; } /** diff --git a/framework/base/Request.php b/framework/base/Request.php index 9db85a3..06d4c06 100644 --- a/framework/base/Request.php +++ b/framework/base/Request.php @@ -13,12 +13,18 @@ namespace yii\base; * @author Qiang Xue * @since 2.0 */ -class Request extends Component +abstract class Request extends Component { private $_scriptFile; private $_isConsoleRequest; /** + * Resolves the current request into a route and the associated parameters. + * @return array the first element is the route, and the second is the associated parameters. + */ + abstract public function resolve(); + + /** * Returns a value indicating whether the current request is made via command line * @return boolean the value indicating whether the current request is made via console */ diff --git a/framework/console/Request.php b/framework/console/Request.php index 5af2d4f..5fae031 100644 --- a/framework/console/Request.php +++ b/framework/console/Request.php @@ -22,6 +22,10 @@ class Request extends \yii\base\Request return isset($_SERVER['argv']) ? $_SERVER['argv'] : array(); } + /** + * Resolves the current request into a route and the associated parameters. + * @return array the first element is the route, and the second is the associated parameters. + */ public function resolve() { $rawParams = $this->getRawParams(); diff --git a/framework/web/Application.php b/framework/web/Application.php index 92686e4..a7e019e 100644 --- a/framework/web/Application.php +++ b/framework/web/Application.php @@ -23,7 +23,7 @@ class Application extends \yii\base\Application public function registerDefaultAliases() { parent::registerDefaultAliases(); - \Yii::$aliases['@www'] = dirname($_SERVER['SCRIPT_FILENAME']); + \Yii::$aliases['@webroot'] = dirname($_SERVER['SCRIPT_FILENAME']); } /** @@ -32,8 +32,8 @@ class Application extends \yii\base\Application */ public function processRequest() { - $route = $this->getUrlManager()->parseRequest($this->getRequest()); - return $this->runAction($route, $_GET); + list ($route, $params) = $this->getRequest()->resolve(); + return $this->runAction($route, $params); } /** @@ -46,6 +46,24 @@ class Application extends \yii\base\Application } /** + * 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'); + } + + /** * @return UrlManager */ public function getUrlManager() diff --git a/framework/web/Request.php b/framework/web/Request.php index 3d7ebc1..a4a1fb9 100644 --- a/framework/web/Request.php +++ b/framework/web/Request.php @@ -10,6 +10,7 @@ namespace yii\web; use Yii; +use yii\base\HttpException; use yii\base\InvalidConfigException; /** @@ -38,6 +39,25 @@ class Request extends \yii\base\Request private $_cookies; /** + * Resolves the current request into a route and the associated parameters. + * @return array the first element is the route, and the second is the associated parameters. + * @throws HttpException if the request cannot be resolved. + */ + public function resolve() + { + Yii::setAlias('@www', $this->getBaseUrl()); + + $result = Yii::$app->getUrlManager()->parseRequest($this); + if ($result !== false) { + list ($route, $params) = $result; + $params = array_merge($_GET, $params); + return array($route, $params); + } else { + throw new HttpException(404, Yii::t('yii|Page not found.')); + } + } + + /** * Returns the method of the current request (e.g. GET, POST, HEAD, PUT, DELETE). * @return string request method, such as GET, POST, HEAD, PUT, DELETE. * The value returned is turned into upper case.