从Laravel 5中的模型返回视图

时间:2015-10-27 16:23:39

标签: php html laravel-5

我的Controller创建了一个类SoapWrapper.php的实例,其中包含一个从Web服务检索某些数据的函数。由于Web服务只允许您为每个查询收集100条记录,因此我查找查询的记录总数,然后循环并每100条记录调用Web服务查询以获取所有数据。

要向用户提供正在处理请求的某种反馈(各种加载屏幕),我想返回View,其中显示HTML框,其中包含一些文字说Loading record x of y,其中y是记录总数,x在每次循环迭代时更新。

以下是当前ModelSoapWrapper.php

<?php namespace App\Models;
use SoapClient;
use Illuminate\Http\RedirectResponse;
use Redirect;
class SoapWrapper {
    // SOAP exchange for WoS authentication
    public $auth_client;
    public $auth_response;
    // SOAP exchange for WoS search
    public $search_client;
    public $search_response;
    // number of records found
    public $len;
    // XML data to send as SOAP Request to WoS
    public $data;
    // array to store records
    public $records = [];

    // function to iterate and store all relevant data returned from SOAP exchange
    public function iterateWosSearch($submit) {
        // variable to store average time/record retrieval (based on calculations)
        $avg = 0.08;
        // create a variable to store time for loading screen
        $timeDecimal = (round((($submit->len)*$avg), 2));
        // create an array to represent citation values to ignore, i.e. not interested
        // in any publications with less than 4 citations
        $ignore = array(0, 1, 2, 3);
        // create a counter variable to use for progress bar
        $counter = 1;
        // turn time into readable format (mins & secs, rather than just secs)
        if ($timeDecimal > 59.99) {
            $minutes = round(($timeDecimal/60), 0, PHP_ROUND_HALF_DOWN);
            while ($timeDecimal > 59.99) {
                $timeDecimal -= 60;
                $seconds = round($timeDecimal, 0);
            };
        } else {
            $minutes = 0;
            $seconds = round($timeDecimal, 0);
        };
        // iterate through all records, perform search for each 100 records (max per call)
        // and tabulate data
        for ($i = 1; $i <= ($submit->len); $i+=100) {
            // set search parameters for current iteration (first record = 1, 101, 201, 301 etc.)
            $submit->data['retrieveParameters']['firstRecord'] = $i;
            // gather search response for current iteration
            try {
                $submit->search_response = $submit->search_client->search($submit->data);
            } catch (Exception $e) {  
                echo $e->getMessage(); 
            };
            // turn Soap Client object from current response into XML element
            $xml = simplexml_load_string($submit->search_response->return->records);
            // save variable names for citations, country and publication year
            $citations = "";
            $pubyear = "";
            $country = "";
            // iterate through current data set and store to $records array
            foreach($xml->REC as $record) {
                // create authors array for this REC data
                $authors = [];
                return View::make('loading', array('minutes' => $minutes, 'seconds' => $seconds, 'counter' => $counter));
                // authors
                foreach($record->static_data->summary->names->name as $thisAuthor) {
                    array_push($authors, $thisAuthor->full_name);
                }
                // country (if exists)
                if (isset($record->static_data->item->reprint_contact->address_spec->country)) {
                    $country = (string)$record->static_data->item->reprint_contact->address_spec->country;
                } else {
                    $country = "";
                };

                // set current publication year
                $pubyear = (string)$record->static_data->summary->pub_info->attributes()->pubyear;
                // number of citations, if 0-3 ($ignore array) then 'break' out of loop entirely
                if (!in_array($record->dynamic_data->citation_related->tc_list->silo_tc->attributes(), $ignore)) {
                $citations = (string)$record->dynamic_data->citation_related->tc_list->silo_tc->attributes();
                } else {
                    // break from both loops
                    break 2;
                };
                // for this iteration map all the values recorded into a temporary array variable,
                // $aRecord (equivalent to one row of data in table)
                $arecord = [    
                                "authors"   => $authors,
                                "ID"        => "",
                                "pubyear"   => $pubyear,
                                "country"   => $country,
                                "citations" => $citations
                           ];
                // pass the data from this iteration into the array variable '$records',
                // after all iterations, each element in $records will be a single
                // record or row of data for a single journal
                array_push($submit->records, $arecord) ;
            }
        // increment for next record
        $counter+=100;
        }
    }
};

然后在循环的每次迭代中调用View调用loading,通过传递<?php echo $minutes ?>返回查询将花费的估计时间,并且返回相同的秒数,并且$counter的当前值,以反映当前正在处理的记录集:

<p>Loading record <?php echo $counter ?> of <?php echo $len ?></p>

是否可以从View内返回Model?我不知道我还能为此创建一个“加载”页面。

0 个答案:

没有答案