[英]Create a service in symfony2
I have two very similar classes that are treating questions in an ads. 我有两个非常相似的类来处理广告中的问题。 Currently they are separated by treating the API and the other on the web.
目前,通过在Web上处理API和其他API将它们分开。 I would like to create a service that would make the validation and save the bank and asks me to return any key.
我想创建一个可以进行验证并保存银行并要求我返回任何密钥的服务。
WEB 网页
public function createAction(Request $request, $adId)
{
$this->denyAccessUnlessGranted('ROLE_USER', null, 'Unable to access this page!');
$em = $this->getDoctrine()->getManager();
$ad = $em->getRepository('DelivveWebBundle:Ad')->findOneBy(array('id' => $adId));
$author = $this->getUser();
$question = new Question();
$question->setAd($ad);
$question->setAuthor($author);
$form_question = $this->createQuestionForm($question, $adId);
$form_question->handleRequest($request);
if ($form_question->isValid()) {
$em->persist($question);
$notification = new Notification();
$notification->setType(Constant::NOTIFY_QUESTION);
$notification->setTarget($question->getId());
$notification->setUser($ad->getOwner());
$notification->setAgent($author);
$em->persist($notification);
$em->flush();
return $this->redirect($this->generateUrl('show_ad', array('id' => $ad->getId())) . '#question' . $question->getId());
}
return $this->redirect($this->generateUrl('show_ad', array('id' => $ad->getId())));
}
API API
public function postAdQuestionAction(ParamFetcher $paramFetcher, $id)
{
/** @var EntityManager $em */
$em = $this->getDoctrine()->getManager();
/** @var Ad $ad */
$ad = $this->getDoctrine()->getRepository('DelivveWebBundle:Ad')->find($id);
/** @var User $author */
$author = $this->getUser();
if ($ad) {
if ($ad->getStatus() == Constant::AD_NEW) {
if (!$ad->getOwner()->isEqualTo($this->getUser())) {
$question = new Question();
$question->setAuthor($this->getUser());
$question->setAd($ad);
$question->setMessage($paramFetcher->get('message'));
$em->persist($question);
$notification = new Notification();
$notification->setType(Constant::NOTIFY_QUESTION);
$notification->setTarget($question->getId());
$notification->setUser($ad->getOwner());
$notification->setAgent($author);
$em->persist($notification);
$em->flush();
return $this->view(array('status' => 0, 'message' => null), 200);
} else {
return $this->view(array('status' => 3, 'message' => $this->get('translator')->trans('not_permitted')), 403);
}
} else {
return $this->view(array('status' => 2, 'message' => $this->get('translator')->trans('ad.closed')), 403);
}
}
else {
return $this->view(array('status' => 1, 'message' => $this->get('translator')->trans('ad.not_found')), 403);
}
}
However'm new to symfony and I'm not sure where to start, what I have so far is this: 但是,对于symfony来说不是新手,我不确定从哪里开始,到目前为止,我的工作是:
services.yml services.yml
services:
questions_service:
class: Delivve\WebBundle\Services\QuestionsService
arguments: ["@doctrine.orm.entity_manager"]
class QuestionsService 类QuestionsService
class QuestionsService extends Controller{
/**
* @var EntityManager
*/
protected $em;
public function __construct(EntityManager $entityManager){
$this->em = $entityManager;
}
public function createQuestion($id, $message){
/** @var Ad $ad */
$ad = $this->getDoctrine()->getRepository('DelivveWebBundle:Ad')->find($id);
/** @var User $author */
$author = $this->getUser();
if ($ad) {
if ($ad->getStatus() == Constant::AD_NEW) {
if (!$ad->getOwner()->isEqualTo($this->getUser())) {
$question = new Question();
$question->setAuthor($this->getUser());
$question->setAd($ad);
$question->setMessage($message);
$em->persist($question);
$notification = new Notification();
$notification->setType(Constant::NOTIFY_QUESTION);
$notification->setTarget($question->getId());
$notification->setUser($ad->getOwner());
$notification->setAgent($author);
$em->persist($notification);
$em->flush();
return 0;
} else {
return 3;
}
} else {
return 2;
}
}
else {
return 1;
}
}
}
The idea is that the two controller would call createQuestion function, but do not know how to do this, from what I read in the references have to make a call but did not get it right, if anyone can help me, thank you now. 我的想法是,这两个控制器将调用createQuestion函数,但不知道如何执行此操作,我从参考文献中读取的内容必须进行调用,但没有正确执行,如果有人可以帮助我,请谢谢。
To call a service you only need to get it from the container. 要调用服务,您只需要从容器中获取它即可。 If you're inside a controller you can do;
如果您在控制器内部,则可以执行此操作。
$this->get('questions_service');
So for calling a method: 因此,调用方法:
$this->get('questions_service')->createQuestion($id, $message);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.