代码之家  ›  专栏  ›  技术社区  ›  Loctarogar

Symfony路线。无法设置批注

  •  1
  • Loctarogar  · 技术社区  · 6 年前

    我不明白,如果我用

    喜欢

    /**
     * @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") ?
    

    1 回复  |  直到 6 年前
        1
  •  2
  •   Loctarogar    6 年前

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

    我需要使用

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