我对Laravel来说相当新,并且想知道实例化和为我的类提供多个对象的最有效方法是什么。
我目前正在做的一个例子就是sudo代码:
// MainController.php
<?php
Class MainController extends BaseController {
public function __construct() {
parent::construct();
}
// class code follows
}
// BaseContoller.php
<?php
use classFile;
Use repositoryFile;
use classFile2;
Use repositoryFile2;
..... and lets say 10 more "included" files
Class BaseController extends Controller {
var classFile;
var repositoryFile;
var classFile2;
var repositoryFile2;
..... and 10 more variables;
public function __construct() {
$this->classFile = new classFile;
$this->classFile = new classFile;
$this->repositoryFile = new repositoryFile;
$this->classFile2 = new classFile2;
$this->repositoryFile2 = new repositoryFile2;
..... and 10 more class instantiations
}
// class code follows
}
希望这很有意义.....
几乎所有继承BaseController的类都会在某些时候使用这些对象,因此为每个类加载它们都是正确的。我的主要两个问题是:
我已经关注了很多Laracasts的内容以及各种阅读材料,但我还没有看到人们如何处理大量的物品,比如我试图使用。或者也许那就是我要去的地方&#34;错误&#34;?
干杯!
==以下是我正在努力的API主页调用示例。它将来自整个网站的内容汇总到移动应用的Feed中:
public function index()
{
$channels = $this->channelRepository->getChannels();
$allChannels = $this->channelRepository->getAllChannels();
$sponsors = $this->sponsorRepository->getSponsors();
$inactiveUserChannels = [];
// use the pattern maker, set the pattern we want to use
$this->patternMaker->setPattern(1);
$channelFeed = [];
if( userIsAuthenticated() )
{
$inactiveUserChannels = $this->userRepository->getUserInactiveChannels( 1 );
}
// Picks
$picks = $this->articleRepository->getArticles( 'picks', 25 );
$ads = $this->sponsorRepository->getWhereNotInCollection( $sponsors, 30 );
$response = $this->patternMaker->make( [ 'articles' => $picks, 'sponsors' => $ads ] );
$picks = $response->articles;
$ads = $response->sponsors;
// Whats on
$channel = 50;
$whatsOn = $this->articleRepository->getArticlesWithEvents(null); // get 20 articles from the whats on channel
$response = $this->patternMaker->make( [ 'articles' => $whatsOn, 'sponsors' => $ads ], "whats-on" );
$whatsOn = $response->articles;
$ads = $response->sponsors;
$channel = $this->channelTransformer->transform( getChannel($channels, $channel) );
$channel['articles'] = $whatsOn;
$channelFeed[] = $channel;
// create a new instance of the channel feed class and pass the required params to it
$this->channelFeed = $this->createChannelFeed( $allChannels, [ 48, 49, 51, 52 ], $ads, $inactiveUserChannels );
if( ! $response = cached("homepage") )
{
$data = [
'channels' => $this->channelTransformer->transformCollection( $channels )
,'adverts' => $this->sponsorTransformer->transformCollection( $sponsors->toArray() )
,'features' => $this->articleTransformer->transformCollection( $this->articleRepository->getArticles( 'featured', 25 ), [ 'showBody' => false] )
,'picks' => $picks
,'channelFeed' => $this->channelFeed->make()
];
cacheIt("homepage", $response, "1 hour");
}
return $this->respondFound(Lang::get('api.homepageFound'), $data);
}
这是早期阶段,我正在进行重构,这就是类依赖性问题的来源。
答案 0 :(得分:0)
首先,除非MainController上的构造函数具有更多逻辑,否则可以将构造函数全部放在一起,并直接调用BaseController构造函数。
你可以这样做:
Class BaseController extends Controller {
protected $classFile;
//repeat for all classes
function classFile() {
if(!$this->classFile) $this->clasFile = new classFile;
return $this->classFile;
}
//repeat for all classes
function useExample() {
$this->classFile()->randomMethod();
}
}
......这种方式至少你不会超过需要。
那个说,听起来你真正想要的是Facades,你可以去:
$channels = ChannelRepository::getChannels();