Laravel 5: Call method in class before the method indicated by the route

4.6k views Asked by At

I'm sure there's a well documented way to do this in Laravel, I'm just missing it or not understanding what I'm reading. I have an application that uses a token for accessing part of the website, rather than a username or password. That token identifies the job, checks the status of the job and then redirects the user to the appropriate page.

This is what I have in my routes.php file so far:

$router->pattern('token', '[A-Za-z0-9]{32}');
$router->pattern('id', '[0-9]{1,11}');

Route::group(['prefix' => '{token}', 'middleware' => 'token'], function()
{
    Route::get('/', 'RedirectController@index');
    Route::get('/first-steps', ['as' => 'firstSteps', 'uses' => 'FirstStepsController@index']);
    Route::get('/first-steps/form', ['as' => 'firstStepsForm', 'uses' => 'FirstStepsController@form']);
    Route::post('/first-steps/form', 'FirstStepsController@processForm');
    Route::get('/designs', ['as' => 'designs', 'uses' => 'DesignsController@index']);
    Route::get('/designs/{id}', ['as' => 'designShow', 'users' => 'DesignsController@show']);
});

There are a few other routes that are irrelevant. This all works great, if the user visits the link we give them then RedirectController@index checks the stage of the job and redirects them to the correct URL (currently /first-steps for stage 1 and /designs for stage 2). The problem is if the user bookmarks a page, such as /first-steps, when the project moves on to stage 2, the user can still access the stage 1 pages. I want to be able to have a method that runs before the one indicated by the route that makes sure that they are visiting the correct URL for the stage the job is in, redirect if necessary and then store the job (an eloquent object) in the class (or inject it to the method called).

I've tried creating a before method in the controller but it doesn't get called:

public function before(Request $request, $token)
{
    $this->website = Website::where('access_token', '=', $token)->firstOrFail();
}

As you can see in the router I have a custom middleware that validates the token. This is the code for that middleware:

public function handle($request, Closure $next)
{
    if ( ! Website::where('access_token', '=', $request->route()->parameter('token'))->count())
    {
        return response('Invalid token.', 401);
    }
    return $next($request);
}

I have a feeling that I can use this middleware to achieve what I need to do, but I'm not sure how to proceed or even if I'm on the right track. Can someone please explain what I need to use to achieve my requirements?

1

There are 1 answers

5
Scott Anderson On BEST ANSWER

Middleware might be an option.

Alternatively could you do this in the constructor of your BaseController?

E.g.

class BaseController extends Controller {

    public function __construct()
    {
         //if token in query string
         //load Job and check status Vs requested URL
         //redirect to correct URL
    }
}