如何在 Laravel 4 中注册命名空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15204103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:35:35  来源:igfitidea点击:

How to register a namespace in laravel 4

laravellaravel-4

提问by isimmons

The problem: class PostRepostioryInterface not found for line 4 in PostController.php or in tinkering with the namespace I've even got class App\Models\Interfaces\PostRepositoryInterface not found

问题:在 PostController.php 中找不到第 4 行的 PostRepostioryInterface 类,或者在修改命名空间时我什至找不到类 App\Models\Interfaces\PostRepositoryInterface

The questions: How to register a namespace in laravel 4? What do I need to do to get L4 to recognise the classes/interfaces at this namespace?

问题:如何在 Laravel 4 中注册命名空间?我需要做什么才能让 L4 识别此命名空间中的类/接口?

Larave 3 had a $namespaces static object in ClassLoader where you could add namespaces by

Larave 3 在 ClassLoader 中有一个 $namespaces 静态对象,您可以在其中添加命名空间

Autoloader::namespaces(array(
  'App\Models\Interfaces' => path('app').'models/interfaces',
));

I'm not sure if I have that right for laravel 3 but either way, AutoLoader doesn't exist in Laravel 4 and ClassLoader exists but the method namespaces doesn't exist in ClassLoader in Laravel 4.

我不确定我是否对 Laravel 3 有这样的权利,但无论哪种方式,Laravel 4 中都不存在 AutoLoader,并且存在 ClassLoader,但 Laravel 4 中的 ClassLoader 中不存在方法命名空间。

I've looked at this but it doesn't seem to work without registering the namespace somehow. Using namespaces in Laravel 4

我看过这个,但如果不以某种方式注册命名空间,它似乎不起作用。 在 Laravel 4 中使用命名空间

Example structure:

示例结构:

app/models/interfaces
    PostRepostitoryInterface.php
app/models/repositories
    EloquentPostRepository.php


namespaces:
    App\Models\Repositories;
    App\Models\Interfaces;

the files:

文件:

PostRepositoryInterface.php

PostRepositoryInterface.php

<?php namespace App\Models\Interfaces;
interface PostRepositoryInterface {
    public function all();
    public function find($id);
    public function store($data);
}

EloquentPostRepository.php

EloquentPostRepository.php

<?php namespace App\Models\Repositories;
use App\Models\Interfaces\PostRepositoryInterface;
class EloquentPostRepository implements PostRepositoryInterface {

    public function all()
    {
        return Post::all();
    }

    public function find($id)
    {
        return Post::find($id);
    }

    public function store($data)
    {
        return Post::save($data);
    }
}

PostController.php

后控制器.php

<?php
use App\Models\Interfaces\PostRepositoryInterface;
class PostsController extends BaseController {

    public function __construct( PostRepositoryInterface $posts )
    {
        $this->posts = $posts; 
    }

Thanks

谢谢

采纳答案by isimmons

On the laravel irc channel I found out the namespaces should work in L4 without a need for registering them anywhere. This is because the composer dump-autoload adds them to the composer/autoload file for me. So that was not an issue.

在 laravel irc 频道上,我发现命名空间应该可以在 L4 中工作,而无需在任何地方注册它们。这是因为 composer dump-autoload 为我将它们添加到 composer/autoload 文件中。所以这不是问题。

The issue turned out to be a typo apparently(I can't find it in the code above but after going through every line copy/pasting the class names and namespaces something changed), and also somehow in my real code I left out the 'use' statement for EloquentPostRepository.php

这个问题显然是一个错字(我在上面的代码中找不到它,但是在复制/粘贴每一行之后,类名和命名空间发生了一些变化),而且不知何故在我的真实代码中我遗漏了 ' EloquentPostRepository.php 的 use' 语句

use App\Models\Interfaces\PostRepositoryInterface;

Now I've hit another wall trying to use the namespaced interface with ioc and the controller constructor (target interface App\Models\Interfaces\PostRepositoryInterface is not instantiable) but I guess that should be a different question.

现在我遇到了另一堵墙,试图将命名空间接口与 ioc 和控制器构造函数(目标接口 App\Models\Interfaces\PostRepositoryInterface 不可实例化)一起使用,但我想这应该是一个不同的问题。

回答by rmobis

You probably forgot to do composer dump-autoload. This updates the list of classes Laravel autoloads.

你可能忘记了composer dump-autoload。这会更新 Laravel 自动加载的类列表。

You can read more on composer documentation.

您可以阅读有关composer 文档的更多信息