简体   繁体   English

获取Laravel控制器中的当前视图

[英]Get the current view in a Laravel controller

What I try to do : I want to generate PDF of the current view More details: : I got many clients and I want to create a button on each page so that I can generate PDF of each clients details. 我尝试做什么 :我想生成当前视图的PDF 更多详细信息::我有很多客户端,我想在每个页面上创建一个按钮,以便我可以生成每个客户详细信息的PDF。

Laravel package: https://github.com/barryvdh/laravel-dompdf Laravel包: https//github.com/barryvdh/laravel-dompdf

What I have done up to now : 我到目前为止做了什么

  1. I added DomPdf to my application 我将DomPdf添加到我的应用程序中
  2. I created a Route::resource('imprimer', 'PrintController'); 我创建了一个Route :: resource('imprimer','PrintController'); route 路线
  3. I added a PrintController 我添加了一个PrintController

class PrintController extends \BaseController{

    public function show(){
      $pdf = PDF::loadView('clients.show');
      return $pdf->stream();
}}
  1. I included a link in my view (2 versions) 我在视图中包含了一个链接(2个版本)

{{ HTML::linkAction('PrintController@show', 'PDF',array(), array('class'=>'btn btn-primary pull-right mgright10')) }} {{HTML :: linkAction('PrintController @ show','PDF',array(),array('class'=>'btn btn-primary pull-right mgright10'))}}

<a href="{{ URL::to('imprimer/' . $client->id) }}">Pdf2</a>

My question : How to pass to current view (ex: clients/1) to the controller so that the controller generates a PDF of the current page? 我的问题 :如何将当前视图(例如:clients / 1)传递给控制器​​,以便控制器生成当前页面的PDF?

You have to set a variable that contains the current view name, since there's no built in method for that. 您必须设置包含当前视图名称的变量,因为没有内置方法。 We can use View::composer which is basically a callback for when a view gets rendered. 我们可以使用View::composer ,它基本上是渲染视图时的回调。

View::composer('*', function($view){
    View::share('view_name', $view->getName());
});

If it only affects certain views you could also restrict it by changing * to another mask. 如果它只影响某些视图,您也可以通过将*更改为另一个蒙版来限制它。

So now you can do: 所以现在你可以这样做:

{{ HTML::linkAction('PrintController@show', 'PDF', array($view_name), array('class'=>'btn btn-primary pull-right mgright10')) }}

And receive it in your controller: 并在您的控制器中接收它:

public function show($viewName){
   //...
}

I found how (I was close to it). 我发现了(我接近它)。 Here is how I did for reference: 以下是我的参考方法:

PrintController.php PrintController.php

class PrintController extends \BaseController{
public function show($id)
{
    $client_id = Client::find( $id );
    $html = View::make( 'clients.show' )->withClient( $client_id );
    $pdf = App::make( 'dompdf' );
    $pdf->loadHTML( $html );
    return $pdf->stream();
    }
}

View 视图

<a href="{{ URL::to('imprimer/'.$client->id) }}">
  <button>PDF</button>
</a>

Route.php Route.php

Route::resource('imprimer', 'PrintController');

I managed to create a new way to call the "create PDF" function without having to add code to the PrintController. 我设法创建了一种新方法来调用“创建PDF”功能,而无需向PrintController添加代码。

PrintController.php PrintController.php

<?php
class PrintController extends \BaseController{

public function show( $id )
{
if( isset($id) )
{
    /** remove last 2 character */
    $class_name = substr( $id, 0, -2 );

    /** Stay only last character */
    $id = substr( $id, -1 );

    if( isset($class_name) )
    {
        $with_method = 'with'.ucfirst( $class_name ); // withClient
        $find_method = ucfirst( $class_name ); // Client
        $html = View::make( $class_name .'s.show' )->$with_method( $find_method::find($id) );    
    }
    else
    {
        $html = 'Data Not Found!';
    }

    $pdf = App::make( 'dompdf' );
        $pdf->loadHTML( $html );
        return $pdf->stream(); 
}
}
}

View (Only the client and $client to remplace) 查看 (仅限客户端和$ client重新提交)

<a href="{{ URL::to('imprimer/'.'client='. $client->id) }}" target="_blank">
  <button class="btn btn-primary pull-right mgright10">PDF2</button>
</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM