laravel 为什么在同一个命名空间中找不到我的类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18246713/
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 08:17:23  来源:igfitidea点击:

Why can't my class be found, when it's in the same namespace?

laravellaravel-4composer-php

提问by kmc

I have created a modulesfolder in my Laravel app. There are two modules so far, but I'm just concentrating on corehere.

modules在我的 Laravel 应用程序中创建了一个文件夹。到目前为止有两个模块,但我只关注core这里。

folder structure

文件夹结构

I'm using Confide and Entrust to build User functionality, like so:

我正在使用 Confide 和 Entrust 来构建用户功能,如下所示:

namespace App\Modules\Core;

use Zizaco\Confide\ConfideUser;
use Zizaco\Entrust\HasRole;

class User extends ConfideUser {
  use HasRole;
}

and Permissions:

和权限:

namespace App\Modules\Core;

use Zizaco\Entrust\EntrustPermission;

class Permission extends EntrustPermission
{
}

and Roles:

和角色:

namespace App\Modules\Core;

use Zizaco\Entrust\EntrustRole;

class Role extends EntrustRole
{
}

My Composer.json autoload reads:

我的 Composer.json 自动加载读取:

"autoload": {
  "classmap": [
    "app/commands",
    "app/controllers",
    "app/models",
    "app/database/migrations",
    "app/database/seeds",
    "app/tests/TestCase.php",
    "app/modules"
  ],
  "psr-0": {
    "App\Modules\": "modules/"
  }
},

I put the psr-0 stuff in there because I couldn't get things to work. They still don't work, though the output autoload files when I run composer seem to have promising entries in them.

我把 psr-0 的东西放在那里,因为我无法让事情工作。它们仍然不起作用,尽管当我运行 Composer 时输出的自动加载文件似乎包含有希望的条目。

The database has been migrated, and now I'm trying to run the database seeding. My seeding script reads:

数据库已迁移,现在我正在尝试运行数据库播种。我的播种脚本如下:

use App\Modules\Core\User;
use App\Modules\Core\Role;
use App\Modules\Core\Permission;

class UserTablesSeeder extends Seeder {

  public function run()
  {
    DB::table('users')->insert(array(
      'email'  => 'xxx',
      'first_name'  => 'xxx',
      'password'  => 'xxxx',
      'active'  => 1
    ));

    $admin = new Role;
    $admin->name = 'Admin';
    $admin->save();

    $manageUsers = new Permission;
    $manageUsers->name = 'manage_standard_users';
    $manageUsers->display_name = 'Manage Users';
    $manageUsers->save();

    $admin->perms()->sync(array($manageUsers->id));

    $user = User::where('email','=','xxx')->first();
    $user->attachRole($admin);
  }
}

But when I run php artisan db:seedI get an error:

但是当我运行时php artisan db:seed出现错误:

PHP Fatal error:  Class 'Permission' not found in /home/wedding/quincy/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php on line 604
{"error":{"type":"Symfony\Component\Debug\Exception\FatalErrorException","message":"Class 'Permission' not found","file":"\/home\/wedding\/quincy\/vendor\/laravel\/framework\/src\/Illuminate\/Database\/Eloquent\/Model.php","line":604}}

If I get rid of all the namespacing it all works just fine, but I think I should keep the namespaces because of the modularity I'm trying to create.

如果我摆脱了所有的命名空间,一切都很好,但我认为我应该保留命名空间,因为我正在尝试创建模块化。

I've run composer dump-autoload, and installfor when I added the psr-0 entry. So I'm not sure what else I need to do. I'm very new to composer, so at this point I'm lost as to what the problem is.

我已经运行了composer dump-autoloadinstall当我添加 psr-0 条目时。所以我不确定我还需要做什么。我对作曲家很陌生,所以在这一点上我不知道问题是什么。

Thanks in advance.

提前致谢。

回答by Hejazzman

Don't know if you found your answer.

不知道你有没有找到答案。

I think you need to update your role and permission namespaced class names here:

我认为您需要在此处更新您的角色和权限命名空间类名:

File: vendor/entrust/config/config.php:

文件:vendor/entrust/config/config.php:

(Default is just "Role" and "Permission" without a namespace, so it doesn't work when you move your implementations into one).

(默认值只是没有命名空间的“角色”和“权限”,因此当您将实现移到一个时它不起作用)。

You also have two other options:

您还有另外两个选择:

  • add an alias for the fully namedspaced permission and role class on the app/config.php (Role => "Namespace").

  • there's a mechanism to override package config settings with appropriately named files. You can override just the two entries you need (the Role and Permission namespaces).

  • 在 app/config.php (Role => "Namespace") 上为完全命名空间的权限和角色类添加别名。

  • 有一种机制可以使用适当命名的文件覆盖包配置设置。您可以只覆盖您需要的两个条目(角色和权限命名空间)。

回答by Amit

Confide and Entrust both are looking for Role and Permission model in global namespace. As you have changed namespace of both these models, Confide and Entrust are not able to find it. To solve this problem, you need to override Entrust configuration.

Confide 和 Entrust 都在寻找全局命名空间中的 Role 和 Permission 模型。由于您更改了这两个模型的命名空间,Confide 和 Entrust 无法找到它。要解决此问题,您需要覆盖 Entrust 配置。

  • Create directory "app/config/packages/zizaco/entrust"
  • Copy file "vendor/zizaco/entrust/src/config/config.php" to "app/config/packages/zizaco/entrust/config.php"
  • Edit "app/config/packages/zizaco/entrust/config.php" and change following two lines 'role' => 'App\Modules\Core\Role', 'permission' => 'App\Modules\Core\Permission',

  • php artisan clear-compiled

  • php artisan optimize
  • 创建目录“app/config/packages/zizaco/entrust”
  • 将文件“vendor/zizaco/entrust/src/config/config.php”复制到“app/config/packages/zizaco/entrust/config.php”
  • 编辑“app/config/packages/zizaco/entrust/config.php”并更改以下两行'role' => 'App\Modules\Core\Role', 'permission' => 'App\Modules\Core\Permission' ,

  • php artisan 清晰编译

  • php工匠优化

回答by Andreyco

You should rely on Laravel's packages (created thru Workbench, for instance) while developing localy.
Packages are the primary way of adding functionality to Laravel.

在开发 localy 时,您应该依赖 Laravel 的包(例如,通过 Workbench 创建)。
包是向 Laravel 添加功能的主要方式。

Workbench packages and their classes are handled automaticaly by Laravel - no need to configure anything.

工作台包及其类由 Laravel 自动处理 - 无需配置任何东西。

More informations here: http://laravel.com/docs/packages

更多信息在这里:http: //laravel.com/docs/packages

回答by Gadoma

if you want to use your own modules instead of standard workbench packages, check out this article that depics how to achieve that:

如果您想使用自己的模块而不是标准的工作台包,请查看这篇文章,其中描述了如何实现这一点:

http://creolab.hr/2013/05/modules-in-laravel-4/

http://creolab.hr/2013/05/modules-in-laravel-4/

summary by the author ( Boris Strahija):

作者 ( Boris Strahija) 的总结:

Laravel 4 is heavily based on composer packages, which is a good thing, but sometimes developers (like myself) like to separate their code into modules. This is especially nice when building larger projects. Now this was fairly easy to do in Laravel 3 with the bundles system, but in Laravel 4 many people just recommend building packages since L4 has a nice workbench feature. This is all good, but sometimes I like to separate my app specific controllers and views into modules, and not have to go through it with the workbench.

Laravel 4 很大程度上基于 composer 包,这是一件好事,但有时开发人员(如我自己)喜欢将他们的代码分成模块。这在构建大型项目时尤其有用。现在这在 Laravel 3 中使用 bundles 系统相当容易,但在 Laravel 4 中,许多人只推荐构建包,因为 L4 有一个很好的工作台功能。这一切都很好,但有时我喜欢将我的应用程序特定的控制器和视图分成模块,而不必使用工作台来完成它。

In short, you have to

简而言之,你必须

  • put your modules code somwhere (for example /app/modules/)
  • include the directory in composer.json file, under autoload/classmap
  • create an appropriate service providers (Laravel 4 uses service providers to register and boot up the packages, you can use it with modules as well)
  • register the service providers - add them to app config in “app/config/app.php” under the providers array
  • 把你的模块代码放在某个地方(例如 /app/modules/)
  • 在 composer.json 文件中包含目录,在 autoload/classmap 下
  • 创建一个合适的服务提供者(Laravel 4 使用服务提供者来注册和启动包,你也可以将它与模块一起使用)
  • 注册服务提供者 - 将它们添加到应用程序配置中的“app/config/app.php”中的提供者数组下

So now we have out modules fully working. You can add module specific routes, group your controllers/views/models, get module configuration like this:

所以现在我们有完全正常工作的模块。您可以添加特定于模块的路由,对控制器/视图/模型进行分组,获得如下模块配置:

Config::get('content::channels');

Or get translated phrases like this:

或者得到这样的翻译短语:

Lang::get('shop::errors.no_items_in_cart');

Finally, to test your modules you can create some routes, but that is up to you how you use your code.

最后,为了测试您的模块,您可以创建一些路由,但这取决于您如何使用代码。

回答by Travelling Samaritan

If you look at Doctrine composer.json, for example /vendor/doctrine/cache/composer.json:

如果您查看 Doctrine composer.json,例如/vendor/doctrine/cache/composer.json

"autoload": {
    "psr-0": { "Doctrine\Common\Cache\": "lib/" }
},

The files are located in:

这些文件位于:

/vendor/doctrine/cache/lib/Doctrine/Common/Cache/ArrayCache.php
/vendor/doctrine/cache/lib/Doctrine/Common/Cache/Cache.php
... and so on

By that logic, I think you should put your files in:

按照这个逻辑,我认为你应该把你的文件放在:

modules/App/Modules/Core/User.php
modules/App/Modules/Core/Permission.php
modules/App/Modules/Core/Role.php

回答by Nenad

From you screenshot the Permission class is located in the modelsfolder so when you include your namespace you should type

从你的截图中,权限类位于models文件夹中,所以当你包含你的命名空间时,你应该输入

use App\Modules\Core\Models\Permission;