php 找不到 Laravel 类“App\Modules\ServiceProvider”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21379554/
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
Laravel Class 'App\Modules\ServiceProvider' not found?
提问by Renish Khunt
Hello Friends I am new in Laravel framework.
你好朋友我是 Laravel 框架的新手。
i create modules directory in app folder.
我在 app 文件夹中创建模块目录。
then i also create ServiceProvider.php file in modules directory.
然后我还在模块目录中创建 ServiceProvider.php 文件。
my file structure like.
我的文件结构像。
app\modules\ServiceProvider.php
This is code of ServiceProvider.php.
这是ServiceProvider.php 的代码。
<?php
namespace App\Modules;
abstract class ServiceProvider extends \Illuminate\Support\ServiceProvider {
public function boot() {
if ($module = $this->getModule(func_get_args())) {
$this->package("app/" . $module, $module, app_path() . "/modules/" . $module);
}
}
public function register() {
if ($module = $this->getModule(func_get_args())) {
$this->app["config"]->package("app/" . $module, app_path() . "/modules/" . $module . "/config");
// Add routes
$routes = app_path() . "/modules/" . $module . "/routes.php";
if (file_exists($routes))
require $routes;
}
}
public function getModule($args) {
$module = (isset($args[0]) and is_string($args[0])) ? $args[0] : null;
return $module;
}
}
Then i create a new points directory in modules directory.
然后我在模块目录中创建一个新的点目录。
and also create ServiceProvider.php file in points directory.
并在点目录中创建 ServiceProvider.php 文件。
This is ServiceProvider.php file code.
这是 ServiceProvider.php 文件代码。
<?php
namespace App\Modules\Points;
class ServiceProvider extends \App\Modules\ServiceProvider {
public function register() {
parent::register("points");
}
public function boot() {
parent::boot("points");
}
}
Then now i try to load project i got error like.
然后现在我尝试加载项目,我得到了类似的错误。
Class 'App\Modules\ServiceProvider' not found
Symfony\Component\Debug\Exception\FatalErrorException
…/-app/-modules/-points/-ServiceProvider.php5
i also add autoload entery in composer.json file like.
我还在 composer.json 文件中添加了 autoload entery。
"autoload": {
"classmap": [
"app/modules"
]
},
Then also run this command.
然后也运行这个命令。
composer dump-autoload
but then after is not work.
但是之后就不行了。
i also register my ServiceProvide in app.php like.
我也在 app.php 中注册了我的 ServiceProvide。
'providers' => array(
'App\Modules\Points\ServiceProvider'
),
please tell where i doing a mistake.
请告诉我哪里做错了。
thank you.
谢谢你。
回答by Patrick Maciel
I try run your code, and everything works great.
我尝试运行您的代码,一切正常。
It's a new installation of Laravel 4.1
这是 Laravel 4.1 的全新安装
Obs.: check yourvendor/composer/autoload_classmap.php
观察:检查你的vendor/composer/autoload_classmap.php
<?php
// autoload_classmap.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'App\Modules\Points\ServiceProvider' => $baseDir . '/app/modules/points/ServiceProvider.php',
'App\Modules\ServiceProvider' => $baseDir . '/app/modules/ServiceProvider.php',
'BaseController' => $baseDir . '/app/controllers/BaseController.php',
'DatabaseSeeder' => $baseDir . '/app/database/seeds/DatabaseSeeder.php',
'HomeController' => $baseDir . '/app/controllers/HomeController.php',
'IlluminateQueueClosure' => $vendorDir . '/laravel/framework/src/Illuminate/Queue/IlluminateQueueClosure.php',
'SessionHandlerInterface' => $vendorDir . '/symfony/http-foundation/Symfony/Component/HttpFoundation/Resources/stubs/SessionHandlerInterface.php',
'TestCase' => $baseDir . '/app/tests/TestCase.php',
'User' => $baseDir . '/app/models/User.php',
);
composer.json
作曲家.json
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"require": {
"laravel/framework": "4.1.*"
},
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/modules",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
]
},
"scripts": {
"post-install-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"post-update-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"post-create-project-cmd": [
"php artisan key:generate"
]
},
"config": {
"preferred-install": "dist"
},
"minimum-stability": "stable"
}
app.php
应用程序.php
<?php
'providers' => array(
'Illuminate\Foundation\Providers\ArtisanServiceProvider',
'Illuminate\Auth\AuthServiceProvider',
'Illuminate\Cache\CacheServiceProvider',
'Illuminate\Session\CommandsServiceProvider',
'Illuminate\Foundation\Providers\ConsoleSupportServiceProvider',
'Illuminate\Routing\ControllerServiceProvider',
'Illuminate\Cookie\CookieServiceProvider',
'Illuminate\Database\DatabaseServiceProvider',
'Illuminate\Encryption\EncryptionServiceProvider',
'Illuminate\Filesystem\FilesystemServiceProvider',
'Illuminate\Hashing\HashServiceProvider',
'Illuminate\Html\HtmlServiceProvider',
'Illuminate\Log\LogServiceProvider',
'Illuminate\Mail\MailServiceProvider',
'Illuminate\Database\MigrationServiceProvider',
'Illuminate\Pagination\PaginationServiceProvider',
'Illuminate\Queue\QueueServiceProvider',
'Illuminate\Redis\RedisServiceProvider',
'Illuminate\Remote\RemoteServiceProvider',
'Illuminate\Auth\Reminders\ReminderServiceProvider',
'Illuminate\Database\SeedServiceProvider',
'Illuminate\Session\SessionServiceProvider',
'Illuminate\Translation\TranslationServiceProvider',
'Illuminate\Validation\ValidationServiceProvider',
'Illuminate\View\ViewServiceProvider',
'Illuminate\Workbench\WorkbenchServiceProvider',
'App\Modules\Points\ServiceProvider'
),
app/modules/points/ServiceProvider.php
应用程序/模块/点/ServiceProvider.php
<?php
namespace App\Modules\Points;
class ServiceProvider extends \App\Modules\ServiceProvider {
public function register() {
parent::register("points");
}
public function boot() {
parent::boot("points");
}
}
app/modules/ServiceProvider.php
应用程序/模块/ServiceProvider.php
<?php
namespace App\Modules;
abstract class ServiceProvider extends \Illuminate\Support\ServiceProvider {
public function boot() {
if ($module = $this->getModule(func_get_args())) {
$this->package("app/" . $module, $module, app_path() . "/modules/" . $module);
}
}
public function register() {
if ($module = $this->getModule(func_get_args())) {
$this->app["config"]->package("app/" . $module, app_path() . "/modules/" . $module . "/config");
// Add routes
$routes = app_path() . "/modules/" . $module . "/routes.php";
if (file_exists($routes))
require $routes;
}
}
public function getModule($args) {
$module = (isset($args[0]) and is_string($args[0])) ? $args[0] : null;
return $module;
}
}
回答by Andreyco
Add this to composer.jsonautoload section:
将此添加到composer.json自动加载部分:
"psr-4": {
"App\": "app/"
}
and then composer dump-autoload
进而 composer dump-autoload
回答by 112Legion
I have the same issue.
我有同样的问题。
The problem was in the file: ./bootstrap/cache/config.php
问题出在文件中: ./bootstrap/cache/config.php
I have removed it and everything starts to work: rm ./bootstrap/cache/config.php
我已经删除它,一切都开始工作: rm ./bootstrap/cache/config.php
My infrastructure located in docker I have run that command in the PHP container.
我的基础设施位于 docker 中,我已经在 PHP 容器中运行了该命令。
回答by windmaomao
I'm pretty newbie in creating a package. The first time after I created the following structure, I put it in Vendor folder.
我是创建包的新手。第一次创建以下结构后,我将其放在 Vendor 文件夹中。
qplot
environment-color
src
config
QPlot
EnvironmentColor
ColorServiceProvider.php
EnvironmentColor.php
tests
But soon I realized this doesn't make sense, since Laravel won't autoload all the bundles for you unless you registered it. So I moved folder to /app/vendor (new folder).
但很快我意识到这没有意义,因为 Laravel 不会为你自动加载所有包,除非你注册它。所以我将文件夹移动到 /app/vendor (新文件夹)。
And then following Andreyco's suggestion to notify autoload path
然后按照 Andreyco 的建议通知自动加载路径
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/vendor",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
]
},
And then when I run php artisan dump-autoload, and open vendor/composer/autoload_classmap.php, all new classes under QPlot are registered :)
然后当我运行php artisan dump-autoload并打开时vendor/composer/autoload_classmap.php,QPlot 下的所有新类都已注册:)
Now when I go back to add the provider to Laravel /app/config/app.php,
现在,当我回去将提供程序添加到 Laravel 时/app/config/app.php,
'providers' => array(
'QPlot\EnvironmentColor\ColorServiceProvider'
So the steps are
所以步骤是
- setup your package in a folder where your git will cover
- register the autoload in composer
- composer dump-autoload
- add provider to config
- now go back to coding
- 将您的包设置在您的 git 将覆盖的文件夹中
- 在 composer 中注册自动加载
- 作曲家转储自动加载
- 将提供程序添加到配置
- 现在回到编码
回答by Majbah Habib
Execute the command in your project root path
在你的项目根路径执行命令
composer dump-autoload
作曲家转储自动加载
回答by Amol Gholap
my initial thought was the composer autoload as well, but it didn't feel very Laravel 5ish to me. L5 makes heavy use of Service Providers, they are what bootstraps your application.
我最初的想法是 Composer 自动加载也是如此,但我觉得它不是 Laravel 5ish。L5 大量使用服务提供者,它们是引导您的应用程序的工具。
To start off I created a folder in my app directory called Helpers. Then within the Helpers folder I added files for functions I wanted to add. Having a folder with multiple files allows us to avoid one big file that gets too long and unmanageable.
首先,我在我的应用程序目录中创建了一个名为 Helpers 的文件夹。然后在 Helpers 文件夹中,我为要添加的功能添加了文件。拥有一个包含多个文件的文件夹可以让我们避免一个太长且无法管理的大文件。
Next I created a HelperServiceProvider.php by running the artisan command:
接下来我通过运行 artisan 命令创建了一个 HelperServiceProvider.php:
artisan make:provider HelperServiceProvider or php artisan make:provider HelperServiceProvider Within the register method I added this snippet
artisan make:provider HelperServiceProvider 或 php artisan make:provider HelperServiceProvider 在 register 方法中我添加了这个片段
public function register()
{
foreach (glob(app_path().'/Helpers/*.php') as $filename){
require_once($filename);
}
}
lastly register the service provider in your config/app.php in the providers array
最后在您的 config/app.php 中的 providers 数组中注册服务提供者
'providers' => [ 'App\Providers\HelperServiceProvider', ] now any file in your Helpers directory is loaded, and ready for use.
'providers' => [ 'App\Providers\HelperServiceProvider', ] 现在您的 Helpers 目录中的任何文件都已加载,可以使用了。
UPDATE 2016-02-22
更新 2016-02-22
There are a lot of good options here, but if my answer works for you, I went ahead and made a package for including helpers this way. You can either use the package for inspiration or feel free to download it with Composer as well. It has some built in helpers that I use often (but which are all inactive by default) and allows you to make your own custom helpers with a simple Artisan generator. It also addresses the suggestion one responder had of using a mapper and allows you to explicitly define the custom helpers to load, or by default, automatically load all PHP files in your helper directory. Feedback and PRs are much appreciated!
这里有很多不错的选择,但是如果我的回答对您有用,我会继续制作一个包,以这种方式包含助手。您可以使用该包获取灵感,也可以随意使用 Composer 下载它。它有一些我经常使用的内置助手(但默认情况下都是不活动的),并允许您使用简单的 Artisan 生成器创建自己的自定义助手。它还解决了响应者使用映射器的建议,并允许您明确定义要加载的自定义帮助程序,或者默认情况下自动加载您的帮助程序目录中的所有 PHP 文件。非常感谢反馈和 PR!
composer require browner12/helpers
作曲家需要 browner12/helpers

