dsag14654 2014-04-18 17:28
浏览 32
已采纳

Laravel:扩展已安装的Bundle的用户模型

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条回答 默认 最新

  • duanhun3273 2014-04-18 17:48
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等