使用外部来源的数据进行注册
Registration using data from external sources
我最近刚拿起 Laravel。我正在尝试更改 Registrar.php
以满足我在 Laravel 5 中的需要。
报名流程如下:
- 要求用户输入用户名和密码
- 使用这些详细信息向外部服务发出请求(可能需要大约 20 秒)
- 检查提供的详细信息是否正确,如果没有则抛出错误
- 从外部服务检索信息并创建帐户
我无法一步到位,我需要先从服务中获取信息。
- Laravel 是否有外观来发出 cURL 请求?
- 如何抛出自定义错误?
- 我可以先创建一个用户,然后再添加信息吗?怎么样?
在 vanilla PHP 中,我会发起一个 AJAX 调用来启动请求并允许用户在 AJAX 调用返回数据后继续登录。
您可以执行以下操作:
- 对于 cURL 请求,我建议使用 Guzzle(Laravel 没有专门的 Facade 或服务提供商)。
- 要处理 Laravel 的错误,请检查 Errors Documentation。
- 为了处理用户 create/update,Laravel 已经附带了一个用户 Eloquent 模型,您可以使用它来创建和更新帐户,看起来像这样:
// Create the new user
$user = User::create([ /* array with details goes here */ ]);
// Do your other stuff...
// Update the user's other details
$user->detail = "Some detail"; // add as many columns as you like
$user->save();
您可以在 Eloquent Documentation 中找到有关使用模型的任何其他详细信息。
我最近刚拿起 Laravel。我正在尝试更改 Registrar.php
以满足我在 Laravel 5 中的需要。
报名流程如下:
- 要求用户输入用户名和密码
- 使用这些详细信息向外部服务发出请求(可能需要大约 20 秒)
- 检查提供的详细信息是否正确,如果没有则抛出错误
- 从外部服务检索信息并创建帐户
我无法一步到位,我需要先从服务中获取信息。
- Laravel 是否有外观来发出 cURL 请求?
- 如何抛出自定义错误?
- 我可以先创建一个用户,然后再添加信息吗?怎么样?
在 vanilla PHP 中,我会发起一个 AJAX 调用来启动请求并允许用户在 AJAX 调用返回数据后继续登录。
您可以执行以下操作:
- 对于 cURL 请求,我建议使用 Guzzle(Laravel 没有专门的 Facade 或服务提供商)。
- 要处理 Laravel 的错误,请检查 Errors Documentation。
- 为了处理用户 create/update,Laravel 已经附带了一个用户 Eloquent 模型,您可以使用它来创建和更新帐户,看起来像这样:
// Create the new user
$user = User::create([ /* array with details goes here */ ]);
// Do your other stuff...
// Update the user's other details
$user->detail = "Some detail"; // add as many columns as you like
$user->save();
您可以在 Eloquent Documentation 中找到有关使用模型的任何其他详细信息。