扩展Symfony2控制器

use*_*356 0 symfony

这作为普通控制器工作正常:

namespace BundleName\Bundle\SiteBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DefaultController extends Controller
{
    public function indexAction()
    {

        return $this->render('MyBundle:Default:index.html.twig', array("abc" => "test"));

    }

}
Run Code Online (Sandbox Code Playgroud)

...所以肯定通过这样做它应该只是扩展控制器:

namespace BundleName\Bundle\SiteBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class ControllerExtension extends Controller
{
    public function render(string $view, array $parameters = array(), Response $response = null)
    {

        return parent::render($view, $parameters, $response);

    }

}

class DefaultController extends ControllerExtension
{
    public function indexAction()
    {

        return $this->render('MyBundle:Default:index.html.twig', array("abc" => "test"));

    }

}
Run Code Online (Sandbox Code Playgroud)

..但我得到这个错误:

运行时注意:声明... ControllerExtension :: render()应该与Symfony\Bundle\FrameworkBundle\Controller\Controller :: render()的声明兼容... Bundle/SiteBundle/Controller/DefaultController.php

添加这个没有区别(这是我在某处看到的修复):

use Symfony\Component\HttpFoundation\Response 
Run Code Online (Sandbox Code Playgroud)

Wou*_*r J 6

PHP是一种懒惰的语言.您不能键入字符串,整数或布尔值,只能键入数组和类名.

因此,为了获得工作函数并纠正PHP,您应该这样做:

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response; //! important as @Inori said

class ControllerExtension extends Controller
{
    public function render($view, array $parameters = array(), Response $response = null)
    {
        return parent::render($view, $parameters, $response);
    }
}
Run Code Online (Sandbox Code Playgroud)