我正在做一个干净的架构项目,在这个项目中,我试图在我的主页上显示一个搜索栏,并包括一个API,添加我的搜索栏,我使用Symfony的表单包,所以在我的“域”存储库中,我有:“实体”;“表单”;“演示者”;“请求”;“响应”;和“案例”存储库;
在我的“Form”存储库中,我有我的搜索栏类:
namespace Domain\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
class SearchFormType implements AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add("car", TextType::class, [
"label" => "Rechercher une voiture",
"required" => true,
"attr" => [
"placeholder" => "Rechercher"
]
]);
}
}
在我的例子中,我有一个函数,必须包括表单信息的API,这个类还没有完成,但这不是问题所在:
使用案例:
namespace Domain\UseCase;
use Domain\Request\SearchCarRequest;
use Infrastructure\dataProviders\carsSearchApi;
use Symfony\Component\Form\FormInterface;
class SearchCarUseCase
{
private $model;
public function __construct()
{
}
public function searchCar(FormInterface $form, string $model)
{
$this->model = $model;
$carApi = new carsSearchApi($model);
}
public function validateModel(SearchCarRequest $request)
{
$request->model = $this->model;
}
}
我忘记了调用viewpage类和codeCase的控制器类:
namespace Infrastructure\Symfony\controller;
use Domain\Form\SearchFormType;
use Domain\Presenter\SearchCarPresenterInterface;
use Domain\UseCase\SearchCarUseCase;
use Infrastructure\Symfony\view\SearchHtmlView;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
#[Route("/", name: "main")]
class mainController extends AbstractController
{
public string $model;
private SearchHtmlView $searchView;
private SearchCarUseCase $carUseCase;
private SearchCarPresenterInterface $presenter;
public function __construct(
SearchHtmlView $searchView,
SearchCarUseCase $carUseCase,
SearchCarPresenterInterface $presenter
)
{
$this->searchView = $searchView;
$this->carUseCase = $carUseCase;
$this->presenter = $presenter;
}
/**
* @throws SyntaxError
* @throws RuntimeError
* @throws LoaderError
*/
public function __invoke(Request $carRequest): Response
{
$form = $this->createForm(SearchFormType::class);
$form->handleRequest($carRequest);
if ($form->isSubmitted() && $form->isValid()) {
$this->model = $form->get("car")->getData();
$this->carUseCase->searchCar($form, $this->model);
}
$this->addFlash("message", "veuillez remplir le champ demande");
return $this->searchView->generateView($form);
}
}
此类不在Domain文件夹中,而是在Infrastructure > Symfony > Controllers中。
最后,我有SearchHtmlView.php类,这个类检索所有信息并生成视图,还有一个问题:
namespace Infrastructure\Symfony\view;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Response;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
class SearchHtmlView
{
private Environment $twig;
private FormInterface $form;
public function __construct(Environment $twig, FormInterface $form)
{
$this->twig = $twig;
$this->form = $form;
}
/**
* @throws RuntimeError
* @throws SyntaxError
* @throws LoaderError
*/
public function generateView(FormInterface $form): Response
{
return new Response($this->twig->render(
'main/main.html.twig',
[
"form" => $this->form->createView()
]
));
}
}
PS:我知道我的WICKCase类还没有完成,API的实现也没有完成,但问题不在这里。
这就是我的页面显示:
无法自动连接服务“Infrastructure\Symfony\view\SearchHtmlView”:方法“__construct()”的参数“$form”引用接口“Symfony\Component\Form\FormInterface”,但不存在此类服务。您是否创建了实现此接口的类?
我尝试在我的formType类中做到这一点:
namespace Domain\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
abstract class SearchFormType implements FormInterface
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add("car", TextType::class, [
"label" => "Rechercher une voiture",
"required" => true,
"attr" => [
"placeholder" => "Rechercher"
]
]);
}
}
把它变成一个抽象类,实现FormInterface。但这不起作用,这是我在干净架构中的第一个项目,我不明白一切。
1条答案
按热度按时间yruzcnhs1#
注入一个
Symfony\Component\Form\FormFactory
,然后像这样使用它!