如何使用我的服务将联系人导出到CSV? [英] How to export contact to CSV with my service?

查看:301
本文介绍了如何使用我的服务将联系人导出到CSV?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个symfony2服务将数据库导出为CSV文件,使其可用。

I have a symfony2 service to export a database into a CSV file making it availaible to the.

我遇到此错误:

The controller must return a response ( null given ) . 

Did you forget to add a return statement somewhere in your controller?

我不明白为什么。你能告诉我这个吗?

I do not understand why. Could you enlighten me on this?

我的代码:

    namespace Rac\CaraBundle\Manager;

/* Imports */

use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\Validator\ValidatorInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\HttpFoundation\StreamedResponse;

/* Local Imports */
use Rac\CaraBundle\Entity\Contact;

/**
 * Class CSV Contact Importer
 *
 */
class CSVContactImporterManager {

    /**
     * @var ObjectManager
     */
    private $om;

    /**
     * @var EventDispatcherInterface
     */
    private $eventDispatcher;

    /**
     * @var ValidatorInterface
     */
    private $validator;

    /**
     * @var ContactManager
     */
    private $contactManager;


    /**
     * @param EventDispatcherInterface $eventDispatcher
     * @param ObjectManager            $om
     * @param Contact                  $contactManager
     *
     */
    public function __construct(
    EventDispatcherInterface $eventDispatcher, ObjectManager $om, ValidatorInterface $validator, ContactManager $contactManager
    ) {
        $this->eventDispatcher = $eventDispatcher;
        $this->om = $om;
        $this->validator = $validator;
        $this->contactManager = $contactManager;
    }
    public function getExportToCSVResponse() {
        // get the service container to pass to the closure
        $contactList = $this->contactManager->findAll();
        $response = new StreamedResponse();
        $response->setCallback(
            function () use ($contactList) {
            //Import all contacts
            $handle = fopen('php://output', 'r+');
            // Add a row with the names of the columns for the CSV file
            fputcsv($handle, array('Nom', 'Prénom', 'Société', 'Position', 'Email', 'Adresse', 'Téléphone', 'Téléphone mobile'), "\t");
            $header = array();
            //print_r($contactList);
            foreach ($contactList as $row) {
                fputcsv($handle, array(
                    $row->getFirstName(),
                    $row->getLastName(),
                    $row->getCompany(),
                    $row->getPosition(),
                    $row->getEmail(),
                    $row->getAddress(),
                    $row->getPhone(),
                    $row->getMobile(),
                    ), "\t");
            }
            fclose($handle);
        }
        );
        $response->headers->set('Content-Type', 'application/force-download');
        $response->headers->set('Content-Disposition', 'attachment; filename="export.csv"');

        return $response;
    }

我的控制器:

use Rac\CaraBundle\Entity\Contact;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use UCS\Bundle\RichUIBundle\Controller\BaseController;
use UCS\Bundle\RichUIBundle\Serializer\AbstractListSerializer;

/**
 * Contact BackOffice Environment Controller.
 *
 *
 *
 * @Route("/contact_environment")
 */
class ContactEnvironmentController extends BaseController{
    /* My code here..*/


   /**
     * @Route("/export", name="contact_environment_export",options={"expose"=true})
     * @Method("GET")
     *
     * @return type
     */
    public function exort(){
        $manager = $this->get("cara.csv_contact_importer_manager");
        $manager->getExportToCSVResponse();

    }
}


推荐答案

在您的控制器中,您需要返回 getExportToCSVResponse()方法中的值。

In your controller you need to return the value from your getExportToCSVResponse() method.

public function exort(){
    $manager = $this->get("cara.csv_contact_importer_manager");
    return $manager->getExportToCSVResponse();
}

这篇关于如何使用我的服务将联系人导出到CSV?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