在Laravel 4上使用非Laravel软件包 [英] Using a non-laravel package on Laravel 4

查看:59
本文介绍了在Laravel 4上使用非Laravel软件包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在框架中包含不是专门为L4设计的软件包? 如果是这样,怎么做?我知道我需要将该软件包添加到我的composer.json中,这会将它添加到vendor文件夹中,但是我可以以某种方式在providers数组中注册它吗?还有其他步骤吗?

Is it possible to include a package that was not specifically designed for L4 in the framework? If so, how is it done? I know I need to add the package to my composer.json which adds it to the vendor folder, but can I register it somehow in the providers array? are there any other steps necessary?

我想使用最初为Yii设计的 Google Checkout程序

I would like to use the Google checkout package originally designed for Yii

推荐答案

在Laravel 4中使用第三方作曲家程序包

开发人员创建作曲家软件包时,应使用PSR-0或PSR-4标准映射自动加载.如果不是这种情况,在Laravel应用程序中加载软件包可能会出现问题. PSR-0标准是:

When developers create composer packages, they should map the auto-loading using PSR-0 or PSR-4 standards. If this is not the case there can be issues loading the package in your Laravel application. The PSR-0 standard is:

{
    "autoload": {
        "psr-0": { "Acme": "src/" }
    }
}

和PSR-4标准:

{
    "autoload": {
         "psr-4": { "Acme\\": "src/" }
    }
}

基本上,以上是告诉作曲家在何处查找以名称分隔的文件的标准.如果您不使用自己的名称空间,则无需配置其他任何内容.

Basically the above is a standard for telling composer where to look for name-spaced files. If you are not using your own namespaces you dont have to configure anything else.

场景1

SCENARIO 1

Laravel中的PSR-0标准以下软件包(带有自动加载类映射)

这是一个简单的例子,例如,我将使用facebook php sdk,可以找到它:

This is a simple one, and for example i will use the facebook php sdk, that can be found:

https://packagist.org/packages/facebook/php-sdk

第1步:

将软件包包括在composer.json文件中.

Include the package in your composer.json file.

"require": {
    "laravel/framework": "4.0.*",
    "facebook/php-sdk": "dev-master"
}

第2步:

run: composer update

第3步:

由于facebook软件包使用了一个类映射,因此可以立即使用,因此可以立即开始使用该软件包. (下面的代码示例直接来自普通视图.请不要在生产应用程序的视图中使用逻辑.)

Because the facebook package uses a class map its working out of the box, you can start using the package instantly. (The code example below comes straight from a normal view. Please keep your logic out from views in your production app.)

$facebook = new Facebook(array(
    'appId'  => 'secret',
    'secret' => 'secret'
));
var_dump($facebook); // It works!

场景2

SCENARIO 2

在此示例中,我将使用instagram php API中的包装器.在这里需要进行一些调整以加载程序包.试一试吧! 该软件包可以在这里找到:

For this example i will use a wrapper from the instagram php api. Here there need to be made some tweaks to get the package loaded. Lets give it a try! The package can be found here:

https://packagist.org/packages/fishmarket/instaphp

第1步:

添加到composer .json

Add to composer .json

"require": {
    "laravel/framework": "4.0.*",
    "fishmarket/instaphp": "dev-master"
}

然后您就可以正常更新(作曲者更新)

Then you can update normally (composer update)

接下来,请尝试使用该包,就像处理Facebook包一样.同样,这只是视图中的代码.

Next try to use the package like you did with the facebook package. Again, this is just code in a view.

$instagramconfig = array(
    'client_id' => 'secret',
    'client_secret'=> 'secret',
    'access_token' => 'secret'
);

$api = Instaphp::Instance(null, $instagramconfig);                
var_dump($api); // Epic fail!

如果尝试上述示例,则会出现此错误:

If you try the above example you will get this error:

FatalErrorException: Error: Class 'Instaphp' not found in ...

因此,我们需要解决此问题.为此,我们可以检查instagram composer.json,它的自动加载与Facebook php sdk的自动加载有所不同.

So we need to fix this issue. To do this we can examine the instagram composer.json, that has its autoload diffrent than the facebook php sdk had.

