Support the ongoing development of Laravel.io →
Requests IOC Architecture
Last updated 2 years ago.
0

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);
    }
});
Last updated 2 years ago.
0

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

phpstorm

Last updated 2 years ago.
0

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);
		}
	}
});
Last updated 2 years ago.
0

Wooooo! This fixed it.

Last updated 2 years ago.
0

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:

https://github.com/tappleby/laravel-auth-token/blob/master/src/Tappleby/AuthToken/AuthTokenServiceProvider.php#L31-L33

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.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.