我想在编辑和查看时使用相同的视图。不幸的是,我无法编辑我的观点。在symfony3中,我认为没有办法索引表单。我尽我所能,但我不知道如何使用相同的表格。在我的项目中,我还使用JEE(直接链接到数据库)使用UniRest API与symfony进行通信。这是我的视图和编辑控制器:
/**
* @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,
));
这是我的文件 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 %}
这是我的 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 %}
这是我的文件 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+
感谢您的帮助。
答案 0 :(得分:0)
首先,我的 put 和删除方法未配置。此外,我的问题是我认为我的路由文件 routing.yml 中的 put 方法与我的Web服务没有关系。所以我改变了我的路径以编辑我的表单 / update / {id} ,之后我得到了404错误,这是因为我在J2EE方面的实现。