Symfony路线。无法设置注释

时间:2018-09-18 17:39:32

标签: symfony symfony-routing

我不明白,如果我用

创建Crud控制器

bin / console make:从控制器中提取所有路由

喜欢

/**
 * @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中,我什么也没写,只是空文件。

1 个答案:

答案 0 :(得分:2)

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

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

我需要使用

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