laravel中的命名空间问题?

时间:2014-09-01 17:02:15

标签: php laravel laravel-4

我有以下结构。

app/
  acme/
    Providers/
    Services/
      CloudServices/
        Files/
          FielsInterface.php
          s3Files.php
      Images/
        ImageEditorInterface.php
         ImageEditor.php

我通过添加

在控制器中使用s3files进行绑定
use Acme\Services\CloudServices\Files\FilesInterface

到控制器的顶部,然后使用构造函数,它工作得很好。

我现在想在ImageEditor.php中使用它,其命名空间是

Acme\Services\Images

但是使用下面的代码我得到一个类型错误,说参数2必须是FilesInterface的一个实例,但没有给出

<?php namespace Acme\Services\Images;

use IntImage;
use Response;
use Acme\Services\CloudServices\Files\FilesInterface;

/**
*
*/
class ImageEditor implements ImageEditorInterface
{
    protected $imageeditor;
    protected $files;
    protected $imageSizes = array(
        "thumbnail" => array(
            "h" => 50,
            "w" => 50
        ),
        "x-small" => array(
            "h" => 150,
            "w" => 150
        ),
        "small" => array(
            "h" => 390,
            "w" => 360
        )
    );

    public function __construct(IntImage $imageeditor, FilesInterface $files)
    {
        $this->imageeditor = $imageeditor;
        $this->files = $files;
    }

看到它在控制器中工作,我猜我的使用声明是错误的,但我不知道应该是什么呢?

EDIT1:

ImageEditor正在注入并在像这样的控制器中使用

<?php

use Acme\Services\Images\ImageEditorInterface;
use Acme\Services\CloudServices\Files\FilesInterface;

class ImagesController extends BaseController
{
    protected $image;
    protected $editor;
    protected $files;
    protected $queue;

    public function __construct(Image $image, ImageEditorInterface $editor, FilesInterface $files, Queue $queue)
    {
        $this->image = $image;
        $this->editor = $editor;
        $this->files = $files;
        $this->queue = $queue;
        $this->beforeFilter('auth', array('except' => array('show')));
    }
public function store()
{
    $this->editor->createUserImages()

}

edit2:在Acme / Providers / FilesServiceProvider.php我有,是不是那样做你描述的?另外,因为我可以使用控制器中的文件实现,它不会建议正确设置该部分吗?

<?php namespace TrainerCompare\Providers;

use Illuminate\Support\ServiceProvider;
use TrainerCompare\Services\CloudServices\Files\S3Files;
use AWS;

/**
*
*/
class FilesServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->bind('TrainerCompare\Services\CloudServices\Files\FilesInterface', function () {
            $aws = new AWS;

            return new S3Files($aws);
        });
    }
}

2 个答案:

答案 0 :(得分:1)

在你的应用程序的某个地方你需要告诉Laravel在需要注入FilesInterface时要实例化的具体类。

App::bind(
    'Acme\Services\CloudServices\Files\FilesInterface', 
    'Acme\Services\CloudServices\Files\s3Files'
);

您可能也将服务提供商添加到app/config/app.php,对吗?

所以,因为你已经绑定它,要调试,检查它是否实际绑定:

Route::any('test', function()
{
    dd( App::bound('TrainerCompare\Services\CloudServices\Files\FilesInterface') );
});

因为看起来像Laravel在控制器实例化时无法找到该绑定。

并检查是否正在调用您的服务提供商注册方法:

class FilesServiceProvider extends ServiceProvider
{
    public function register()
    {
        dd('yes it is');

        ....
    }
}

编辑:

这里可能存在一个我无法看到的命名问题,但是Laravel正在寻找容器中未列出的内容(原样)。您可能需要检查的事项:您的文件命名空间,自动加载规则,您可以尝试暂时删除您的用途并将它们直接添加到控制器来进行测试

public function __construct(
    Image $image, ImageEditorInterface $editor,
    TrainerCompare\Services\CloudServices\Files\FilesInterface $files, 
    Queue $queue
)
{ ... }

答案 1 :(得分:-2)

您需要在名称间隔类中使用反斜杠添加use语句,如下所示:

<?php namespace Acme\Services\Images;

use \Acme\Services\CloudServices\Files\FilesInterface;

否则PHP会将其视为相对命名空间路径,并尝试解决它:

Acme\Services\Images\Acme\Services\CloudServices\Files\FilesInterface

相关问题