Search code examples
phplaravellaravel-middleware

Creating a group middleware for all the admission steps in LARAVEL


I am currently building an admission website for my project in LARAVEL. My professor suggested that I should use middleware for my steps. I have steps 1-12 in my website. Ok example, I am the user I haven't finished step 1 yet, the routes for steps 2-12 are disabled. Now, I already finished up to the step 3 of my admission but I want to go back to step 1 to edit my name so I went back. Step 3 is still enabled since it it the current admission step im doing. How do i do that in Laravel Middleware?

Sample Routes

//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');

Solution

  • This is just pseudocode, but it would be something like this.

    CustomAuthController

    
    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");
    }
    

    Also, this doesn't have to be done with session(). You could just as easily update a column in a database with the step number.