Laravel:扩展已安装的Bundle用户模型 [英] Laravel: Extending an installed Bundle's User Model
问题描述
我已经将 Sentinel laravel捆绑包安装到我的项目中.
I've installed the Sentinel laravel bundle into my project.
我已经发布了软件包视图,并在注册表单中添加了几个字段.我希望这些字段写入我的 users
数据库表.
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.
我在捆绑软件目录的 SentryUser.php
文件中找到了 SentryUser
类的 store()
方法,并看到了以下一行:
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']) ));
完全有效.
现在,我知道需要在哪里传递数据了,我想删除这些更改,在我的 app \ model
中创建一个新的 extendedSentryUser.php
文件目录,并扩展 SentryUser
:
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
:
使用Sentinel \ Repo \ User \ 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???
推荐答案
查看以下文件:/src/Sentinel/Repo/RepoServiceProvider.php
在文件顶部,将 use Sentinel \ Repo \ User \ SentryUser;
替换为您自己的类,看起来应该是 use Apps \ Models \ extendedSentryUser;
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;
然后在第26行,修改代码以改为使用您的课程:
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']
);
});
此外,您可能希望您的ExtendedSentryUser的构造函数仅通过 parent :: __ construct
调用父级的构造.
Also, you probably want your extendedSentryUser's constructor to just call the parent's construct via parent::__construct
.
还没有测试过,但是应该可以.您可能还需要执行 composer dumpautoload
.
haven't tested this but it should work. You might need to do a composer dumpautoload
as well.
这篇关于Laravel:扩展已安装的Bundle用户模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!