Laravel 4 中的自动加载类图

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

Autoload Classmap in Laravel 4

phplaravellaravel-4autoloader

提问by Patrick Maciel

I have a:

我有一个:

// administration
app/controller/admin/ProjectsController.php

And I want use too:

我也想使用:

// public in website
app/controller/ProjectsController.php

But, in autoload_classmap.php, it's registred like that:

但是,在 autoload_classmap.php 中,它是这样注册的:

'ProjectsController' => 'app/controller/admin/ProjectsController'

So, If I want one more 'ProjectsController' for public views, how I need to do?
What is better? 2 controllers (admin and public), or one (hybrid).

那么,如果我想要一个更多的“ProjectsController”供公众查看,我需要怎么做?
什么是更好的?2 个控制器(管理员和公共),或一个(混合)。

Thanks.

谢谢。

回答by Aran

You should namespace your Admin controllers.

您应该命名您的 Admin 控制器。

That way it'll match up to PSR and autoloader will treat them differently.

这样它就会匹配 PSR 并且自动加载器会以不同的方式对待它们。

namespace Admin;

namespace Admin;

At the top of your admin files.

在您的管理文件的顶部。

Edit:

编辑:

It may even be worth namespacing all your controllers and models.

甚至可能值得为您的所有控制器和模型命名。

So for your ProjectControllerin app\controllersyou could put

因此,对于你ProjectControllerapp\controllers,你可以把

namespace ProjectName

namespace ProjectName

Then for everything in subfolders, e.g. app\controllers\admin

然后对于子文件夹中的所有内容,例如 app\controllers\admin

namespace ProjectName\Admin

namespace ProjectName\Admin

and so on for other folders, and files.

等等其他文件夹和文件。

This'll reduce the likely hood of your code clashing with anything else.

这将减少您的代码与其他任何内容发生冲突的可能性。

Edit: Edit:

编辑: 编辑:

After namespacing classes you'll need to reference classes and functions that are outside your namespace. For example Controller belongs to the global namespace so you put \ at the start of Controller.

在命名空间类之后,您需要引用命名空间之外的类和函数。例如 Controller 属于全局命名空间,因此您将 \ 放在 Controller 的开头。

The documentation here should help out a lot. PHP Namespaces

这里的文档应该有很大帮助。PHP 命名空间