symfony2中service的简单用法

针对一个简单的service的个人写法

1.首先需要在Bundle中建一个service文件夹。在文件夹中写一个service的php文件  如 bookService.php

symfony2中service的简单用法_第1张图片


2.配置service路由;

symfony2中service的简单用法_第2张图片


3.controller的处理

symfony2中service的简单用法_第3张图片

/**  * 删除地址  *  * @param string $receiverAddressId  *  */ public function deleteAddress($receiverAddressId){
    $repository = $this->entityManager->getRepository('AppBundle:ReceiverAddress');
    $receiverAddress = $repository->find($receiverAddressId);
    if (empty($receiverAddress) || $receiverAddress->getStatus() == 2) {
        throw new \Exception('该地址不存在或者已经被删除',BaseException::ERROR_CODE_NO_AUTHORITY);
    }

    $receiverAddress->setStatus(2);

    $this->entityManager->flush();
}
/**  * 增加地址  *  * @param string $receiverAddressId  * @param string $userId  *@param string $receiver  * @param string $address  * @param string $tel  * @param string $zipCode  * @reruen $receiverAddressId  */ public function addAddress($userId,$receiver,$address,$tel,$zipCode){
    $receiverAddress = new ReceiverAddress();
    $receiverAddress->setUserId($userId);
    $receiverAddress->setReceiver($receiver);
    $receiverAddress->setAddress($address);
    $receiverAddress->setTel($tel);
    $receiverAddress->setZipCode($zipCode);

    $this->entityManager->persist($receiverAddress);
    $this->entityManager->flush();

    $data = $receiverAddress->getId();
    return $data;
}
/**  * 修改地址  *  * @param string $receiverAddressId  * @param string $userId  *@param string $receiver  * @param string $address  * @param string $tel  * @param string $zipCode  *  */ public function modifyAddress($receiverAddressId,$userId,$receiver,$address,$tel,$zipCode){
    $receiverAddress = $this->entityManager->getRepository('AppBundle:ReceiverAddress')->find($receiverAddressId);
    if (empty($receiverAddress) || $receiverAddress->getStatus() == 2) {
        throw new \Exception('该地址不存在或者已经被删除',BaseException::ERROR_CODE_NO_AUTHORITY);
    }

    $receiverAddress->setUserId($userId);
    $receiverAddress->setReceiver($receiver);
    $receiverAddress->setAddress($address);
    $receiverAddress->setTel($tel);
    $receiverAddress->setZipCode($zipCode);

    $this->entityManager->flush();
}


4.service的文件书写



你可能感兴趣的:(symfony的学习)