简体   繁体   English

Symfony 渲染控制器以获取树枝文件中的数据

[英]Symfony render controller to get data in twig file

I'm a bit new to symfony (3.2.1), and I would like to have a controller for each twig file controlling my views to keep my controllers thin as indicated in Symfony Best Pratcices.我对 symfony (3.2.1) 有点陌生,我希望每个 twig 文件都有一个控制器来控制我的视图,以保持我的控制器薄,如 Symfony 最佳实践中所述。

In my page, I call在我的页面中,我调用

{% include "my_bundle/user.html.twig" %}

to have a sidebar with all user info, login, etc..有一个包含所有用户信息、登录名等的侧边栏。

For user.html.twig I would like to have a separate controller, so I created UserController.php.对于 user.html.twig 我想要一个单独的控制器,所以我创建了 UserController.php。

Seems like the right way to do it, no ?似乎是正确的方法,不是吗?

For now I'm doing a real basic request but can not seem to make it work, though I thought I did everything right.现在我正在做一个真正的基本请求,但似乎无法使其工作,尽管我认为我做的一切都是正确的。

So src/AppBundle/Controller/UserController.php looks like :所以 src/AppBundle/Controller/UserController.php 看起来像:

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class UserController extends Controller
{
    public function UserIdAction(Request $request)
    {   
        $userID = '2';

        return $this->render('my_bundle:user.html.twig', array('userID' => $userID));
    }
}

and app/Resources/views/my_bundle/user.html.twig looks like :和 app/Resources/views/my_bundle/user.html.twig 看起来像:

<div id="userSideBar">
    <div class="container">
        {{ render(controller('AppBundle:Controller:UserId')) }}
        <p>{{ userID }}</p>
    </div>
</div>

Doing that I get error :这样做我得到错误:

An exception has been thrown during the rendering of a template ("The _controller value "AppBundle:Controller:UserController" maps to a "AppBundle\\Controller\\ControllerController" class, but this class was not found. Create this class or check the spelling of the class and its namespace.").模板渲染时抛出异常("The _controller value "AppBundle:Controller:UserController" 映射到一个 "AppBundle\\Controller\\ControllerController" 类,但是没有找到这个类。创建这个类或者检查拼写类及其命名空间。”)。

I also tried app/Resources/views/my_bundle/user.html.twig like :我也试过 app/Resources/views/my_bundle/user.html.twig 像:

<div id="userSideBar">
    <div class="container">
        {{ render(controller('AppBundle:Controller:UserId', { 'userID': userID })) }}
    </div>
</div>

But then I get error :但后来我得到错误:

Variable "userID" does not exist.变量“userID”不存在。

Any idea on where I could be wrong please ?请知道我可能出错的地方吗?

EDIT :编辑 :

I may have missed an important part here :我可能错过了这里的一个重要部分:

To include the controller, you'll need to refer to it using the standard string syntax for controllers (ie bundle:controller:action)要包含控制器,您需要使用控制器的标准字符串语法(即 bundle:controller:action)来引用它

So I changed app/Resources/views/my_bundle/user.html.twig to :所以我将 app/Resources/views/my_bundle/user.html.twig 更改为:

<div id="userSideBar">
    <div class="container">
        {{ render(controller('AppBundle:User:UserId')) }}
    </div>
</div>

But now I get :但现在我得到:

PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 8388608 bytes) PHP 致命错误:允许的内存大小为 134217728 字节已用完(尝试分配 8388608 字节)

and server stops running, really confusing.和服务器停止运行,真的很混乱。

I had trouble likes this.我有这样的麻烦。 When i try to render controller in twig, PHP STORM was show wrong path.当我尝试在树枝中渲染控制器时,PHP STORM 显示错误的路径。 As my template was not in root folder Controller it light me "AdminBundle:Client\\Companies:clientsWidget" but I was need change slash "AdminBundle:Client/Companies:clientsWidget"由于我的模板不在根文件夹 Controller 中,它点亮了我"AdminBundle:Client\\Companies:clientsWidget"但我需要更改斜杠"AdminBundle:Client/Companies:clientsWidget"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM