gpt4 book ai didi

php - Laravel 类 'App\Modules\ServiceProvider' 未找到?

转载 作者:行者123 更新时间:2023-12-03 00:51:36 27 4
gpt4 key购买 nike

friend 们大家好,我是 Laravel 框架的新手。

我在应用程序文件夹中创建模块目录。

然后我还在模块目录中创建 ServiceProvider.php 文件。

我的文件结构如下。

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;
}

}

然后我在模块目录中创建一个新的点目录。

并在点目录中创建 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");
}

}

然后现在我尝试加载项目,但出现错误。

Class 'App\Modules\ServiceProvider' not found 

Symfony\Component\Debug\Exception\FatalErrorException
…/­app/­modules/­points/­ServiceProvider.php5

我还在composer.json 文件中添加了自动加载输入。

"autoload": {
"classmap": [
"app/modules"
]
},

然后也运行此命令。

composer dump-autoload

但是之后就不行了。

我也在app.php中注册了我的ServiceProvide。

'providers' => array(
'App\Modules\Points\ServiceProvider'
),

请告诉我哪里做错了。

谢谢。

最佳答案

我尝试运行你的代码,一切都很好。

这是 Laravel 4.1 的全新安装

观察:检查您的 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

{
"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
'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

<?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

<?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;
}

}

关于php - Laravel 类 'App\Modules\ServiceProvider' 未找到?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21379554/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com