enter image description here變量「url_resources」不存在。在這裏,我試圖顯示剛剛輸入的表單的值。它總是給我帶來同樣的錯誤。我希望它能顯示在同一頁面上,而不會導航到不同的頁面。我已經創建了3個文本框來顯示相應的文件,正如您在代碼中看到的那樣。變量「url_resources」不存在。這裏我試圖顯示剛纔輸入的表格的值
控制器
<?php
namespace UrlResourceBundle\Controller;
use UrlResourceBundle\Entity\url_resources;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
class DefaultController extends Controller
{
/**
* @Route("/", name="todo_list")
*/
public function listAction()
{
$urls_resources = $this->getDoctrine()
->getRepository('UrlResourceBundle:url_resources')
->findAll();
return $this->render('todo/index.html.twig', array(
'urls_resources' => $urls_resources
));
}
/**
* @Route("/todo/create", name="todo_create")
*/
public function createAction(Request $request)
{
$url_resources = new url_resources;
$form = $this->createFormBuilder($url_resources)
->add('title', TextType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
->add('url', TextType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
->add('description', TextType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
->add('save', SubmitType::class, array('label' => "Create",'attr' => array('class' => 'btn btn-primary', 'style' => 'margin-bottom:15px')))
->getForm();
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid())
{
//Get Data
$title = $form['title']->getData();
$url = $form['url']->getData();
$description = $form['description']->getData();
$url_resources->setTitle($title);
$url_resources->setUrl($url);
$url_resources->setDescription($description);
$em = $this->getDoctrine()->getManager();
$em->persist($url_resources);
$em->flush();
$this->addFlash(
'notice',
'Entry Created'
);
return $this->redirectToRoute('todo_create');
return $this->redirect($request->getUri());
}
return $this->render('todo/create.html.twig', array(
'form' => $form->createView()
));
}
/**
* @Route("/todo/edit/{id}", name="todo_edit")
*/
public function editAction($id, Request $request)
{
$url_resources = $this->getDoctrine()
->getRepository('UrlResourceBundle:url_resources')
->find($id);
$url_resources->setTitle($url_resources->getTitle());
$url_resources->setUrl($url_resources->getUrl());
$url_resources->setDescription($url_resources->getDescription());
$form = $this->createFormBuilder($url_resources)
->add('title', TextType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
->add('url', TextType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
->add('description', TextareaType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
->add('save', SubmitType::class, array('label' => "Update",'attr' => array('class' => 'btn btn-primary', 'style' => 'margin-bottom:15px')))
->getForm();
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid())
{
//Get Data
$title = $form['title']->getData();
$url = $form['url']->getData();
$description = $form['description']->getData();
$em = $this->getDoctrine()->getManager();
$url_resources = $em->getRepository('UrlResourceBundle:url_resources')->find($id);
$url_resources->setTitle($title);
$url_resources->setUrl($url);
$url_resources->setDescription($description);
$em->flush();
$this->addFlash(
'notice',
'Entry Updated'
);
return $this->redirectToRoute('todo_list');
}
return $this->render('todo/edit.html.twig', array(
'url_resources' => $url_resources,
'form' => $form->createView()
));
}
/**
* @Route("/todo/details/{id}", name="todo_details")
*/
public function detailsAction($id)
{
$url_resources = $this->getDoctrine()
->getRepository('UrlResourceBundle:url_resources')
->find($id);
return $this->render('todo/details.html.twig', array(
'url_resources' => $url_resources
));
}
/**
* @Route("/todo/delete/{id}", name="todo_delete")
*/
public function deleteAction($id)
{
$em = $this->getDoctrine()->getManager();
$url_resources = $em->getRepository('UrlResourceBundle:url_resources')->find($id);
$em->remove($url_resources);
$em->flush();
$this->addFlash(
'notice',
'Entry deleted');
return $this->redirectToRoute('todo_list');
}
}
嫩枝
{% extends 'base.html.twig' %}
{% block body %}
<h2 class="page-header">Create</h2>
{{form_start(form)}}
{{form_widget(form)}}
<ul class="list-group">
<li class="list-group-item">Title: {{url_resources.title}}</li>
<li class="list-group-item">URL: {{url_resources.url}}</li>
<li class="list-group-item">Description: {{url_resources.description}}</li>
</ul>
{{form_end(form)}}
{% endblock %}
你能給出確切的錯誤信息嗎?我們不知道它在哪裏發生 –
我已添加錯誤的屏幕截圖...請參閱問題的頂部..謝謝... – jcoder