Php 在Laravel控制器中获取当前视图

Php 在Laravel控制器中获取当前视图,php,laravel,dompdf,Php,Laravel,Dompdf,我尝试做的事情:我想生成当前视图的PDF 更多详细信息::我有很多客户端,我想在每个页面上创建一个按钮,以便生成每个客户端详细信息的PDF 拉维包装: 我到目前为止所做的事情: 我在我的应用程序中添加了DomPdf 我创建了一个Route::resource('impimer','PrintController');路线 我添加了一个打印控制器 我在视图中包含了一个链接(2个版本) {{HTML::linkAction('PrintController@show'、'PDF',array(),

我尝试做的事情:我想生成当前视图的PDF 更多详细信息::我有很多客户端,我想在每个页面上创建一个按钮,以便生成每个客户端详细信息的PDF

拉维包装:

我到目前为止所做的事情

  • 我在我的应用程序中添加了DomPdf
  • 我创建了一个Route::resource('impimer','PrintController');路线
  • 我添加了一个打印控制器
  • 我在视图中包含了一个链接(2个版本)
  • {{HTML::linkAction('PrintController@show'、'PDF',array(),array('class'=>'btn btn primary pull right mgright10'))}

    
    

    我的问题:如何将当前视图(例如:客户端/1)传递给控制器,以便控制器生成当前页面的PDF?

    您必须设置一个包含当前视图名称的变量,因为没有内置方法。我们可以使用
    View::composer
    ,它基本上是视图渲染时的回调

    View::composer('*', function($view){
        View::share('view_name', $view->getName());
    });
    
    如果它只影响某些视图,您还可以通过将
    *
    更改为另一个掩码来限制它

    现在你可以做:

    {{ HTML::linkAction('PrintController@show', 'PDF', array($view_name), array('class'=>'btn btn-primary pull-right mgright10')) }}
    
    并在控制器中接收:

    public function show($viewName){
       //...
    }
    
    我发现(我离它很近)。以下是我的做法,以供参考:

    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();
        }
    }
    
    Route::resource('imprimer', 'PrintController');
    
    <?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(); 
    }
    }
    }
    
    查看

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

    我设法创建了一种调用“createpdf”函数的新方法,而无需向PrintController添加代码

    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();
        }
    }
    
    Route::resource('imprimer', 'PrintController');
    
    <?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(); 
    }
    }
    }
    

    我在filters.php中添加了View:composer代码块。我得到了一个未定义的变量:viewName(视图:…app\views\clients\show.blade.php)errorOh我的错。一次我写了
    view\u name
    ,另一次写了
    $viewName
    。。。答案是updatedI现在获取未定义的变量:client(视图:…\app\views\clients\show.blade.php)我会尝试看看原因,但如果您知道,请告诉我;)嗯,不知道要诚实,但是现在每次你想用不同的视图打印一个页面时,你都必须做一个新的控制器功能。你没有得到我的答案吗?是的,我知道我实际上试图将我的答案和你的答案合并。那么我的解决方案有什么问题呢?仍然是
    $client
    变量?是的,它是关于$client变量的。在控制器中创建视图时,您通常会传入此变量吗?