Symfony,树枝传输数据
Symfony, twig transmit data
在 symfony 中,我有 twig,我想传输数据以进行操作,但在 url 中,我不想看到 'role',只传输操作数据,这是怎么做到的?
路由:
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"
我尝试在我设置的 indexAction 中传输 $role indexAction -> editAction:
public function indexAction($username)
{
$user_role = $user->getRoles();
$request->getSession()->set('role', $user_role[0]);
并且在表格中我在这个动作中有动作 hoe 传输这个 $role 但这个角色没有添加到路由中:
<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 中我渲染了一些数据并且在模板中我有
如果你想将变量从视图传递到接收控制器,你应该使用 POST
方法。
您可以实现这一点,例如提交表单。
如果你在执行前面的action
时知道这个数据,那么你可以将这个信息存储在会话中:
$request->getSession()->set('VARIABLE_NAME', $value);
然后通过以下方式取回:
$value = $request->getSession()->get('VARIABLE_NAME');
在 symfony 中,我有 twig,我想传输数据以进行操作,但在 url 中,我不想看到 'role',只传输操作数据,这是怎么做到的? 路由:
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"
我尝试在我设置的 indexAction 中传输 $role indexAction -> editAction:
public function indexAction($username)
{
$user_role = $user->getRoles();
$request->getSession()->set('role', $user_role[0]);
并且在表格中我在这个动作中有动作 hoe 传输这个 $role 但这个角色没有添加到路由中:
<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 中我渲染了一些数据并且在模板中我有
如果你想将变量从视图传递到接收控制器,你应该使用 POST
方法。
您可以实现这一点,例如提交表单。
如果你在执行前面的action
时知道这个数据,那么你可以将这个信息存储在会话中:
$request->getSession()->set('VARIABLE_NAME', $value);
然后通过以下方式取回:
$value = $request->getSession()->get('VARIABLE_NAME');