Symfony,twig传输数据

时间:2015-07-29 07:44:18

标签: php symfony routing twig

在symfony我有树枝,我想传输数据以便采取行动,但在网址中我不想看到'角色',只传输动作,这是怎么做到的? 路由:

artel_admin_index:
path:     /{ida}/edit/{id}/submit
defaults: { _controller: ArtelProfileBundle:Dashboard:edit }
requirements: { _method: POST|GET }

树枝:

<td>
<a href="{{ path('artel_admin_index', {'ida': user.id, 'id': developer.id, 'role': user.role }) }}"> {{ developer.firstname }} {{ developer.lastname }}</a>
</td>

动作:

    public function editAction($ida, $id)
{
    $request = $this->get('request');
    $value = $request->getSession()->get('role');
    dump($request, $value);exit;

我明白了:

DashboardController.php on line 138:
Request {#7 ▼
+attributes: ParameterBag {#10 ▶}
+request: ParameterBag {#8 ▶}
+query: ParameterBag {#9 ▶}
+server: ServerBag {#13 ▶}
+files: FileBag {#12 ▶}
+cookies: ParameterBag {#11 ▶}
+headers: HeaderBag {#14 ▶}
#content: null
#languages: null
#charsets: null
#encodings: null
#acceptableContentTypes: null
#pathInfo: "/39/edit/116/submit"
#requestUri: "/app_dev.php/39/edit/116/submit"
#baseUrl: "/app_dev.php"
#basePath: null
#method: "GET"
#format: null
#session: Session {#151 ▶}
#locale: null
#defaultLocale: "en"
}

DashboardController.php on line 138:
"ROLE_COMPANY"

我尝试传输$ role indexAction - &gt;我设置的indexAction中的editAction:

public function indexAction($username)
{
    $user_role = $user->getRoles();
    $request->getSession()->set('role', $user_role[0]);

并且在表格中我有动作锄头在这个动作中发送这个$角色但是这个角色没有添加路由:

<td>
  <a href="{{ path('artel_admin_index', {'ida': user.id, 'id': developer.id}) }}"> {{ developer.firstname }} {{ developer.lastname }}</a>
</td>
indexAction中的

    $user_role = $user->getRoles();
    $request->getSession()->set('role', $user_role[0]);
    $role = $request->getSession()->get('role');
    dump($user_role, $role);exit;

我看到了

UserProfileController.php on line 69:
array:1 [▼
 0 => "ROLE_COMPANY"
]

UserProfileController.php on line 69:
"ROLE_COMPANY"

现在在索引操作中我在模板中渲染?因为现在在indexAction中我渲染了一些数据并且在模板中我有

1 个答案:

答案 0 :(得分:1)

如果要将变量从视图传递到接收控制器,则应使用POST方法。

您可以实现此目的,例如,提交表单。

如果您在执行上一个action时知道此数据,则可以在会话中存储此信息:

$request->getSession()->set('VARIABLE_NAME', $value);

然后通过以下方式取回:

$value = $request->getSession()->get('VARIABLE_NAME');