php 为LARAVEL中的所有准入步骤创建组中间件

wvt8vs2t  于 2023-03-16  发布在  PHP
关注(0)|答案(1)|浏览(94)

我目前正在为我在LARAVEL的项目建立一个录取网站。我的教授建议我应该使用中间件为我的步骤。我有步骤1-12在我的网站。好的例子,我是用户我还没有完成步骤1,步骤2-12的路线被禁用。现在,我已经完成了我的入学步骤3,但我想回到步骤1编辑我的名字,所以我回去了。步骤3仍然启用,因为它是当前的入学步骤我正在做。我如何在Laravel中间件中做到这一点?

示例路由

//Admission Steps
// STEP 1
Route::get('/step1/{id}',[CustomAuthController::class, 'step1'])->middleware('isLoggedIn');
Route::post('/step1-register',[CustomAuthController::class, 'step1Register'])->name('step1-register');
// STEP 2
Route::get('/step2/{id}',[CustomAuthController::class, 'step2'])->middleware('isLoggedIn');
Route::post('/step2-register',[CustomAuthController::class, 'step2Register'])->name('step2-register');
// STEP 3
Route::get('/step3/{id}',[CustomAuthController::class, 'step3'])->middleware('isLoggedIn');
Route::post('/step3-register',[CustomAuthController::class, 'step3Register'])->name('step3-register');
o8x7eapl

o8x7eapl1#

这只是伪代码,它应该是这样的。

自定义验证控制器

public function step2()
{
    // Check to make sure the user is on the correct step
    $step = session()->get('currentStep');

    if ($step != "2" ) {
        // redirect to step 1 with an error message
    }

    // Return the form for step 2
    return view("step2");
}

public function step2Register()
{
    // Check to make sure the user is on the correct step
    $step = session()->get('currentStep');
    
    if ($step != "2" ) {
        // redirect to step 1 with an error message
    }

    // Do your normal processing here. Validation, etc. 
    // Store whatever you want from this step. 

    // Everything was processed successfully so update the current step
    session()->put('currentStep', 3);

    // Move them to step 3 now
    return view("step3");
}

另外,这并不一定要用session()来完成,您可以用步骤号轻松地更新数据库中的列。

相关问题