簡體   English   中英

在laravel中發布兩種不同形式的數據?

[英]Posting data from two different forms in laravel?

我有一個登錄頁面並想要在同一頁面上設置表單。

登錄表單工作得很好,已經將偽數據輸入到我正在使用的數據庫中。

我遇到的問題是我收到了一個調用錯誤的方法(大概是因為我對兩個不同的函數進行了相同的post函數調用。

當前在我的routes.php文件中

    // route to process the form
Route::post('/', array('uses' => 'HomeController@doLogin'));

Route::post('/', array('uses' => 'HomeController@doRegister'));

我的控制器文件看起來像這樣(很抱歉,它有點長,我認為最好提供所有內容,而不是假設有人僅憑我的解釋就能理解我的問題)

public function doRegister() {
    $v = User::validate(Input::all());

    if ( $v->passes() ) {
            User::create(array(
                    'name'=>      Input::get('name'),
                    'email'=>     Input::get('email'),
                    'password'=>  Hash::make(Input::get('password')),
            ));

            return 'Thanks for registering!';
    } else {
            return Redirect::to('/')->withErrors($v->getMessages());
    }
}

public function doLogin()
{
    // validate the info, create rules for the inputs
    $rules = array(
        'email'    => 'required|email', // make sure the email is an actual email
        'password' => 'required|alphaNum|min:3' // password can only be alphanumeric and has to be greater than 3 characters
    );

    // run the validation rules on the inputs from the form
    $validator = Validator::make(Input::all(), $rules);

    // if the validator fails, redirect back to the form
    if ($validator->fails()) {
        return Redirect::to('/')
            ->withErrors($validator) // send back all errors to the login form
            ->withInput(Input::except('password')); // send back the input (not the password) so that we can repopulate the form
    } else {

        // create our user data for the authentication
        $userdata = array(
            'email'     => Input::get('email'),
            'password'  => Input::get('password')
        );

        // attempt to do the login
        if (Auth::attempt($userdata)) {

            // validation successful!
            // redirect them to the secure section or whatever
            // return Redirect::to('secure');
            // for now we'll just echo success (even though echoing in a controller is bad)
            echo 'SUCCESS!';

        } else {        

            // validation not successful, send back to form 
            return Redirect::to('/');

        }

    }
}

據我所知,這是因為我沒有為注冊表格設置要正確使用的功能。

希望我在解釋我的問題上做得很好,請問有什么解決方案嗎? (對laravel來說不是什么新事物)

一種形式將張貼登錄,另一種形式進行注冊

Route::post('login', array('uses' => 'HomeController@doLogin'));
Route::post('register', array('uses' => 'HomeController@doRegister'));

然后,您將打開如下表格:

{{ Form::open(array('url' => 'login')) }}

{{ Form::open(array('url' => 'register')) }}

編輯:例如,表單將僅放置在您的主視圖中,然后您只需從登錄和注冊方法重定向,而不顯示視圖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM