在我的synfony应用程序中,我有两个控制器:ActionController应该只是渲染一个包含表单的模板。提交表单,我想向SpreadSheetControllers getSpreadSheet()方法发送一个GET请求。
这是ActionController:
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
class ActionController extends AbstractController {
/**
* @Route("/action")
*/
public function action() {
$form = $this->createFormBuilder()
->setAction($this->generateUrl('/spreadSheet'))
->setMethod('GET')
->add('save', SubmitType::class, array('label' => 'Action'))
->getForm();
return $this->render('action.html.twig', array(
'form' => $form->createView(),
));
}
}
这是SpreadSheetController:
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
class SpreadSheetController extends AbstractController {
/**
* @Route("/spreadSheet")
*/
public function getSpreadSheet() {
return new Response(
'<html><body>spreadSheet</body></html>'
);
}
}
仍然,当浏览http://localhost:8000/action时,我得到一个RouteNotFoundException:无法生成指定路由“/ spreadSheet”的URL,因为这样的路由不存在。
有谁知道为什么没找到路线?
您必须引用路由的名称,而不是实际的URL。为此命名您的路线:
/**
* @Route("/spreadSheet", name="spreadsheet")
*/
然后在ActionController中引用该名称:
->setAction($this->generateUrl('spreadsheet'))