Renish Khunt Renish Khunt - 1 month ago 14
PHP Question

Laravel Class 'App\Modules\ServiceProvider' not found?

Hello Friends I am new in Laravel framework.

i create modules directory in app folder.

then i also create ServiceProvider.php file in modules directory.

my file structure like.

app\modules\ServiceProvider.php


This is code of 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.

This is ServiceProvider.php file code.

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

"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.

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


please tell where i doing a mistake.

thank you.

Answer

I try run your code, and everything works great.

It's a new installation of Laravel 4.1

Obs.: check your 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;
  }

}
Comments