如何在 laravel 4 中自动加载“库"?

How to autoload #39;libraries#39; in laravel 4?(如何在 laravel 4 中自动加载“库?)
本文介绍了如何在 laravel 4 中自动加载“库"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 app 文件夹中创建了一个库文件夹来添加我的应用程序库.我已经更新了应用程序配置文件和 composer.json 以自动加载该文件夹,但是当我运行命令 composer dump-autoload 时,我收到下一个错误:

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'未找到","file":"D:\Users\Miguel Borges\Documents\Trabalhos\Tese\portal\bootstrap\compiled.php","line":4130}}PHP 致命错误:第 4130 行的 D:UsersMiguel BorgesDocumentsTrabalhosTeseportalootstrapcompiled.php 中找不到类 'AppLibrariesSearchSearchServiceProvider'[1.1s 完成,退出码 255]

我的应用文件夹树:

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.

推荐答案

您应该为您的应用程序创建一个顶级命名空间.

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

然后将所有库代码放在该命名空间下.注意:任何第三方库都应该(希望)通过 Composer 安装,因此有自己的命名空间/自动加载设置.

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.

您的目录结构将是:

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

然后您的类将遵循该命名空间:

Then your classes will follow that namespace:

文件: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"
    }
},

这篇关于如何在 laravel 4 中自动加载“库"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)