浏览代码

[FrameworkBundle] Adding a shortcut method to the controller for throwing the NotFoundHttpException

The rationale is that this is a very common task and we can't expect non-advanced users to have to remember what the fully-qualified
class name of the Exception is in order to use it.
Ryan Weaver 14 年之前
父节点
当前提交
ad80966d83
共有 1 个文件被更改,包括 15 次插入0 次删除
  1. 15 0
      src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php

+ 15 - 0
src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php

@@ -14,6 +14,7 @@ namespace Symfony\Bundle\FrameworkBundle\Controller;
 use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\HttpFoundation\RedirectResponse;
 use Symfony\Component\DependencyInjection\ContainerAware;
+use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
 
 /**
  * Controller is a simple implementation of a Controller.
@@ -92,6 +93,20 @@ class Controller extends ContainerAware
         return $this->container->get('templating')->renderResponse($view, $parameters, $response);
     }
 
+    /**
+     * Returns a NotFoundHttpException.
+     *
+     * This will result in a 404 response code. Usage example:
+     *
+     *     throw $this->createNotFoundException('Page not found!');
+     *
+     * @return NotFoundHttpException
+     */
+    public function createNotFoundException($message = 'Not Found', \Exception $previous = null)
+    {
+        return new NotFoundHttpException($message, $previous);
+    }
+
     /**
      * Returns true if the service id is defined.
      *