Browse Source

Added concrete http exception classes.

tags/2.0.0-beta
Qiang Xue 11 years ago
parent
commit
c7c13a6693
  1. 4
      apps/advanced/frontend/controllers/SiteController.php
  2. 6
      docs/guide/authorization.md
  3. 2
      docs/guide/controller.md
  4. 4
      extensions/debug/Module.php
  5. 4
      extensions/debug/controllers/DefaultController.php
  6. 4
      extensions/gii/Module.php
  7. 14
      extensions/gii/controllers/DefaultController.php
  8. 6
      extensions/gii/generators/crud/templates/controller.php
  9. 4
      framework/yii/base/Application.php
  10. 4
      framework/yii/web/AccessControl.php
  11. 28
      framework/yii/web/AccessDeniedHttpException.php
  12. 4
      framework/yii/web/Application.php
  13. 28
      framework/yii/web/BadRequestHttpException.php
  14. 6
      framework/yii/web/Controller.php
  15. 28
      framework/yii/web/MethodNotAllowedHttpException.php
  16. 28
      framework/yii/web/NotFoundHttpException.php
  17. 2
      framework/yii/web/Request.php
  18. 2
      framework/yii/web/User.php
  19. 2
      framework/yii/web/VerbFilter.php

4
apps/advanced/frontend/controllers/SiteController.php

@ -7,7 +7,7 @@ use yii\web\Controller;
use common\models\LoginForm;
use frontend\models\ContactForm;
use common\models\User;
use yii\web\HttpException;
use yii\web\BadRequestHttpException;
use yii\helpers\Security;
class SiteController extends Controller
@ -132,7 +132,7 @@ class SiteController extends Controller
]);
if (!$model) {
throw new HttpException(400, 'Wrong password reset token.');
throw new BadRequestHttpException('Wrong password reset token.');
}
$model->scenario = 'resetPassword';

6
docs/guide/authorization.md

@ -234,10 +234,10 @@ public function editArticle($id)
{
$article = Article::find($id);
if (!$article) {
throw new HttpException(404);
throw new NotFoundHttpException;
}
if (!\Yii::$app->user->checkAccess('edit_article', ['article' => $article])) {
throw new HttpException(403);
throw new AccessDeniedHttpException;
}
// ...
}
@ -250,7 +250,7 @@ public function editArticle($id)
{
$article = Article::find(['id' => $id, 'author_id' => \Yii::$app->user->id]);
if (!$article) {
throw new HttpException(404);
throw new NotFoundHttpException;
}
// ...
}

2
docs/guide/controller.md

@ -122,7 +122,7 @@ class BlogController extends Controller
{
$post = Post::find($id);
if (!$post) {
throw new HttpException(404);
throw new NotFoundHttpException;
}
if (\Yii::$app->request->isPost)) {

4
extensions/debug/Module.php

@ -10,7 +10,7 @@ namespace yii\debug;
use Yii;
use yii\base\Application;
use yii\web\View;
use yii\web\HttpException;
use yii\web\AccessDeniedHttpException;
/**
* The Yii Debug Module provides the debug toolbar and debugger
@ -79,7 +79,7 @@ class Module extends \yii\base\Module
} elseif ($action->id === 'toolbar') {
return false;
} else {
throw new HttpException(403, 'You are not allowed to access this page.');
throw new AccessDeniedHttpException('You are not allowed to access this page.');
}
}

4
extensions/debug/controllers/DefaultController.php

@ -9,7 +9,7 @@ namespace yii\debug\controllers;
use Yii;
use yii\web\Controller;
use yii\web\HttpException;
use yii\web\NotFoundHttpException;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
@ -99,7 +99,7 @@ class DefaultController extends Controller
}
$this->summary = $data['summary'];
} else {
throw new HttpException(404, "Unable to find debug data tagged with '$tag'.");
throw new NotFoundHttpException("Unable to find debug data tagged with '$tag'.");
}
}
}

4
extensions/gii/Module.php

@ -8,7 +8,7 @@
namespace yii\gii;
use Yii;
use yii\web\HttpException;
use yii\web\AccessDeniedHttpException;
/**
* This is the main module class for the Gii module.
@ -110,7 +110,7 @@ class Module extends \yii\base\Module
if ($this->checkAccess()) {
return parent::beforeAction($action);
} else {
throw new HttpException(403, 'You are not allowed to access this page.');
throw new AccessDeniedHttpException('You are not allowed to access this page.');
}
}

14
extensions/gii/controllers/DefaultController.php

@ -9,7 +9,7 @@ namespace yii\gii\controllers;
use Yii;
use yii\web\Controller;
use yii\web\HttpException;
use yii\web\NotFoundHttpException;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
@ -69,7 +69,7 @@ class DefaultController extends Controller
}
}
}
throw new HttpException(404, "Code file not found: $file");
throw new NotFoundHttpException("Code file not found: $file");
}
public function actionDiff($id, $file)
@ -84,7 +84,7 @@ class DefaultController extends Controller
}
}
}
throw new HttpException(404, "Code file not found: $file");
throw new NotFoundHttpException("Code file not found: $file");
}
/**
@ -94,7 +94,7 @@ class DefaultController extends Controller
* @param string $id the ID of the generator
* @param string $name the action name
* @return mixed the result of the action.
* @throws HttpException if the action method does not exist.
* @throws NotFoundHttpException if the action method does not exist.
*/
public function actionAction($id, $name)
{
@ -103,7 +103,7 @@ class DefaultController extends Controller
if (method_exists($generator, $method)) {
return $generator->$method();
} else {
throw new HttpException(400, "Unknown generator action: $name");
throw new NotFoundHttpException("Unknown generator action: $name");
}
}
@ -136,7 +136,7 @@ class DefaultController extends Controller
* Loads the generator with the specified ID.
* @param string $id the ID of the generator to be loaded.
* @return \yii\gii\Generator the loaded generator
* @throws \yii\web\HttpException
* @throws NotFoundHttpException
*/
protected function loadGenerator($id)
{
@ -146,7 +146,7 @@ class DefaultController extends Controller
$this->generator->load($_POST);
return $this->generator;
} else {
throw new HttpException(404, "Code generator not found: $id");
throw new NotFoundHttpException("Code generator not found: $id");
}
}
}

