[英]Laravel PSR-0 - concrete class (that extends a abstract class) not loading
我的composer.json
有以下條目
"autoload": {
...
"psr-0": {
"Latheesan": "app/"
}
...
},
我的文件夾結構如下所示:
這是我的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
}
這是我的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;
}
}
為了測試這一點,我在routes.php
創建了以下條目
use Latheesan\Reporting;
Route::get('/test', function() {
return App::make('PackagingLevelsReport')->toCSV();
});
當我訪問本地開發站點的網址(即http://my-project.local/test )時,出現以下錯誤:
我已經運行過composer dump-auto
,但是我的課仍然沒有上課。 有什么想法在這里可能出什么問題嗎?
代替:
App::make('PackagingLevelsReport')
嘗試:
App::make('Latheesan\Reporting\PackagingLevelsReport')
在您的版本中, App::make
在全局命名空間(而不是它所在的位置)中尋找類PackagingLevelsReport
。
更新
為了響應您的后續行動,一種解決方案是創建一個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);
});
}
}
另外,如果您希望能夠在進行App::make
調用的上下文中指定參數,則只需提供第二個數組參數即可:
App::make('Latheesan\Reporting\PackagingLevelsReport', ['param1', 'param2'])
我認為第二個選擇就是您要尋找的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.