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?

推荐答案

The way I would do it

如果您的表单是(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() }}

Create a controller 'TestController'

添加路由

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

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相关问答推荐

密码确认在Livewire中不匹配

当两个外键都在同一个表上时创建直通关系

如何更新 Laravel 4 中现有的 Eloquent 关系?

如何在 vue 模板中引用 laravel csrf 字段

Laravel 4:如何将 WHERE 条件应用于 Eloquent 类的所有查询?

如何对有序的has many关系进行分页?

Carbon现在时间错了

如何使用 Eloquent Laravel 更新集合

Laravel - 验证 - 如果字段为空则需要

Laravel 5.5 类型错误:传递给 Illuminate\Auth\EloquentUserProvider::validateCredentials() 的参数 1 必须是实例

在 Laravel 中下载后如何重定向?

不支持驱动Driver[] - Laravel 5.3

Laravel 将参数从路由传递到过滤器

如何更改 Handlebars.js 的默认分隔符?

从 Artisan 电话中获得响应

防止 Eloquent 查询上的模型水合

Eloquent的 attach/detach/sync 触发任何事件?

Laravel binding绑定的用途和目的是什么?

使用迁移更改表 Laravel 5

限制自己重载外部 API 的速率