6

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.

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

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',

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

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

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']
        );
    });

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();
}

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??

Any help would be greatly appreciated.

Community
  • 1
  • 1
Chris Schmitz
  • 20,160
  • 30
  • 81
  • 137
  • Not an answer as such - but your approach looks good to me. The only thing is once you need to start altering packages to suit your needs, perhaps its better to just write your own implementation - especially with something so vital and unique as your users. – Laurence May 21 '14 at 17:15
  • Thanks for the comment. I considered writing my own implementation, but this is my first laravel project so I don't know that I could do it as reliably just yet. I think if I use Sentry in the future (which is likely), I'll be writing my own implementation using Sentinel as an example. – Chris Schmitz May 21 '14 at 19:04
  • Just a heads up for anyone using the Sentinel bundle; I messaged rydurham on github and he released a new version of the bundle that allows you to create additional fields via the config file. https://github.com/rydurham/Sentinel/commit/d5ab52eced644651555809b53210934507c5f0e8 – Chris Schmitz May 21 '14 at 20:51

1 Answers1

0

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"
    }
],

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

darrylkuhn
  • 1,318
  • 1
  • 9
  • 9