6
extensions/gii/generators/crud/templates/controller.php

@ -29,7 +29,7 @@ namespace <?= StringHelper::dirname(ltrim($generator->controllerClass, '\\')) ?>
use <?= ltrim($generator->modelClass, '\\') ?>;
use <?= ltrim($generator->searchModelClass, '\\') ?><?php if (isset($searchModelAlias)):?> as <?= $searchModelAlias ?><?php endif ?>;
use <?= ltrim($generator->baseControllerClass, '\\') ?>;
use yii\web\HttpException;
use yii\web\NotFoundHttpException;
use yii\web\VerbFilter;
/**
@ -130,7 +130,7 @@ class <?= $controllerClass ?> extends <?= StringHelper::basename($generator->bas
* If the model is not found, a 404 HTTP exception will be thrown.
* <?= implode("\n\t * ", $actionParamComments) . "\n" ?>
* @return <?= $modelClass ?> the loaded model
* @throws HttpException if the model cannot be found
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel(<?= $actionParams ?>)
{
@ -148,7 +148,7 @@ if (count($pks) === 1) {
if (($model = <?= $modelClass ?>::find(<?= $condition ?>)) !== null) {
return $model;
} else {
throw new HttpException(404, 'The requested page does not exist.');
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}

4
framework/yii/base/Application.php

@ -634,9 +634,9 @@ abstract class Application extends Module
{
$category = get_class($exception);
if ($exception instanceof HttpException) {
$category .= '\\' . $exception->statusCode;
$category = 'yii\\web\\HttpException:' . $exception->statusCode;
} elseif ($exception instanceof \ErrorException) {
$category .= '\\' . $exception->getSeverity();
$category .= ':' . $exception->getSeverity();
}
Yii::error((string)$exception, $category);
}

4
framework/yii/web/AccessControl.php

@ -131,14 +131,14 @@ class AccessControl extends ActionFilter
* The default implementation will redirect the user to the login page if he is a guest;
* if the user is already logged, a 403 HTTP exception will be thrown.
* @param User $user the current user
* @throws HttpException if the user is already logged in.
* @throws AccessDeniedHttpException if the user is already logged in.
*/
protected function denyAccess($user)
{
if ($user->getIsGuest()) {
$user->loginRequired();
} else {
throw new HttpException(403, Yii::t('yii', 'You are not allowed to perform this action.'));
throw new AccessDeniedHttpException(Yii::t('yii', 'You are not allowed to perform this action.'));
}
}
}

28
framework/yii/web/AccessDeniedHttpException.php

@ -0,0 +1,28 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\web;
/**
* AccessDeniedHttpException represents an "Access Denied" HTTP exception with status code 403.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class AccessDeniedHttpException extends HttpException
{
/**
* Constructor.
* @param string $message error message
* @param integer $code error code
* @param \Exception $previous The previous exception used for the exception chaining.
*/
public function __construct($message = null, $code = 0, \Exception $previous = null)
{
parent::__construct(403, $message, $code, $previous);
}
}

4
framework/yii/web/Application.php

