简体   繁体   English

Laravel 5.1中的多步注册

[英]Multi Step Registration in Laravel 5.1

I'm trying to build multi-step registration form. 我正在尝试构建多步注册表单。

I have a route /register 我有路线/register

Step 1 I'm posting this form to step2 第1步我将此表单发布到第2步

route('register', ['step' => 1])

在此输入图像描述

Step 2 第2步

And i'm getting data of step1 and put inside hidden inputs. 我正在获取step1的数据并将其置于隐藏的输入中。 And posting to third step for ending registration. 并发布到结束注册的第三步。 If it is successful no problem. 如果成功没问题。 But what happened if registration fails ? 但是如果注册失败会发生什么?

route('register', ['step' => 2])

Step 3 第3步

route('register', ['step' => 3])

If Registration fails i'm redirecting user to step2. 如果注册失败,我将用户重定向到第2步。

Here is my redirect code. 这是我的重定向代码。

    $new_user = $request->all();
    $validator = Validator::make($new_user, $this->rules());

    if ($validator->fails())
    {
        return redirect()->back()
            ->withInput($new_user)
            ->withErrors($validator->getMessageBag()->toArray());
    }
    else
    {
        //
    }

If validation fails i'm redirecting to step2 everything ok. 如果验证失败,我将重定向到第二步,一切正常。 But i'm seeing forms in picture (Step 1) But my uri is /register?step=2 但我看到图片中的表格(步骤1)但我的uri是/ register?step = 2

What is the problem. 问题是什么。 Where am i making mistake ? 我哪里弄错了?

UPDATE: (Route Definitions) 更新:(路线定义)

Route::get('/register', [
    'uses'       => 'Auth\AuthController@getRegister',
    'as'         => 'register',
    'middleware' => ['guest'],
]);

Route::post('/register', [
    'uses'       => 'Auth\AuthController@postRegister',
    'middleware' => ['guest'],
]);

UPDATE 2: (getRegister and postRegister) 更新2:(getRegister和postRegister)

Note: I didn't finished coding getRegister and postRegister. 注意:我没有完成getRegister和postRegister的编码。

getRegister getRegister

public function getRegister(Request $request)
    {
        if(!$request->has('step'))
        {
            /**
             * Eğer kayıt ekranında ?step=1,2 vs. yoksa direk ?step=1 e yönlendirme yapıyoruz.
             */
            return redirect()->route('register', ['step' => 1]);
        }

        $countries = (new LocationCountry)->getAllCountries()->toArray();
        foreach($countries as $key => $country)
        {
            $countryNames[$key] = $countries[$key]['translation'] = trans('country.'.$country['code']);
        }

        array_multisort($countryNames, SORT_STRING, $countries);

        /**
         * Ülke ve Zaman Dilimi için Varsayılan Seçimi
         */

        $default = new \stdClass();

        $default->country = (Lang::locale() == 'tr') ? 'TR' : 'US';

        $default->timezone = (Lang::locale() == 'tr') ? 'Europe/Istanbul' : 'America/New_York';

        $timezones = (new DateController)->getTimeZoneList();

        return view('auth.register.index', compact(['timezones', 'countries', 'default']))
            ->with('orderProcess', TRUE);
    }

postRegister 的postRegister

public function postRegister(Request $request){
        if(!$request->has('step'))
        {
            /**
             * Eğer kayıt ekranında ?step=1,2 vs. yoksa direk ?step=1 e yönlendirme yapıyoruz.
             */
            return redirect()->route('register', ['step' => 1]);
        }

        if ($request->get('step') == 2)
        {
            $new_user = $request->all();

            $new_user['tc_citizen'] = (!isset($new_user['tc_citizen'])) ? 0 : 1;
            $new_user['area_code']  = (new LocationCountry)->getCountryAreaCodeByCode($new_user['country']);

            $cities = (new Location)->getCities();

            /**
             * Eğer Post Durumunda ise ve town değişkeni varsa...
             */
            if($request->has('town'))
            {
                $towns = (new Location)->getTowns($request->get('city'));

                if(!$towns->isEmpty())
                {

                }
            }

            return view('auth.register.step2', compact(['new_user', 'cities']))
                ->with('orderProcess', TRUE);
        }

        if($request->get('step') == 3)
        {
            /**
             * Kayıt Sonuç Sayfası
             */
            $new_user = $request->all();
            $validator = Validator::make($new_user, $this->rules());

            if ($validator->fails())
            {
                return redirect()->back()
                    ->withInput($new_user)
                    ->withErrors($validator->getMessageBag()->toArray());
            }
            else
            {

            }
        }
    }

It's because your redirection is telling the browser to do a GET request on the register?step=2 URL. 这是因为你的重定向告​​诉浏览器在register?step=2上执行GET请求register?step=2 URL。 And in your getRegister method you don't check for the step value (hence you see the same form as for GET step=1 ). 并且在您的getRegister方法中,您不检查step值(因此您看到与GET step=1相同的形式)。


I see two possible solutions: 我看到两种可能的解决方案

  • either you tweak your redirection so that it does a POST request to step=2 (might be tricky) 要么你调整你的重定向,以便它执行step=2的POST请求(可能是棘手的)
  • or you serve a different page for GET request to step=2 或者您为step=2 GET请求提供不同的页面

I would advise you for the second option: 我会建议你第二个选择:

  • The form in step=1 should do a POST to step=1 , which should redirect to a GET step=2 if everything is fine (using flash cookie to pass the variables) 形式step=1应该做一个POST到step=1 ,这应重定向到一个GET step=2 ,如果一切都很好(使用闪光灯的cookie传递变量)
  • The form in step=2 should do a POST to step=2 , which should redirect to a GET step=3 if everything is fine (using flash cookie to pass the variables) 形式step=2应该做一个POST到step=2 ,这应重定向到一个GET step=3 ,如果一切都很好(使用闪光灯的cookie传递变量)

Maybe you can try this 也许你可以试试这个

return redirect()->back()
     ->withInput(array_merge($new_user, ['step', $step-1]))
     ->withErrors($validator->getMessageBag()->toArray());

I hope it works fine for you. 我希望它适合你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM