Skip to content

Instantly share code, notes, and snippets.

@sudomabider
Last active October 14, 2016 00:44
Show Gist options
  • Save sudomabider/6d720dd2b217ce1b366a14aa61683f27 to your computer and use it in GitHub Desktop.
Save sudomabider/6d720dd2b217ce1b366a14aa61683f27 to your computer and use it in GitHub Desktop.
set guard paramter on route without auth
<?php
namespace App\Providers;
use App\Modules\Payment\Entities\PaymentMethod;
use App\Modules\Shipping\Entities\ShippingOption;
use App\Modules\Variant\Entities\VariantOption;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Routing\Router;
class RouteServiceProvider extends ServiceProvider
{
/**
* This namespace is applied to your controller routes.
*
* In addition, it is set as the URL generator's root namespace.
*
* @var string
*/
protected $namespace = 'App\Http\Controllers';
/**
* Define your route model bindings, pattern filters, etc.
*
* @internal param Router $router
*/
public function boot()
{
parent::boot();
$this->registerRouteGuardParameter();
}
/**
* Define the routes for the application.
*
* @param \Illuminate\Routing\Router $router
* @return void
*/
public function map(Router $router)
{
$this->mapWebRoutes($router);
//
}
/**
* Define the "web" routes for the application.
*
* These routes all receive session state, CSRF protection, etc.
*
* @param \Illuminate\Routing\Router $router
* @return void
*/
protected function mapWebRoutes(Router $router)
{
$router->group([
'namespace' => $this->namespace, 'middleware' => 'web',
], function () {
require app_path('Http/routes.php');
});
}
private function registerRouteGuardParameter()
{
$this->app['router']->matched(function (\Illuminate\Routing\Events\RouteMatched $e) {
$route = $e->route;
if (!array_has($route->getAction(), 'guard')) {
return;
}
$routeGuard = array_get($route->getAction(), 'guard');
$this->app['auth']->resolveUsersUsing(function ($guard = null) use ($routeGuard) {
return $this->app['auth']->guard($routeGuard)->user();
});
$this->app['auth']->setDefaultDriver($routeGuard);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment