我将在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;
}
完成我需要的是否合适?它似乎没有什么工作吗?