I am working on my first project with laravel and spark and I am making good progress, but I am not sure how to combine two middleware in an either/or fashion.
In my App/Controllers/HomeController file I have this middleware:
public function __construct()
{
$this->middleware('auth');
//$this->middleware('dev');
$this->middleware('subscribed');
}
Now, I want everyone who is logged in to access this ('auth') and who is also a paying subscriber ('subscribed'), and this works as expected. But this means that as a developer ('dev') I cannot access the page (as I don't have a paying subscription).
What I am looking to achieve is if either dev or subscribed then allow access. Any ideas how to achieve this?
I had a similar problem, where I needed routes to be accessed by either subscribed or teamSubscribed. I ended up making a new middleware that checked to see if a user was in either of the two groups. So far it's working well.
@kevinm40 You can create middleware that checks for the two conditions:
class VerifyTeamIsSubscribed
{
public function handle($request, Closure $next)
{
if ($this->userCanViewTeam($request)) {
return $next($request);
}
if ($request->expectsJson()) {
return response()->json([
'message' => 'Subscription required.',
], 402);
} else {
return redirect('/settings#/subscription');
}
}
protected function userCanViewTeam(Request $request)
{
$team = $request->route()->parameter('team');
if ($team->subscribed()) {
return true;
}
if ($request->user() && $request->user()->ownsTeam($team)) {
return true;
}
return false;
}
}
I’ve done this for a multi-tenant CMS. I also add a banner in the header if the user’s viewing a site without a subscription:
@unless($team->subscribed())
<div class="alert alert-warning" role="alert">
Your website is currently <strong>unavailable</strong>.
Subscribe to activate your website.
<a class="alert-link" href="{{ url('/settings#/subscription'') }}">Subscribe</a>
</div>
@endunless