2014-09-02 21 views
10

Tôi đang sử dụng maatwebsite/trội thư viện để tạo ra file excel sau đó tải tập tin của tôiLàm cách nào để chuyển hướng sau khi tải xuống trong Laravel?

trong bộ điều khiển của tôi, tôi làm như thế này:

public function todas() 
    { 
     $input = Input::all(); 

     if(isset($input['todos'])&&($input['todos']!=0)) 
     { 
      set_time_limit(0); 
      $datainicio = DB::table('tb_periodo')->where('cod', $input['todos'])->pluck('periodo_inicio'); 
      $datafinal = DB::table('tb_periodo')->where('cod', $input['todos'])->pluck('periodo_fim'); 
      $mes = DB::table('tb_periodo')->where('cod', $input['todos'])->pluck('mes_referencia'); 

      $horarioQuery = $this->horario->with(array('funcionario', 'item_contabil')) 
           ->whereBetween('data', array($datainicio, $datafinal)) 
           ->whereNull('deleted_at') 
           ->orderBy('cod_funcionario') 
           ->orderBy('data', 'ASC') 
           ->get(); 

      $horarios = reset($horarioQuery); 

      $count = count($horarios); 

      if($horarios==null) 
       return Redirect::route('relatorios.todas')->withErrors(['não existem marcações para este período']); 

      $funcionarios = $this->funcionario->all(); 

      $datainicio = Carbon::createFromFormat('Y-m-d H:i:s', $datainicio); 
      $datafinal = Carbon::createFromFormat('Y-m-d H:i:s', $datafinal); 

      $nome = 'Marcações'.$mes.'-'.Carbon::now()->year; 
      $this->horario->allToExcel($nome, $horarios); 

      return View::make('relatorios.todashow', compact('funcionarios', 'datainicio', 'datafinal', 'horarios', 'count', 'nome')); 
     } 
     else 
     { 
      return Redirect::route('relatorios.todas')->withErrors(['Selecione um período!']); 
     } 
    } 

đó là chức năng của tôi để tạo ra excel file:

public static function allToExcel($nome, $horarios) 
    { 
      Excel::create($nome , function ($excel) use ($horarios) { 

      $excel->sheet('Marcações', function ($sheet) use ($horarios) { 

       $sheet->row(1, array(

          'Unidade', 
          'Nome', 
          'Função', 
          'Data', 
          'Hora Faturada', 
          'Hora Contratada', 
          'Horas Trabalhadas', 
          'Horas Extras', 
          'Tempo Exposicao', 
          'Atividade', 
          'Item Contabil', 
          'Observacoes' 
         )); 

       $sheet->row(1, function($row) { 
        $row->setBackground('#2A8005'); 
        $row->setFontColor('#ffffff'); 
        $row->setFontWeight('bold'); 
       }); 

       $i = 2; 
       foreach ($horarios as $horario) { 

         if($horario->funcionario->funcao_qt != null) 
          $funcao = $horario->funcionario->funcao_qt; 
         else 
          $funcao = $horario->funcionario->funcao_a5; 

         $sheet->row($i, array(
          $horario->unidade, 
          $horario->funcionario->nome, 
          $funcao, 
          $horario->data->format('d/m/Y'), 
          $horario->hora_faturada->format('H:i'), 
          $horario->hora_contratada, 
          $horario->getWorkedHours()->format('H:i'), 
          $horario->getExtraHours()->format('H:i'), 
          $horario->tempo_exposicao ?: "-", 
          $horario->atividade, 
          $horario->item_contabil->CTD_DESC01, 
          $horario->observacoes 
         )); 
         if($i % 2 != 0) 
         { 
         $sheet->row($i, function($row) { 
          $row->setBackground('#D1D1D1'); 
          }); 
         }  

         $i++; 
        } 
      }); 

      })->download('xls'); 
    } 

nhưng sau khi tôi tải xuống tệp excel, tôi không thể chuyển hướng đến tuyến đường hoặc chế độ xem và tôi cũng đã cố gắng sử dụng:

tuyến đường:

Route::post('relatorios/todas', array('as' => 'relatorios.todas', 'after' => 'voltar', 'uses' => '[email protected]')); 

lọc:

Route::filter('voltar', function() 
{ 
    return Redirect::back()->with('message', '<p class="bg-success"><b>Relatório gerado com sucesso</b></p>'); 
}); 

nhưng didnt làm việc dù sao, có một cách khác để chuyển hướng sau khi tải về tập tin của tôi ??!

thx trước!

Trả lời

10

Không thể thực hiện được. Vấn đề là nếu bạn gửi một hướng dẫn tải về trình duyệt người dùng, bạn đang, như một vấn đề của thực tế, gửi một phản ứng và bạn có thể gửi chỉ một phản hồi trở lại.

Điều bạn có thể làm là, trước tiên hãy chuyển hướng người dùng của bạn đến trang "cuối cùng" và trong trang đó bắt đầu tải xuống. Mã này sẽ là một cái gì đó như:

Session::flash('download.in.the.next.request', 'filetodownload.pdf'); 

return Redirect::to('/whatever/page'); 

Sau đó, trong trang mới, bạn sẽ có một số tùy chọn:

Vì vậy, bạn có thể trong cách bố trí của bạn làm như sau:

<html> 
    <head> 
     @if(Session::has('download.in.the.next.request')) 
     <meta http-equiv="refresh" content="5;url={{ Session::get('download.in.the.next.request') }}"> 
     @endif 
    <head> 

    <body> 
     ... 
    </body> 
</html> 

Ngoài ra, hãy xem câu trả lời này : PHP generate file for download then redirect

+0

Tôi sẽ dùng thử! –

0

mà làm việc

tôi đã từ bỏ từ việc sử dụng ajax và chỉ cố gắng với các tuyến đường

Route::get('relatorios/exportar/{cod}', array('as' => 'relatorios.exportar', 'uses' => '[email protected]')); 

điều khiển của tôi

public function exportar($cod) 
    { 
     set_time_limit(0); 
     $datainicio = DB::table('tb_periodo')->where('cod', $cod)->pluck('periodo_inicio'); 
     $datafinal = DB::table('tb_periodo')->where('cod', $cod)->pluck('periodo_fim'); 
     $mes = DB::table('tb_periodo')->where('cod', $cod)->pluck('mes_referencia'); 

     $horarioQuery = $this->horario->with(array('funcionario', 'item_contabil')) 
          ->whereBetween('data', array($datainicio, $datafinal)) 
          ->whereNull('deleted_at') 
          ->orderBy('cod_funcionario') 
          ->orderBy('data', 'ASC') 
          ->get(); 

     $horarios = reset($horarioQuery); 

     $nome = 'Marcações'.$mes.'-'.Carbon::now()->year; 

     $this->horario->allToExcel($nome, $horarios); 
    } 

xem:

{{ link_to_route('relatorios.exportar', 'Exportar para excel', array($cod), array('class' => 'btn btn-success')) }} 

đó là giải quyết đối với tôi, vì dont tải một trang khác và tải về tập tin chính xác. thx để được trợ giúp !!

+0

Tôi gặp vấn đề tương tự nhưng không thể hiểu câu trả lời của bạn, bạn có thể xây dựng được không .. – Arshi

Các vấn đề liên quan