"autoload": {
         "psr-0": { "Instaphp": "." }
    }

与facebook composer.json相比:

Compared to the facebook composer.json:

"autoload": {
        "classmap": ["src"]
    }

(Composer处理各种自动加载,从文件和类映射到PSR.请查看您的vendor/composer/文件夹以了解其工作方式.)

(Composer handles different kinds of autoloading, from files and class-maps to PSR. Take a look at your vendor/composer/ folder to see how its done.)

现在,我们将不得不手动加载该类.简单,只需添加以下内容(在控制器,模型或视图的顶部):

Now we will have to load the class, manually. Its easy, just add this (top of your controller, model or view):

use Instaphp\Instaphp;

composer dump-autoload,它有效!

composer dump-autoload, and it works!

第二步(可选)

另一种方法是(如果您不想使用"use"语句,则可以简单地告诉作曲家直接从代码中查找文件.只需按以下方式更改实例:

Another method is (if you dont want to use the "use" statement, you can simply tell composer to look for the files straight from your code. Just change the Instance like so:

// reference the name-spaced class straight in the code
$api = Instaphp\Instaphp::Instance(null, $instagramconfig); 
var_dump($api); // It works

但是,我建议使用use语句向其他开发人员(和您将来的自己)清楚说明程序中使用了哪些(外部)类/程序包.

However I suggest using the usestatement to make it clear to other developers (and your future self) what (external) classes/packages are used in the program.

场景3

SCENARIO 3

在这里,我们使用内置在IOC容器中的Laravels注册服务提供商.请注意,某些软件包可能不适合此方法.我将使用与场景2中相同的Instagram包.

Here we use the Laravels built in IOC container to register service providers. Please note that some packages might not be suitable for this method. I will use the same Instagram package as in scenario 2.

又快又脏

如果您不关心设计模式和服务提供者,则可以绑定这样的类:

If you don't care about design patterns and service providers you can bind a class like this:

App::bind('Instaphp', function($app)
{
    return new Instaphp\Instaphp;
});

您可以像这样解决它.

App::make('Instaphp');

快速而肮脏的结尾

如果您正在从事更大的项目,并且使用了接口,则可能应该进一步抽象绑定.

If you're working on a bigger project, and you make use of interfaces you should probably abstract the bindings further.

第1步:

在您的应用文件夹中创建一个文件夹,例如"providers"文件夹.

Create a folder inside your app folder, for example a 'providers' folder.

app/providers

确保Laravel自动加载该文件夹,您可以将一些其他信息传递给composer.json,如下所示:

Make sure Laravel auto-loads that folder, you can pass in some additional info to composer.json, like this:

"autoload": {

    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php",
        "app/providers" // this was added
    ]

},

现在在名为Instagram.php的新文件夹中创建一个文件,并将其放置在其中:

Now create a File inside the new folder called Instagram.php and place this inside:

 <?php

    use Illuminate\Support\ServiceProvider;

    class InstagramServiceProvider extends ServiceProvider {

    public function register()
    {
        $this->app->bind('Instaphp', function()
        {
            return new Instaphp\Instaphp;
        });

    }

}

现在再次运行composer dump-autoload,就可以使用该软件包了.请注意,instagram程序包具有final private function __construct(),这意味着您不能在原始类之外使用该程序包,而无需将构造方法更改为public.我并不是说这是一个好习惯,对于instagram程序包,我建议使用方案2.

Now run composer dump-autoload again, and you can use the package. Note that the instagram package has a final private function __construct(), this means you cannot use that package outside the original class without changing the construct method to public. I'm not saying this is a good practice, and i suggest to use the scenario 2, in the case of the instagram package.

无论如何,在此之后,您可以使用如下软件包:

Anyway, after this you can use the package like this:

$instagramInstance = App::make('Instaphp');

$instagramconfig = array(
    'client_id' => 'secret',
    'client_secret'=> 'secret',
    'access_token' => 'secret'
 );

$instagram = new $instagramInstance();
$userfeed = $instagram->Users->feed($instagramconfig);

var_dump($userfeed); // It works!

这篇关于在Laravel 4上使用非Laravel软件包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