Laravel 4 - 一个表单中有两个提交按钮,并且两个提交都由不同的操作处理

2022-01-08 00:00:00 php laravel laravel-4

我有一个包含电子邮件和密码字段的登录表单.我有两个提交按钮,一个用于登录(如果用户已经注册),另一个用于注册(用于新用户).由于登录操作和注册操作不同,因此我需要某种方法将带有所有发布数据的请求重定向到其各自的操作.有没有办法在 Laravel 4 中以纯 Laravel 的方式实现这一点?

I have a login form which has email and password fields. And I have two submit buttons, one for login ( if user is already registered ) and the other one for registration ( for new users ). As the login action and register action are different so I need some way to redirect the request with all the post data to its respective action. Is there a way to achieve this in Laravel 4 in pure Laravel way?

推荐答案

我会怎么做

如果您的表单是(2 个按钮):

If your form is (2 buttons):

{{ 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() }}

创建一个控制器'TestController'

Create a controller 'TestController'

添加路线

Route::post('test/auth', array('uses' => 'TestController@postAuth'));

在 TestController 中,您将有一种方法来检查单击了哪个提交,以及另外两种用于登录和注册的方法

In TestController you'd have one method that checks which submit was clicked on and two other methods for login and register

<?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.
    }

}
?>

相关文章