Laravel PSR-0 - 具体类(扩展抽象类)不加载

时间:2015-04-30 14:18:17

标签: php oop laravel laravel-4

我的composer.json

中有以下条目
"autoload": {
    ...
    "psr-0": {
        "Latheesan": "app/"
    }
    ...
},

我的文件夹结构如下所示:

enter image description here

这是我的 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)时,我收到以下错误:

类PackagingLevelsReport不存在

我已经跑了composer dump-auto但我的班级仍然没有被选中。任何想法在这里可能有什么问题?

1 个答案:

答案 0 :(得分:1)

而不是:

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'])

我认为第二种选择就是你要找的东西。