Laravel:如何组织BaseContoller

时间:2014-05-15 05:55:54

标签: php laravel controller laravel-4

我将在BaseController中找到一个方法来检测用户所在的城市。 最好的方法是什么?我还想使用自动解决方案,并将某些BaseController传递给LocationService的构造函数。

<?php
use Andrew\Services\LocationService;

class BaseController extends Controller {

protected $locationService;

public function __construct( LocationService $locationService )
{
    $this->locationService = $locationService;
    $this->detectLocation();
}

/**
 * Setup the layout used by the controller.
 *
 * @return void
 */
protected function setupLayout()
{
    if ( ! is_null($this->layout))
    {
        $this->layout = View::make($this->layout);
    }
}

public function detectLocation()
{
    $this->locationService->detect();
}

}

如果我采用这种方法,那么我必须将LocationService的实例从每个子控制器传递给父母的构造者

// Sub-controller

public function __construct( CalculatorValidator $validator, TotalCostService $totalCost)
{
    parent::__construct(new LocationService);
    $this->validator = $validator;
    $this->totalCost = $totalCost;
}   

完成我需要的是否合适?它似乎没有什么工作吗?

0 个答案:

没有答案