Martin
Martin

Reputation: 557

Prevent user from accessing some routes in Laravel

Working on a Laravel application and I have some routes, the routes are of a multi step form. I need to prevent the user from accessing the last route (which directs to last page of the form) before accessing or filling in the previous routes.

Routes

Route::get( '/first', 'TrController@payQuote')->name('b2c.payquote');
Route::get( '/second', 'TrController@emailQuote')->name('b2c.sendquote');
Route::get( '/receipt', 'TrController@getReceipt')->name('b2c.receipt');
Route::get( '/success', 'TrController@getSuccess')->name('b2c.success');

Upvotes: 1

Views: 2182

Answers (2)

Bruno Rodrigues
Bruno Rodrigues

Reputation: 314

You could create a middleware class and then use the middleware directly in your routes file.

Example:

<?php

namespace App\Http\Middleware;

use Closure;

class CheckPermission
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        // TODO: Add your logic here.

        if ($request->age <= 200) {
            return redirect('home');
        }

        return $next($request);
    }
}

Then in your routes file:

Route::get('admin/profile', function () {
    //
})->middleware(CheckPermission::class);

Upvotes: 4

guttume
guttume

Reputation: 278

There are multiple ways to do it. One way could be that you can check http-referrer on the last page and if it is the route previous to the last one then you allow it otherwise redirect it to the previous page. This can be implemented for every page.

Other way could be database driven. For every page visit you can have an entry in the database and check on the next page if there is an entry otherwise redirect him to wherever you want.

Upvotes: 0

Related Questions