【发布时间】: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 中我什么也没写,只是空文件。
【问题讨论】:
-
我认为你的问题已经解决了!