[英]Laravel PSR-0 - concrete class (that extends a abstract class) not loading
I have the following entry in my composer.json
我的
composer.json
有以下条目
"autoload": {
...
"psr-0": {
"Latheesan": "app/"
}
...
},
And my folder structure looks like this: 我的文件夹结构如下所示:
This is my AbstractReporting class: 这是我的AbstractReporting类:
<?php namespace Latheesan\Reporting;
abstract class AbstractReporting
{
// Force Extending class to define these methods
abstract protected function getReportingData();
// Common method to transform reporting data to CSV format
public function toCSV()
{
// CSV headers
$headers = [
'Content-type' => 'application/csv'
, 'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0'
, 'Content-type' => 'text/csv'
, 'Content-Disposition' => 'attachment; filename='. __CLASS__ .'.csv'
, 'Expires' => '0'
, 'Pragma' => 'public'
];
// Load reporting data
$reporting_data = $this->getReportingData();
// Dynamically write csv
$callback = function() use ($reporting_data) {
$h = fopen('php://output', 'w');
foreach ($reporting_data as $row)
fputcsv($h, $row);
fclose($h);
};
// Return csv data
return Response::stream($callback, 200, $headers);
}
// Common method to transform reporting data to PDF format
// TODO
// Common method to transform reporting data to JSON format
// TODO
}
and this is my PackagingLevelsReport class: 这是我的PackagingLevelsReport类:
<?php namespace Latheesan\Reporting;
class PackagingLevelsReport extends AbstractReporting {
// Class properties
protected $reporting_data = [];
// Class constructor
public function __construct($date_range) {
if ($date_range == 'today') {
$this->reporting_data = [
'sku' => 'box1',
'qty' => 10142
];
}
}
// Method for returning reporting data
protected function getReportingData()
{
return $this->reporting_data;
}
}
to test this, I created the following entry in my routes.php
为了测试这一点,我在
routes.php
创建了以下条目
use Latheesan\Reporting;
Route::get('/test', function() {
return App::make('PackagingLevelsReport')->toCSV();
});
When I visit my local dev site url (ie http://my-project.local/test ) I get the following error: 当我访问本地开发站点的网址(即http://my-project.local/test )时,出现以下错误:
I have already ran composer dump-auto
and yet my class is still not being picked up. 我已经运行过
composer dump-auto
,但是我的课仍然没有上课。 Any ideas what might be wrong here? 有什么想法在这里可能出什么问题吗?
Instead of: 代替:
App::make('PackagingLevelsReport')
Try: 尝试:
App::make('Latheesan\Reporting\PackagingLevelsReport')
In your version, App::make
is looking for the class PackagingLevelsReport
in the global namespace, which isn't where it is located. 在您的版本中,
App::make
在全局命名空间(而不是它所在的位置)中寻找类PackagingLevelsReport
。
Update 更新
In response to your follow-up, one solution is to create a ServiceProvider
that will be able to provide the parameters to the constructor: 为了响应您的后续行动,一种解决方案是创建一个
ServiceProvider
,它将能够向构造函数提供参数:
use Illuminate\Support\ServiceProvider;
class FooServiceProvider extends ServiceProvider {
public function register()
{
$foo = 'foo';
$this->app->bind('foo', function() use($foo)
{
return new Foo($foo);
});
}
}
Alternatively, if you want to be able to specify the parameter from within the context from which the App::make
call is being made, just provide a second array parameter: 另外,如果您希望能够在进行
App::make
调用的上下文中指定参数,则只需提供第二个数组参数即可:
App::make('Latheesan\Reporting\PackagingLevelsReport', ['param1', 'param2'])
I think that the second option is what you're looking for. 我认为第二个选择就是您要寻找的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.