将一个功能拆分为两个

问题描述 投票:0回答:1

为了可重用性,我计划将我的功能分成两个功能。基本上,函数的概念是加载输入文件然后预览数据。

工作守则

public function uploadImportCsv()
    {
        $file = Input::file('file');
        $extension = $file->getClientOriginalExtension();
        $filename = sha1($file->getClientOriginalName().time()) . ".{$extension}";
        //upload to s3
        #doing upload to s3

        $data = [
            'title'=>[],
            'value'=>[]
            ];
    $results = Excel::load(Input::file('file'), function($reader){

            })->get();
    foreach ($results as $result) {
        foreach ($result as $key => $value) {
            if(!in_array($key, $data['title'])){
                array_push($data['title'], $key);
            }       
        }
        array_push($data['value'], $result);
    }

        return Response::json(['filename' => $filename, 'data' => $data]);
    }

分手后

public function previewCsv()
    {
        //Preview table
        $data = [
                'title'=>[],
                'value'=>[]
                ];
        $results = Excel::load(Input::file('file'), function($reader){

                })->get();
        foreach ($results as $result) {
            foreach ($result as $key => $value) {
                if(!in_array($key, $data['title'])){
                    array_push($data['title'], $key);
                }       
            }
            array_push($data['value'], $result);
        }
        return Response::json(['data' => $data]);
    }

    public function uploadImportCsv()
    {
        $file = Input::file('file');
        $extension = $file->getClientOriginalExtension();
        $filename = sha1($file->getClientOriginalName().time()) . ".{$extension}";
        //upload to s3
        #doing upload to s3

        $data = $this->previewCsv();


        return Response::json(['filename' => $filename,'data' => $data]);
    }

我从预览功能调用了该函数,但它不起作用。

php laravel-4
1个回答
0
投票

如果需要相同的结果,则无需从previewCsv函数返回json对象。

public function previewCsv()
{
    ....
    return $data;
}
© www.soinside.com 2019 - 2024. All rights reserved.