Laravel:重写捆绑包的服务提供者 [英] Laravel: Overriding a Bundle's Service Providers

查看:56
本文介绍了Laravel:重写捆绑包的服务提供者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目使用Laravel的Sentinel软件包.

I have a project that uses the Sentinel bundle for Laravel.

不久前,我问了一个有关扩展模型的问题.包.我接受的答案有效,但需要在供应商文件夹中编辑捆绑软件的代码.

A while ago, I asked a question about extending a model provided in the bundle. The answer I accepted worked, but it required editing the bundle's code in the vendor folder.

从那时起,我运行了 composer update 命令,并且我应用的更改被覆盖(在此不足为奇).

Since then, I've run a composer update command and the change I applied was overwritten (no surprise there).

我现在对laravel的工作方式有了更多的了解,因此我能够追溯到我的应用程序引用捆绑包服务的终点.在我的 config/app.php 文件中,有服务提供商参考:

I know a bit more about how laravel works now so I was able to trace back the end point where the bundle's services are referenced by my app. In my config/app.php file I have the service provider reference:

'Sentinel\SentinelServiceProvider',

SentinelServiceProvider 服务具有一个 register 方法,该方法注册了我需要覆盖的类 RepoServiceProvider .

The SentinelServiceProvider service has a register method that registers the RepoServiceProvider which is the class I need to override.

我的计划是创建两个新文件: ExtendedSentinelServiceProvider.php ExtendedRepoServiceProvider.php .

My plan is to create two new files: ExtendedSentinelServiceProvider.php and ExtendedRepoServiceProvider.php.

ExtendedRepoServiceProvider.php 文件中,我可以用自定义User类替换用于User Repository绑定的类:

In the ExtendedRepoServiceProvider.php file, I could replace the class being used for the User Repository binding with my custom User class:

    // Bind the User Repository
    $app->bind('Sentinel\Repo\User\UserInterface', function($app)
    {
        return new ExtendedSentryUser( // This is my custom model that I want the ExtendedRepoServiceProvider to use
            $app['sentry']
        );
    });

ExtendedSentinelServiceProvider.php 文件中,我将用新修改的 ExtendedRepoServiceProvider 类替换现有的 RepoServiceProvider 类引用:

In the ExtendedSentinelServiceProvider.php file, I would replace the existing RepoServiceProvider class reference with my newly modified ExtendedRepoServiceProvider class:

public function register()
{
    $loader = \Illuminate\Foundation\AliasLoader::getInstance();
    $loader->alias('Sentry', 'Cartalyst\Sentry\Facades\Laravel\Sentry');

    $repoProvider = new ExtendedRepoServiceProvider($this->app); // This would be my new ExtendedRepoServiceProvider class
    $repoProvider->register();

    $formServiceProvider = new FormServiceProvider($this->app);
    $formServiceProvider->register();
}

然后,我将 config/app.php 文件中的服务提供商引用替换为新的 ExtendedSentinelServiceProvider 类:

And then I would replace the service provider reference in my config/app.php file with the new ExtendedSentinelServiceProvider class:

'providers' => array(
    ...
    'Sentinel\ExtendedSentinelServiceProvider',
    ...
),

我的问题是,这可以将修改存储在我的应用目录中而不是修改供应商代码吗?在名称间隔方面会出现问题吗?有更好的方法吗?

My question is, would this make sense as a way of being able to store my modifications in my app directory rather than modifying the vendor code? Is this going to be a problem when it comes to name spacing? Is there a better way of doing this??

任何帮助将非常感激.

推荐答案

您不应遇到名称间隔问题.但是,另一个选择-特别是如果您要添加对其他人通常有用的功能-可以将项目分叉到github上,然后在composer.json的"repositories"属性中包含对git项目的引用.例如,您可以添加:

You shouldn't run into name spacing issues. However another option - especially if you're adding a feature that might be generally useful for others - would be to fork the project on github and then include a reference to your git project in the "repositories" attribute of your composer.json. For example you could add:

"repositories": [
    {
        "type": "git",
        "url": "git@github.com:{your-user}/Sentinel.git"
    }
],

然后 composer 会在安装/更新时查看您的 fork.

And then composer would look to your fork when ever it does an install/update.

这篇关于Laravel:重写捆绑包的服务提供者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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