Laravel 4 - 一个表单中有两个提交按钮,并且两个提交都由不同的操作处理 [英] Laravel 4 - two submit buttons in one form and have both submits to be handled by different actions

查看:19
本文介绍了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.
    }

}
?>

这篇关于Laravel 4 - 一个表单中有两个提交按钮,并且两个提交都由不同的操作处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