【问题标题】:Symfony Route. Can't set annotationsSymfony 路线。无法设置注释
【发布时间】:2018-09-18 17:39:32
【问题描述】:

我不明白,如果我用

创建 crud 控制器

bin/console make:crud 所有路由都从控制器工作

喜欢

/**
 * @Route("/", name="product_index", methods="GET")
 */

public function index(ProductRepository $productRepository): Response

{
    return $this->render('product/index.html.twig', ['products' => $productRepository->findAll()]);
}

.

如果我使用 bin/console make:controller 创建控制器

并自己定义带有注释的控制器,它们不起作用

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use JMS\Serializer\SerializerBuilder;
use Symfony\Component\HttpFoundation\JsonResponse;
use App\Entity\Product;
use App\Repository\ProductRepository;

use JMS\Serializer\SerializationContext;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;

class FirstApiController extends AbstractController
{
  /**
   *
   * @Route("/first_api", name="first_api")
   */
  public function index(ProductRepository $productRepository)
  {
      $data = $productRepository->findAll();
      $serializer = SerializerBuilder::create()->build();
#      $jsonContent = $serializer->serialize($data, 'json');

      $jsonContent = $serializer->serialize($data, 'json', SerializationContext::create()->setGroups(array('details')));


      $response = JsonResponse::fromJsonString($jsonContent);
      return $response;

  }

    /*
     * @Route("/first_api/send", name="send")
     *
     */
    public function send()
    {
        $a = "text";
        return $a;
    }

}

为什么这条路线不起作用

@Route("/first_api/send", name="send") ?

在 routes.yaml 中我什么也没写,只是空文件。

【问题讨论】:

  • 我认为你的问题已经解决了!

标签: symfony symfony-routing


【解决方案1】:

我使用了错误的语法!我使用了

/*  <-- the error is here
 * @Route("/first_api/send", name="send")
 *
 */

我需要使用

/**  <-- i nee two "*"
 * @Route("/first_api/send", name="send")
 *
 */
public 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-19
    相关资源
    最近更新 更多