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

前端 未结 8 1392
暗喜
暗喜 2021-01-01 19:39

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.

相关标签:
8条回答
  • 2021-01-01 19:59

    update your composer first it will fix the errors

     $ composer update
    
    0 讨论(0)
  • 2021-01-01 20:00

    Add this line top of AgentServiceProvider.php

    use Agent;

    like:

    namespace App\Providers;
    use Illuminate\Support\ServiceProvider;
    use Agent;
    

    Source

    0 讨论(0)
  • 2021-01-01 20:05

    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.

    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.

    Next I created a HelperServiceProvider.php by running the artisan command:

    artisan make:provider HelperServiceProvider or php artisan make:provider HelperServiceProvider Within the register method I added this snippet

    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

    'providers' => [ 'App\Providers\HelperServiceProvider', ] now any file in your Helpers directory is loaded, and ready for use.

    UPDATE 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 require browner12/helpers

    0 讨论(0)
  • 2021-01-01 20:11

    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;
      }
    
    }
    
    0 讨论(0)
  • 2021-01-01 20:14

    I have the same issue.

    The problem was in the file: ./bootstrap/cache/config.php

    I have removed it and everything starts to work: rm ./bootstrap/cache/config.php

    My infrastructure located in docker I have run that command in the PHP container.

    0 讨论(0)
  • 2021-01-01 20:15

    Execute the command in your project root path

    composer dump-autoload

    0 讨论(0)
提交回复
热议问题