Php Symfony控制器代码
嗨,我刚和Symfony开始了一个项目,我想我做错了什么。现在我想创建一个带有表和过滤器的简单页面,所以我为它创建了控制器Php Symfony控制器代码,php,symfony,Php,Symfony,嗨,我刚和Symfony开始了一个项目,我想我做错了什么。现在我想创建一个带有表和过滤器的简单页面,所以我为它创建了控制器 /** * @Route("/") */ class HomeController extends Controller { public function index(Request $request) { //Form to add new documents $form = $this->newForm();
/**
* @Route("/")
*/
class HomeController extends Controller {
public function index(Request $request) {
//Form to add new documents
$form = $this->newForm();
$form->handleRequest($request);
$user = $this->getDoctrine()->getRepository(User::class)->find($this->getUser());
//Gets all user documents
$files = $user->getDocuments();
//Gets all categories
$categories = $this->getDoctrine()->getRepository(Category::class)->findAll();
//Adds new document to database
if($form->isSubmitted() && $form->isValid()) {
$article = $form->getData();
$article->setUser($this->getUser());
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($article);
$entityManager->flush();
return $this->redirectToRoute('index');
}
return $this->render('home/home.html.twig', [
'files' => $files,
'categories' => $categories,
'form' => $form->createView(),
]);
}
}
此控制器仅显示表中的文档和导航中的类别。因此,为了添加类别过滤器,我刚刚创建了一个新函数:
/**
* @Route("/{categoryId}")
*/
public function categories(request $request, $categoryId)
{
$form = $this->newForm();
$form->handleRequest($request);
$user = $this->getDoctrine()->getRepository(User::class)->find($this->getUser());
$categories = $this->getDoctrine()->getRepository(Category::class)->findAll();
$category = $this->getDoctrine()->getRepository(Category::class)->findOneBy(["id" => $categoryId]);
$categoryFiles = $this->getDoctrine()->getRepository(Document::class)->categoryFiles($category, $user);
if($form->isSubmitted() && $form->isValid()) {
$article = $form->getData();
$article->setUser($this->getUser());
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($article);
$entityManager->flush();
return $this->redirectToRoute('index');
}
return $this->render('home/home.html.twig', [
'files' => $categoryFiles,
'categories' => $categories,
'form' => $form->createView(),
]);
}
一切正常,但我必须在所有函数中对类别和表单重复相同的代码。感谢您的帮助。创建抽象类,然后从子控制器进行扩展。你也可以阅读:
它只是一种方式,可以是更多的方式 我建议您使用服务,一项服务可以是(业务规则、助手、微服务等),最重要的是业务规则,通常应按实体存在一项服务
$user = $this->getUser();
$info = $this->get(HomeService::class)->getInfo($user, null);
// TODO FORM
return $this->render('home/home.html.twig', [
'files' => $info['files'],
'categories' => $info['categories'],
'form' => $form->createView()
]))
第二个动作
$user = $this->getUser();
$info = $this->get(HomeService::class)->getInfo($user, $categoryId);
// TODO FORM
return $this->render('home/home.html.twig', [
'files' => $info['category_files'],
'categories' => $info['categories'],
'form' => $form->createView()
]))
HomeService->info(),它将处理所有
使用S4?熟悉吗?您可以通过将功能移动到服务中,然后将服务注入控制器来减少控制器代码。我使用的是Symfony 4。是的,我熟悉服务,但不确定我是否应该在这里使用它。所以现在我只需要为我重用并导入的所有代码创建服务。谢谢,是的。一次吃一点。也许让自己成为一个CategoryFinder作为一个起点,只是为了熟悉这些机制。与后期静态绑定有什么联系?不,扩展的逻辑永远不应该在控制器中,这不是我说的通常的方式,这只是一种方式,可以创建新服务然后使用示例:$this->container->get(“new_service”)->getCategoryList();与后期静态绑定的联系是什么?你为什么要链接那个页面?我帮助他理解抽象方法,他需要从许多控制器调用一些方法,然后在子类中声明一些变量。