Silex有404页面

时间:2015-09-09 07:35:41

标签: php silex

我在controllers / Redirect / Myredierct.php中创建控制器 我有这样的代码

<?php

namespace MyApp\Controllers\Redirect;

use Silex\Application;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;


abstract class errorController 
{
    public function errorAction(Application $app)
    {
      $app->error(
        function(\Exception $e) use ($app) {
            if($e instanceof NotFoundHttpException && $e->getStatusCode() == 404) {
                return $this->app['twig']->render('error.html.twig');
            }
        }
    );}
}

我的问题是我必须把它包括在内?我无法理解我如何使用我的代码。请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

您的帖子中有两个问题:404状态以及如何使用抽象控制器。

首先,你的文件/类的命名有一个错误:你的类应该像文件一样命名。此外,我不知道它是强制性的还是仅仅是好的做法,但是你的文件应该总是以“Controller”结尾,所以我要做的是将你的文件重命名为“ErrorController.php”,将你的类重命名为“ErrorController”。

现在,您有一个抽象类,这意味着它将由另一个控制器通过继承使用。使用它的方法是创建一个像这样的新控制器:

<?php

namespace MyApp\Controllers;

class YourControllerName extends errorController {
     // Whatever you want
     public function whateverAction(Application $app)
     {
     // You can use you parent controller this way
     $this->errorAction($app);
          ...
     return $response;
     }
} 

希望这有帮助。