@ -58,7 +58,7 @@ class Application extends \yii\base\Application
* 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
* @throws NotFoundHttpException if the requested route is invalid
*/
public function handleRequest($request)
{
@ -85,7 +85,7 @@ class Application extends \yii\base\Application
return $response;
}
} catch (InvalidRouteException $e) {
throw new HttpException(404, $e->getMessage(), $e->getCode(), $e);
throw new NotFoundHttpException($e->getMessage(), $e->getCode(), $e);
}
}

28
framework/yii/web/BadRequestHttpException.php

@ -0,0 +1,28 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\web;
/**
* BadRequestHttpException represents a "Bad Request" HTTP exception with status code 400.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class BadRequestHttpException extends HttpException
{
/**
* Constructor.
* @param string $message error message
* @param integer $code error code
* @param \Exception $previous The previous exception used for the exception chaining.
*/
public function __construct($message = null, $code = 0, \Exception $previous = null)
{
parent::__construct(400, $message, $code, $previous);
}
}

6
framework/yii/web/Controller.php

@ -62,7 +62,7 @@ class Controller extends \yii\base\Controller
} elseif (!is_array($params[$name])) {
$args[] = $actionParams[$name] = $params[$name];
} else {
throw new HttpException(400, Yii::t('yii', 'Invalid data received for parameter "{param}".', [
throw new BadRequestHttpException(Yii::t('yii', 'Invalid data received for parameter "{param}".', [
'param' => $name,
]));
}
@ -75,7 +75,7 @@ class Controller extends \yii\base\Controller
}
if (!empty($missing)) {
throw new HttpException(400, Yii::t('yii', 'Missing required parameters: {params}', [
throw new BadRequestHttpException(Yii::t('yii', 'Missing required parameters: {params}', [
'params' => implode(', ', $missing),
]));
}
@ -92,7 +92,7 @@ class Controller extends \yii\base\Controller
{
if (parent::beforeAction($action)) {
if ($this->enableCsrfValidation && Yii::$app->exception === null && !Yii::$app->getRequest()->validateCsrfToken()) {
throw new HttpException(400, Yii::t('yii', 'Unable to verify your data submission.'));
throw new BadRequestHttpException(Yii::t('yii', 'Unable to verify your data submission.'));
}
return true;
} else {

28
framework/yii/web/MethodNotAllowedHttpException.php

@ -0,0 +1,28 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\web;
/**
* MethodNotAllowedHttpException represents a "Method Not Allowed" HTTP exception with status code 405.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class MethodNotAllowedHttpException extends HttpException
{
/**
* Constructor.
* @param string $message error message
* @param integer $code error code
* @param \Exception $previous The previous exception used for the exception chaining.
*/
public function __construct($message = null, $code = 0, \Exception $previous = null)
{
parent::__construct(405, $message, $code, $previous);
}
}

28
framework/yii/web/NotFoundHttpException.php

@ -0,0 +1,28 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\web;
/**
* NotFoundHttpException represents a "Not Found" HTTP exception with status code 404.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class NotFoundHttpException extends HttpException
{
/**
* Constructor.
* @param string $message error message
* @param integer $code error code
* @param \Exception $previous The previous exception used for the exception chaining.
*/
public function __construct($message = null, $code = 0, \Exception $previous = null)
{
parent::__construct(404, $message, $code, $previous);
}
}

2
framework/yii/web/Request.php

@ -139,7 +139,7 @@ class Request extends \yii\base\Request
$_GET = array_merge($_GET, $params);
return [$route, $_GET];
} else {
throw new HttpException(404, Yii::t('yii', 'Page not found.'));
throw new NotFoundHttpException(Yii::t('yii', 'Page not found.'));
}
}

2
framework/yii/web/User.php

@ -331,7 +331,7 @@ class User extends Component
Yii::$app->getResponse()->redirect($this->loginUrl)->send();
exit();
} else {
throw new HttpException(403, Yii::t('yii', 'Login Required'));
throw new AccessDeniedHttpException(Yii::t('yii', 'Login Required'));
}
}

2
framework/yii/web/VerbFilter.php

@ -100,7 +100,7 @@ class VerbFilter extends Behavior
$event->isValid = false;
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.7
Yii::$app->getResponse()->getHeaders()->set('Allow', implode(', ', $allowed));
throw new HttpException(405, Yii::t('yii', 'Method Not Allowed. This url can only handle the following request methods: {methods}.', [
throw new MethodNotAllowedHttpException(Yii::t('yii', 'Method Not Allowed. This url can only handle the following request methods: {methods}.', [
'methods' => implode(', ', $allowed),
]));
}

Loading…
Cancel
Save