在symfony2中创建服务

时间:2015-12-15 18:42:53

标签: php symfony service

我有两个非常相似的类来处理广告中的问题。目前,他们通过在网络上处理API和另一个来分开。我想创建一个服务,进行验证并保存银行并要求我返回任何密钥。

网络

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

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);
    }
}

然而,他是symfony的新手,我不确定从哪里开始,到目前为止我的是:

services.yml

services:
    questions_service:
        class:        Delivve\WebBundle\Services\QuestionsService
        arguments:    ["@doctrine.orm.entity_manager"]

类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;
        }
    }
}

这个想法是两个控制器会调用createQuestion函数,但不知道如何做到这一点,从我在参考文献中读到的内容必须打电话但没有做对,如果有人可以帮助我,谢谢你现在。

1 个答案:

答案 0 :(得分:1)

要调用服务,您只需从容器中获取服务即可。 如果你在控制器内,你可以做;

$this->get('questions_service');

所以要调用方法:

$this->get('questions_service')->createQuestion($id, $message);