【问题标题】:symfony 4.1 Unable to generate a URLsymfony 4.1 无法生成 URL
【发布时间】:2018-09-05 22:41:55
【问题描述】:

在我的 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,因为这样的路由不存在。

有人知道为什么找不到路线吗??

【问题讨论】:

    标签: controller routes symfony4


    【解决方案1】:

    你必须引用路由的 name,而不是实际的 url。像这样命名您的路线:

    /**
     * @Route("/spreadSheet", name="spreadsheet")
     */
    

    然后在 ActionController 中引用该名称:

        ->setAction($this->generateUrl('spreadsheet'))
    

    【讨论】:

    • 乐于帮助@Kevin!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-17
    • 1970-01-01
    • 2023-03-28
    相关资源
    最近更新 更多