Laravel: Extending an installed Bundle's User Model

让人想犯罪 __ 提交于 2019-12-12 01:44:44

问题


I've installed the Sentinel laravel bundle into my project.

I've published the packages views and added several fields to the registration form. I wanted those fields to write to my users database table.

I found the store() method of the SentryUser class in the SentryUser.php file within the bundle's directories and saw the line:

$user = $this->sentry->register(array('email' => e($data['email']), 'password' => e($data['password'])));

I modified this mapping to include one of my additional fields:

$user = $this->sentry->register(array('email' => e($data['email']), 'password' => e($data['password']), 'first_name' => e($data['first_name']) ));

Which totally works.

So now that I know where the data needs to be passed in, I want to remove those changes, create a new extendedSentryUser.php file in my app\model directory, and extend the SentryUser:

use Sentinel\Repo\User\SentryUser;

<?php namespace Apps\Models;

use Sentinel\Repo\User\SentryUser;


class extendedSentryUser extends SentryUser {

    protected $sentry;

    /**
     * Construct a new SentryUser Object
     */
    public function __construct(SentryUser $sentry)
    {
        $this->sentry = $sentry;
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store($data)
    {

        $result = array();
        try {
            //Attempt to register the user. 
            $user = $this->sentry->register(array('email' => e($data['email']), 'password' => e($data['password']), 'first_name' => e($data['first_name']) ));

            // Add the new user to the specified default group(s).
            $defaultUserGroups = Config::get('Sentinel::config.default_user_groups');

            foreach ($defaultUserGroups as $groupName) {
                $group = $this->sentry->getGroupProvider()->findByName($groupName);
                $user->addGroup($group);
            }

            //success!
            $result['success'] = true;
            $result['message'] = trans('Sentinel::users.created');
            $result['mailData']['activationCode'] = $user->GetActivationCode();
            $result['mailData']['userId'] = $user->getId();
            // $result['mailData']['first_name'] = e($data['first_name']);
            $result['mailData']['email'] = e($data['email']);
        }
        catch (\Cartalyst\Sentry\Users\LoginRequiredException $e)
        {
            $result['success'] = false;
            $result['message'] = trans('Sentinel::users.loginreq');
        }
        catch (\Cartalyst\Sentry\Users\UserExistsException $e)
        {
            $result['success'] = false;
            $result['message'] = trans('Sentinel::users.exists');
        }

        return $result;
    }
}

The thing I'm hung up on is how do I get my application to use this model instead of the existing one? Am I on the completely wrong path???


回答1:


Look at the following file: / src / Sentinel / Repo / RepoServiceProvider.php

At the top of the file, replace use Sentinel\Repo\User\SentryUser; with your own class, which looks it should be use Apps\Models\extendedSentryUser;

Then down on line 26, modify the code to use your class instead:

// Bind the User Repository
        $app->bind('Sentinel\Repo\User\UserInterface', function($app)
        {
            //change SentryUser to extendedSentryUser
            return new extendedSentryUser(
                $app['sentry']
            );
        });

Also, you probably want your extendedSentryUser's constructor to just call the parent's construct via parent::__construct.

haven't tested this but it should work. You might need to do a composer dumpautoload as well.



来源:https://stackoverflow.com/questions/23159050/laravel-extending-an-installed-bundles-user-model

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!