You could maybe do something like this if you don't send the X-Auth-Token for the public pages.
Route::filter('ForPublicPages',function($request,$response){
$payload = Request::header('X-Auth-Token');
//If the X-Auth-Token exists in the header
if($payload){
$this->driver = New AuthTokenDriver();
$this->events = New Dispatcher;
$user = $this->driver->validate($payload);
$this->events->fire('auth.token.valid',$user);
}
});
I'm think I'm missing something. I'm still getting the same issue. The whole IoC/App::/__construct concepts, despite doing all the tutorials I can find, doesn't want to click in my head
The AuthToken package has a facade you can use inside your app, if you add the following to the aliases section of app/config/app.php
'AuthToken' => 'Tappleby\Support\Facades\AuthToken',
Your filter would then look something like:
Route::filter('ForPublicPages',function($request,$response)
{
$payload = Request::header('X-Auth-Token');
//If the X-Auth-Token exists in the header
if($payload){
$user = AuthToken::validate($payload);
if ($user) {
Event::fire('auth.token.valid',$user);
}
}
});
Awesome!
With IoC its all about keeping the initialization of your dependencies outside of your class/code. In this case, the filter doesnt need to know how to make the AuthTokenDriver. Instead it just says "I dont care how you make it, but I need an AuthTokenDriver"
The App (Container) acts as central point to ask these questions, using App::make()
. ServiceProviders are generally used to bind the answers to these questions, you can see the bindings for the AuthTokenDriver here:
Technically its an AuthTokenManager which is used to swap driver types eg. database, redis etc. but thats another topic.
We could then use something like:
$authTokenDriver = App::make('tappleby.auth.token');
$user = $authTokenDriver->validate($authToken);
Facades give us a cleaner way to work with App::make, You can see the getFacadeAccessor
method in https://github.com/tappleby/laravel-auth-token/blob/master/src/Tappleby/Support/Facades/AuthToken.php returns the same string we used with App::make
above.
A call to the AuthToken facade will go through App::make
first.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community