php 如何在codeigniter中使用composer包?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13741917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:59:21  来源:igfitidea点击:

How to use composer packages in codeigniter?

phpnamespacescodeigniter-2composer-php

提问by Tjorriemorrie

I've followed this article: http://philsturgeon.co.uk/blog/2012/05/composer-with-codeigniter

我已经关注了这篇文章:http: //philsturgeon.co.uk/blog/2012/05/composer-with-codeigniter

But I get Fatal error: Class 'Buzz\Browser' not found.

但我明白了Fatal error: Class 'Buzz\Browser' not found

What is missing from his post?

他的帖子缺少什么?

My controller or application isn't namespaced. I was hoping to just be able to use that one package in one controller's action in a non-namespaced framework.

我的控制器或应用程序没有命名空间。我希望能够在非命名空间框架中的一个控制器的操作中使用那个包。

回答by SeanWM

For CodeIgniter 3.x and composer, it's suggestedto just set $config['composer_autoload']to TRUEor a custom path in application/config/config.php.

对于笨3.x和作曲家,它的建议只是集$config['composer_autoload']TRUE或自定义路径application/config/config.php

It seems that CI assumes the vendordirectory is within the applicationdirectory. That wasn't my case. I did the following:

CI 似乎假定vendor目录在application目录中。那不是我的情况。我做了以下事情:

$config['composer_autoload'] = 'vendor/autoload.php';

$config['composer_autoload'] = 'vendor/autoload.php';

回答by Tjorriemorrie

Credit to @jmadsen

归功于@jmadsen

This is possible by just getting the order of loading correct:

这可以通过正确获取加载顺序来实现:

/*
 * --------------------------------------------------------------------
 * LOAD THE BOOTSTRAP FILE
 * --------------------------------------------------------------------
 *
 * And away we go...
 *
 */
// Composer Autoloader
require FCPATH . 'vendor/autoload.php';

require_once BASEPATH.'core/CodeIgniter.php';

/* End of file index.php */

回答by TunaMaxx

EDIT: Damn, I just said pretty much exactly the same thing as @Tjorriemorrie

编辑:该死的,我刚刚和@Tjorriemorrie 说的几乎完全一样

If you've followed all the other directions correctly, all you need to do is add the following code near the very the endyour index.php file:

如果您正确地遵循了所有其他说明,那么您需要做的就是在 index.php 文件的末尾附近添加以下代码:

/*
 * --------------------------------------------------------------------
 * COMPOSER AUTOLOAD
 * --------------------------------------------------------------------
 */
include_once './vendor/autoload.php';

...just make sure you slot it in beforethe CodeIgniter Bootstrap file is called:

...只需确保在调用 CodeIgniter Bootstrap 文件之前将其插入:

/*
 * --------------------------------------------------------------------
 * LOAD THE BOOTSTRAP FILE
 * --------------------------------------------------------------------
 *
 * And away we go...
 *
 */
 require_once BASEPATH.'core/CodeIgniter.php';

回答by Kwame Oteng Appiah-Nti

Well in Codeigniter3.x you can easily do that by going into the application/config/config.php and look for this line

那么在 Codeigniter3.x 中,您可以通过进入 application/config/config.php 并查找这一行来轻松做到这一点

$config['composer_autoload'] = FALSE;

Make sure you set it to TRUE and right after it you require_once this:

确保将其设置为 TRUE 并在它之后您 require_once :

require_once APPPATH.'vendor/autoload.php';

So you get something like this:

所以你会得到这样的东西:

/*
|--------------------------------------------------------------------------
| Composer auto-loading
|--------------------------------------------------------------------------
|
| Enabling this setting will tell CodeIgniter to look for a Composer
| package auto-loader script in application/vendor/autoload.php.
|
|   $config['composer_autoload'] = TRUE;
|
| Or if you have your vendor/ directory located somewhere else, you
| can opt to set a specific path as well:
|
|   $config['composer_autoload'] = '/path/to/vendor/autoload.php';
|
| Note: This will NOT disable or override the CodeIgniter-specific
|   autoloading (application/config/autoload.php)
*/
$config['composer_autoload'] = TRUE;
require_once APPPATH.'vendor/autoload.php';

Just make sure you have your vendor folder in the application folder and you are good to go.

只要确保您的应用程序文件夹中有您的供应商文件夹,您就可以开始了。

I recently found out that you can just set $config['composer_autoload'] = TRUE; and put your vendor folder in the application folder and that's it.

我最近发现你可以设置 $config['composer_autoload'] = TRUE; 并将您的供应商文件夹放在应用程序文件夹中,就是这样。

For those who would want your vendor folder outside the application folder. You can make it happen in this way: for example you want to place it in the root folder.

对于那些希望您的供应商文件夹位于应用程序文件夹之外的人。您可以通过这种方式实现:例如,您想将其放在根文件夹中。

TIP: it has been described in the comment already

提示:它已经在评论中描述了

$config['composer_autoload'] = '/path/to/vendor/autoload.php';

$config['composer_autoload'] = '/path/to/vendor/autoload.php';

/*
|--------------------------------------------------------------------------
| Composer auto-loading
|--------------------------------------------------------------------------
|
| Enabling this setting will tell CodeIgniter to look for a Composer
| package auto-loader script in application/vendor/autoload.php.
|
|   $config['composer_autoload'] = '/path/to/vendor/autoload.php';
|
| Note: This will NOT disable or override the CodeIgniter-specific
|   autoloading (application/config/autoload.php)
*/
$config['composer_autoload'] = FCPATH .'vendor/autoload.php';

Where FCPATH is codeigniter's defined constant for the root folder.

其中 FCPATH 是 codeigniter 为根文件夹定义的常量。

I guess this helps.

我想这有帮助。

回答by Kinobi

You can add directly the Composer Autoloder in your controller:

您可以直接在控制器中添加 Composer Autoloder:

// Composer Autoloader
require FCPATH.'vendor/autoload.php';

回答by DurgVijay Kashyap

There are two ways you can autoload the class file which is required using composer.

有两种方法可以自动加载使用 Composer 所需的类文件。

  1. Add below line in index.php in the root directory.

    require FCPATH . 'vendor/autoload.php';
    
  2. Or autoload directly in the controller where you want to use.

    defined('BASEPATH') OR exit('No direct script access allowed');
    
    require FCPATH . 'vendor/autoload.php';
    
    class Home extends CI_Controller {...}
    
  1. 在根目录下的 index.php 中添加以下行。

    require FCPATH . 'vendor/autoload.php';
    
  2. 或者直接在您要使用的控制器中自动加载。

    defined('BASEPATH') OR exit('No direct script access allowed');
    
    require FCPATH . 'vendor/autoload.php';
    
    class Home extends CI_Controller {...}
    

回答by Jahmic

I'm using Kenjis codeigniter composer package, and it puts the vendor directory off of the root. Since there is no predefined constant (that I know of) for the root, I used the following:

我正在使用Kenjis codeigniter composer package,它将供应商目录置于根目录之外。由于根没有预定义的常量(我知道),因此我使用了以下内容:

$root = getcwd();
$config['composer_autoload'] = "$root/vendor/autoload.php";