PHP Laravel 在重定向中将参数作为发布数据传递
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20795380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Laravel pass parameters as post data in redirect
提问by madziikoy
After the registration was successful I wanted to pass all the data to a login route as POST. How do you do this in laravel 4?
注册成功后,我想将所有数据作为 POST 传递到登录路由。你如何在 Laravel 4 中做到这一点?
I know I can just authenticate the user after registration but the login page has other parameters and more required authentication process.
我知道我可以在注册后对用户进行身份验证,但登录页面有其他参数和更多需要的身份验证过程。
So I wish to push the username and password entered in the registration process into the login process so it can go through with the other processes inside the login route. (ex. Token generation which requires post data of app_id and app_secret)
所以我想把在注册过程中输入的用户名和密码推送到登录过程中,以便它可以通过登录路径中的其他过程。(例如需要 app_id 和 app_secret 的 post 数据的令牌生成)
回答by Antonio Carlos Ribeiro
You can use Laravel Events do to that:
您可以使用 Laravel 事件来做到这一点:
Register your event in filters.php
or create an events.php
and add it to composer.json
:
注册您的活动filters.php
或创建一个events.php
并将其添加到composer.json
:
Event::listen('user.registered', function($data)
{
var_dump($data['app_id']);
var_dump($data['app_secret']);
var_dump($data['user']);
});
And fire it in your register()
method:
并在您的register()
方法中启动它:
public function register()
{
/// register your user:
$user = new User;
...
$user->save();
/// fire the event:
Event::fire('user.registered', array(
'app_id' => 'API',
'app_secret' => 'WHATEVER',
'user' => $user
));
/// Redirect the user to where it should go next:
return Redirect::to('/');
}
回答by searsaw
Why not move alot of that extra logic into the User model. Then you can call $user->crazySecureLogin();
after logging in and after registering. That way you stay DRY, and it could possibly clean up your login controller method too. And to top it off, you don't have to worry about sending a post request as a response.
为什么不将很多额外的逻辑移到用户模型中。然后你可以$user->crazySecureLogin();
在登录后和注册后调用。这样你就可以保持 DRY,它也可能清理你的登录控制器方法。最重要的是,您不必担心发送 post 请求作为响应。
回答by elliotanderson
return Redirect::to('routename')->withInput();
or
或者
return Redirect::to('routename')->with('email', $emailvalue)->with('password', $passwordValue)
etc
等等
回答by user3217526
add namespace on top of your class
use Redirect;
add this code to your redirect stage.
return Redirect::route('route_name',['username' => $request->username, 'password' => $request->password]);
在类的顶部添加命名空间
use Redirect;
将此代码添加到您的重定向阶段。
return Redirect::route('route_name',['username' => $request->username, 'password' => $request->password]);