1 回答

TA貢獻(xiàn)1852條經(jīng)驗 獲得超7個贊
我自己想出了解決方案:D。我不知道我的函數(shù)中需要 Request,現(xiàn)在也知道它的作用。
javascript 保持不變,控制器現(xiàn)在看起來像這樣:
<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use AppBundle\Entity\User as User;
use Appbundle\Entity\Category as Category;
class HomePageController extends Controller
{
/**
* @Route("/", name="homepage")
*/
public function getData() {
//...
}
/**
* @Route("/create-category", name="newCat")
*/
public function createCategory(Request $request) {
// dump($request);
// dump($request->request->get('output'));
$output = $request->request->get('output');
$category = $this->getDoctrine()
->getRepository('AppBundle:Category')
->findall();
$category = new Category();
$category->setTitle($output);
$em = $this->getDoctrine()->getEntityManager();
$em->persist($category);
$em->flush();
return new Response('Created product id '.$category->getId());
}
}
我更改/添加的內(nèi)容:
//Added Request $request
public function createCategory(Request $request) {
//Added line for getting the output of ajax/json post.
$output = $request->request->get('output');
}
添加回答
舉報