我有一个发票控制器,它看起来像这个
<?php
namespace App'Http'Controllers;
use Illuminate'Http'Request;
use App'Http'Requests;
use App'Http'Controllers'Controller;
use Illuminate'Support'Facades'Input;
use App'Event;
use App'Employee;
use App'Invoice;
use Mail;
use View;
class ViewinvoiceController extends Controller
{
/**
* Display a listing of the resource.
*
* @return 'Illuminate'Http'Response
*/
public function getIndex($order_id)
{
$id=$order_id;
$invoicedata=Invoice::where('Id',$id)->get();
$html22 = View('viewinvoice')->with(array('invoicedata'=>$invoicedata ))->render();
require_once(app_path().'/libs/html2pdf/html2pdf.class.php');
$html2pdf = new 'HTML2PDF('P','A4','en',true,'UTF-8',array(0, 0, 0, 0));
// $html2pdf->pdf->SetDisplayMode('fullpage');
$html2pdf->WriteHTML($html22);
$html2pdf->Output('Invoice.pdf');
}
}
我想在其他控制器中使用这个控制器,它看起来像这个
class CollectionController extends Controller
{
public function __construct(){
$this->middleware('role:collector'); // replace 'collector' with whatever role you need.
}
public function getInvoice($order_id){
//Here I have to write the logic of getting the invoice from the invoiceController
}
}
我在谷歌上搜索了一下,发现一种方法是写一个服务来获得发票,
我可以把它作为一个普通的服务类,但我不知道在laravel 5 中什么是正确的方法
任何建议
您可以将此特征放置在App'Services
中,并使用名称ViewinvoiceTrait.php
保存文件
<?php
namespace App'Services;
use App'Invoice;
use View;
trait ViewinvoiceTrait {
/**
* Display a listing of the resource.
*
* @return 'Illuminate'Http'Response
*/
public function getIndex($order_id) {
$id = $order_id;
$invoicedata = Invoice::where('Id', $id)->get();
$html22 = View('viewinvoice')->with(array('invoicedata' => $invoicedata))->render();
require_once(app_path() . '/libs/html2pdf/html2pdf.class.php');
$html2pdf = new 'HTML2PDF('P', 'A4', 'en', true, 'UTF-8', array(0, 0, 0, 0));
// $html2pdf->pdf->SetDisplayMode('fullpage');
$html2pdf->WriteHTML($html22);
return $html2pdf->Output('Invoice.pdf');
}
}
并在你的控制器中使用它,就像一样
use App'Services'ViewinvoiceTrait;
class CollectionController extends Controller
{
use ViewinvoiceTrait;
public function __construct(){
$this->middleware('role:collector'); // replace 'collector' with whatever role you need.
}
public function getInvoice($order_id){
$data = $this->getIndex($order_id);
}
}
根据设计标准,在另一个控制器中使用控制器,如果您认为您有一些功能需要在多个控制器或位置触发,那么Jobs
就来了。
点击此处阅读更多关于拉雅维·乔布斯的信息。作业可以同步或异步运行,这取决于您的应用程序。