使用相同的视图来编辑和查看 symfony [英] Using same view for editing and viewing symfony
问题描述
我想使用相同的视图进行编辑和查看.不幸的是,我无法编辑我的视图.在 symfony3 中,我认为没有办法索引表单.我尝试了所有可以做的事情,但我不知道如何使用相同的表格.在我的项目中,我还使用 JEE(直接链接到数据库)通过 UniRest API 与 symfony 进行通信.这是我的视图和编辑控制器:
/*** @Method({"GET"})*/公共功能 viewAction($id) {$phone = new Phone();$form = $this->createForm(PhoneType::class, $phone);$headers = array('Accept' => 'application/json');$response = Unirest\Request::get(link/phones/'.$id,$headers);//$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());return $this->render('AppBundle:Phone:PhoneView.html.twig', array ('形式' =>$form->createView(),'电话' =>$response->body,) );}/*** @Method({"PUT"})*/公共函数 updateAction(Request $request, $id) {$phone = new Phone();$form = $this->createForm(PhoneType::class, $phone);转储($request->getMethod());if ($request->isMethod('PUT')) {$form->handleRequest($request);$headers = array('Content-Type' => 'application/json');$data = json_encode($phone);$response = Unirest\Request::put('link/phones/'.$id,$headers,$data);//$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());转储($响应->代码);返回 $this->redirect($this->generateUrl('phones_list'));}$headers = array('Accept' => 'application/json');$response = Unirest\Request::get('link/phones/'.$id,$headers);//$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());转储($响应->代码);转储($response->body);return $this->render('AppBundle:Phone:PhoneUpdate.html.twig', array('形式'=>$form->createView(),'电话'=>$response->body,));
这是我的文件 PhoneView.html.twig
{% 扩展 "::base.html.twig" %}{% block title %}Accedant - {{ parent() }}{% endblock %}{% 块体 %}<form novalidate="novalidate" method = "get"><p><标签>颜色</label>:<input type="text" value= {{phone.color }}/><标签>价格</label>:<input type="text" value= {{ phone.price }}/><div class="form-group col-md-offset-5"><button type="submit" class="btn btn-default">保存</button>
</p></表单>{# 更新电话 #}<p class="left-center"><a href="{{ path('phones_update', {'id': phone.id}) }}" class="btn btn-mini btn-danger" class="btn btn-mini btn-danger">修改手机</a></p>{% 结束块 %}
这是我的PhoneUpdate.html.twig
{% 扩展 "::base.html.twig" %}{% 区块标题 %} 电话 - {{ parent() }}{% endblock %}{% 块体 %}<div class="容器">{{ form_start(form, {'method': 'PUT'}) }}<input type="hidden" name'_METHOD' value="PUT">{{ form_widget(form) }}<input type="submit" value="Sauvegarder" class="btn btn-default"/>{{ form_end(form) }}
{% 结束块 %}
这是我的文件 routing.yml
phones_view:路径:/phones/{id}默认值:{ _controller: AppBundle:Phone:view }方法:[GET]要求:编号:\d+电话更新:路径:/phones/{id}方法:[PUT]默认值:{ _controller: AppBundle:Phone:update }要求:编号:\d+
感谢您的帮助.
首先,我的 put 和 delete 方法没有配置.此外,我的问题是我认为路由文件 routing.yml 中的 put 方法与我的网络服务无关.所以我改变了我的路径以编辑我的表单 /update/{id},之后我得到了一个 404 错误,这是因为我在 J2EE 端的实现.
I'd like to use the same view for both editing and viewing. Unfortunatly, I cannot edit my view. In symfony3, I think there is no way to index a form. I tried every thing I could but I don't know how I can use the same form. In my project, I use also JEE (linked directly to data base) to communicate with symfony using UniRest API. Here are my view and edit controllers:
/**
* @Method({"GET"})
*/
public function viewAction($id) {
$phone = new Phone();
$form = $this->createForm(PhoneType::class, $phone);
$headers = array('Accept' => 'application/json');
$response = Unirest\Request::get(link/phones/'.$id,$headers);
//$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
return $this->render('AppBundle:Phone:PhoneView.html.twig', array (
'form' => $form->createView(),
'phone' => $response->body,
) );
}
/**
* @Method({"PUT"})
*/
public function updateAction(Request $request, $id) {
$phone = new Phone();
$form = $this->createForm(PhoneType::class, $phone);
dump($request->getMethod());
if ($request->isMethod('PUT')) {
$form->handleRequest($request);
$headers = array('Content-Type' => 'application/json');
$data = json_encode($phone);
$response = Unirest\Request::put('link/phones/'.$id,$headers,$data);
//$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
dump($response->code);
return $this->redirect($this->generateUrl('phones_list'));
}
$headers = array('Accept' => 'application/json');
$response = Unirest\Request::get('link/phones/'.$id,$headers);
//$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
dump($response->code);
dump($response->body);
return $this->render('AppBundle:Phone:PhoneUpdate.html.twig', array(
'form'=> $form->createView(),
'phone'=> $response->body,
));
And here is my file PhoneView.html.twig
{% extends "::base.html.twig" %}
{% block title %}Accedant - {{ parent() }}{% endblock %}
{% block body %}
<form novalidate="novalidate" method = "get">
<p>
<label> color </label> :<input type="text" value= {{ phone.color }} />
<label> price </label> :<input type="text" value= {{ phone.price }} />
<div class="form-group col-md-offset-5">
<button type="submit" class="btn btn-default">Save</button>
</div>
</p>
</form>
{# Updating Phone #}
<p class="left-center">
<a href="{{ path('phones_update', {'id': phone.id}) }}" class="btn btn-mini btn-danger" class="btn btn-mini btn-danger">
Modify phone
</a>
</p>
{% endblock %}
And here is my PhoneUpdate.html.twig
{% extends "::base.html.twig" %}
{% block title %} Phone - {{ parent() }}{% endblock %}
{% block body %}
<div class="container">
{{ form_start(form, {'method': 'PUT'}) }}
<input type="hidden" name'_METHOD' value="PUT">
{{ form_widget(form) }}
<input type="submit" value="Sauvegarder" class="btn btn-default" />
{{ form_end(form) }}
</div>
{% endblock %}
and here is my file routing.yml
phones_view:
path: /phones/{id}
defaults: { _controller: AppBundle:Phone:view }
methods: [GET]
requirements:
id: \d+
phones_update:
path: /phones/{id}
methods: [PUT]
defaults: { _controller: AppBundle:Phone:update }
requirements:
id: \d+
Thank you for your help.
First of all, my put and delete method was not configurated. Moreover, my problem was that I thought that the put method in my routing file routing.yml has no relation with my web services. So I change my path in order to edit my form /update/{id}, and after that I got a 404 error that was because of my implementation in J2EE side.
这篇关于使用相同的视图来编辑和查看 symfony的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!