簡體   English   中英

在symfony2中創建服務

[英]Create a service in symfony2

我有兩個非常相似的類來處理廣告中的問題。 目前,通過在Web上處理API和其他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函數,但不知道如何執行此操作,我從參考文獻中讀取的內容必須進行調用,但沒有正確執行,如果有人可以幫助我,請謝謝。

要調用服務,您只需要從容器中獲取它即可。 如果您在控制器內部,則可以執行此操作。

$this->get('questions_service');

因此,調用方法:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM