如何从另一个类Laravel调用函数

时间:2016-10-10 07:09:07

标签: php laravel

我的Helpers文件夹中有payrollProcessController和PayrollHelper类。

这是我的PayrollProcessController代码:

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Base\MasterController;
use App\Http\Requests;
use App\Models\PayrollPeriod;
use App\Helpers\PayrollHelper;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Yajra\Datatables\Datatables;

class PayrollProcessController extends MasterController
{
protected $indexView = 'payroll_process.index';
protected $detailView = 'payroll_process.detail';
protected $routeBindModel = 'payroll_process';
protected $redirectPageWhenFormSuccess = 'payroll_process.index'; //Route Name
protected $title = 'Payroll Process';

public function render(View $view, $route = null, $obj = null, $method = 'POST')
{
    $payrollPeriodName = PayrollPeriod::pluck('name','id');
    $view->with(compact('payrollPeriodName'));
    return parent::render($view, $route, $obj, $method);
}
}

这是我的PayrollHelper代码:

<?php

namespace App\Helpers;

use App\Helpers\Exceptions\ValidationException;
use App\Helpers\GeneralHelper;
use App\Models\Config;
use App\Models\Driver;
use App\Models\DriverPayable;
use App\Models\PayrollPeriod;
use Carbon\Carbon;
use Exception;
use Illuminate\Support\Facades\Log;

final class PayrollHelper {
public static function processPayroll(PayrollPeriod $payrollPeriod) 
{
    try {
        $drivers = Driver::where('active', true)->get();
        foreach ($drivers as $driver) {
            $payable = new DriverPayable;
            $payable->payrollPeriod()->associate($payrollPeriod);
            $payable->driver()->associate($driver);
            if (!$payable->save()) {
                throw new ValidationException($payable->errors());
            }
        }
    } catch (Exception $e) {
        Log::error($e);
        SessionHelper::setMessage('Unable to process payroll, Please contact system Administrator');
    } catch (ValidationException $e) {
        Log::info($e->errors);
        SessionHelper::setMessage($e->errors);
    }
}
}
?>

如何调用processPayroll函数?

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:1)

试试这个.. 在控制器中使用帮助器

use App\Helpers\PayrollHelper;

这是你的控制器构造函数

public function __construct(PayrollHelper $payroll_helper)
{
  parent::__construct();
  $this->payroll_helper = $payroll_helper;
}

像这样调用方法

$this->payroll_helper->processPayroll();

<强>更新 你的控制器看起来像这样

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Base\MasterController;
use App\Http\Requests;
use App\Models\PayrollPeriod;
use App\Helpers\PayrollHelper;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Yajra\Datatables\Datatables;

class PayrollProcessController extends MasterController
{
  protected $indexView = 'payroll_process.index';
  protected $detailView = 'payroll_process.detail';
  protected $routeBindModel = 'payroll_process';
  protected $redirectPageWhenFormSuccess = 'payroll_process.index'; //Route Name
  protected $title = 'Payroll Process';

  public function __construct(PayrollHelper $payroll_helper)
  {
    parent::__construct();
    $this->payroll_helper = $payroll_helper;
  }

  public function render(View $view, $route = null, $obj = null, $method = 'POST')
  {
    $payrollPeriodName = PayrollPeriod::pluck('name','id');
    $view->with(compact('payrollPeriodName'));

    //Use where you want
    $this->payroll_helper->processPayroll();

    return parent::render($view, $route, $obj, $method);
  }
}

答案 1 :(得分:1)

我只想分享我所做的, 尚不知道这样做的副作用,但我们会,

在控制器中使用帮助程序

use App\Helpers\PayrollHelper;

在可以使用 Payroll helper 中的函数之一的函数中,执行

$payrollhelper = new PayrollHelper();

然后调用 helper 中的任何函数

$payrollhelper->funtion_name()...

这对我有用