Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
336 views
in Technique[技术] by (71.8m points)

php - How to autoload 'libraries' in laravel 4?

I've created a library folder in app folder to add my app libraries. I've updated app config file and composer.json to autoload that folder, but when I run the command composer dump-autoload I get the next error:

{"error":{"type":"Symfony\Component\Debug\Exception\FatalErrorException","message":"Class 'App\Libraries\Search\SearchServiceProvider' not found","file":"D:\Users\Miguel Borges\Documents\Trabalhos\Tese\portal\bootstrap\compiled.php","line":4130}}PHP Fatal error: Class 'AppLibrariesSearchSearchServiceProvider' not found in D:UsersMiguel BorgesDocumentsTrabalhosTeseportalootstrapcompiled.php on line 4130 [Finished in 1.1s with exit code 255]

My app folder tree:

app
| ...
+ libraries
| + search
| | - Search.php
| | - SearchFacade.php
| | - SearchServiceProvider.php
| + lib2
| | - ...
| + lib3
| | - ...
| | - Theme.php
| - ...
- filters.php
- routes.php

SearchServiceProvider.php

namespace AppLibrariesSearch;

use IlluminateSupportServiceProvider;

class SearchServiceProvider extends ServiceProvider {

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->app['search'] = $this->app->share(function($app)
        {
            return new Search;
        });
    }

}

composer.json

    "autoload": {
        "classmap": [
            "app/commands",
            "app/controllers",
            "app/models",
            "app/libraries",
            "app/database/migrations",
            "app/database/seeds",
            "app/tests/TestCase.php"
        ]
        // ,
  //       "psr-0": {
  //           "app": "app/libraries"
  //       }
    },

Basically, I need to autoload all the libraries within the 'libraries' folder.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should create a top-level namespace for your application.

Then put all libraries you code under that namespace. Note: Any third-party libraries should (hopefully) be installed via Composer and therefore have its own namespace/autoloading setup.

Your directory structure would then be:

libraries
    Myapp
        Search (note directory is capitalized)
            Search.php
            SearchFacade.php
            SearchServiceProvider.php
        AnotherLib

Then your classes will follow that namespace:

File: Myapp/Search/Search.php:

<?php namespace MyappSearch;

class Search { ... }

And finally, your autoloading setup:

"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",
        "app/libraries",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php"
    ]
    ,
    "psr-0": {
         "Myapp": "app/libraries"
    }
},

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...