我有一个包含电子邮件和密码字段的登录表单。我有两个提交按钮,一个用于登录(如果用户已经注册),另一个用于注册(用于新用户)。由于登录操作和注册操作不同,因此我需要某种方法将带有所有发布数据的请求重定向到其各自的操作。有没有办法在 Laravel 4 中以纯 Laravel 的方式实现这一点?
最佳答案
我的做法
如果您的表单是(2 个按钮):
{{ Form::open(array('url' => 'test/auth')) }}
{{ Form::email('email') }}
{{ Form::password('password') }}
{{ Form::password('confirm_password') }}
<input type="submit" name="login" value="Login">
<input type="submit" name="register" value="Register">
{{ Form::close() }}
创建一个 Controller 'TestController'
添加路线
Route::post('test/auth', array('uses' => 'TestController@postAuth'));
在 TestController 中,您有一种方法可以检查点击了哪个提交,还有两种方法用于登录和注册
<?php
class TestController extends BaseController {
public function postAuth()
{
//check which submit was clicked on
if(Input::get('login')) {
$this->postLogin(); //if login then use this method
} elseif(Input::get('register')) {
$this->postRegister(); //if register then use this method
}
}
public function postLogin()
{
echo "We're logging in";
//process your input here Input:get('email') etc.
}
public function postRegister()
{
echo "We're registering";
//process your input here Input:get('email') etc.
}
}
?>
关于php - Laravel 4 - 一个表单中的两个提交按钮,并且两个提交都由不同的操作处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20932543/