我有一个Invoice控制器,它看起来像这样
<?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
}
}
我用Google搜索并发现一种方法是编写服务以获取发票,
我可以把它作为一个普通的班级作为服务,但我不知道什么是正确的方式在laravel 5
任何建议
答案 0 :(得分:0)
在另一个Controller中使用Controller而不是设计标准,如果您认为某些功能需要在多个控制器或位置中触发,那么Jobs
就会出现。
了解有关Laravel Jobs here的更多信息。作业可以同步或异步运行,这取决于您的应用程序。
答案 1 :(得分:0)
您可以将此特征置于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);
}
}