Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener::guessTemplateName PHP Method

guessTemplateName() protected method

Guesses and returns the template name to render based on the controller and action names.
protected guessTemplateName ( array $controller, Request $request ) : TemplateReference
$controller array An array storing the controller object and action method
$request Symfony\Component\HttpFoundation\Request A Request instance
return Symfony\Bundle\FrameworkBundle\Templating\TemplateReference template reference
    protected function guessTemplateName($controller, Request $request)
    {
        if (!preg_match('/Controller\\\(.+)Controller$/', get_class($controller[0]), $matchController)) {
            throw new \InvalidArgumentException(sprintf('The "%s" class does not look like a controller class (it must be in a "Controller" sub-namespace and the class name must end with "Controller")', get_class($controller[0])));

        }

        if (!preg_match('/^(.+)Action$/', $controller[1], $matchAction)) {
            throw new \InvalidArgumentException(sprintf('The "%s" method does not look like an action method (it does not end with Action)', $controller[1]));
        }

        $bundle = $this->getBundleForClass(get_class($controller[0]));

        return new TemplateReference($bundle->getName(), $matchController[1], $matchAction[1], $request->getRequestFormat(), 'twig');
    }

Usage Example

Ejemplo n.º 1
0
 protected function guessTemplateName($controller, Request $request, $engine = 'twig')
 {
     $controllerClass = get_class($controller[0]);
     $userClass = ClassUtils::getUserClass($controllerClass);
     if ($controllerClass === $userClass) {
         return parent::guessTemplateName($controller, $request, $engine);
     }
     if (!preg_match('/Controller\\\\(.+)Controller$/', $userClass, $matchController)) {
         throw new \InvalidArgumentException(sprintf('The "%s" class does not look like a controller class (it must be in a "Controller" sub-namespace and the class name must end with "Controller")', $userClass));
     }
     if (!preg_match('/^(.+)Action$/', $controller[1], $matchAction)) {
         throw new \InvalidArgumentException(sprintf('The "%s" method does not look like an action method (it does not end with Action)', $controller[1]));
     }
     $bundle = $this->getBundleForClass($userClass);
     return new TemplateReference($bundle->getName(), $matchController[1], $matchAction[1], $request->getRequestFormat(), $engine);
 }