Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mariohbrino's avatar

How can I define a route that doesn't redirect to team missing page

When I define the user CanJoinTeams the middleware VerifyUsersHasTeam redirect the user to a team\missing, because I don't have a team setup. I want to find a way to bypass the team verification for the admin. Is that possible?

Thanks.

0 likes
5 replies
bobbybouwmann's avatar
Level 88

Yeah! So in your middleware you can do something like this

public function handle($request, Closure $next, $guard = null)
{
    if ($this->isAdmin()) {
        return $next($request); // By pass the middleware
    }
    
    if ($this->canJoinTeams()) {
        // Redirect to "team missing"
    }

    return $next($request);
}

Something like this should do!

2 likes
mariohbrino's avatar

@bobbybouwmann thank you, I did as you suggested, but it keeps happening on redirect me to team/missing. So I noticed that the route was with middleware set by default.

The route web have been set by default the middleware hasTeam, with that in mind I can made my own route admin. Laravel Spark changes the default web route and add a middleware hasTeam. Check the RouteServiceProvider file in app/Providers/RouteServiceProvider.php

protected function mapWebRoutes(Router $router)
    {
        $router->middleware(['web', 'hasTeam'])
               ->namespace($this->namespace)
               ->group(base_path('routes/web.php'));
    }
1 like
bobbybouwmann's avatar

Mmh I could have told you that as well ;) Only I didn't know how you were using your middleware. Global or per group or per route. Anyway, glad you solved your problem ;)

1 like
rchase's avatar

I have a custom landing page for welcome.blade.php, and then I configured the Spark logo in the dashboard to go back to welcome.blade.php instead of going to /home.

But I found out if the user does not have a team, he is constantly redirected to /home and unable to get to welcome.blade.php because of the middleware.

Here is what I did to allow him to go back to welcome.blade.php without being redirected:

/spark/src/Http/Middleware/VerifyUserHasTeam.php

namespace Laravel\Spark\Http\Middleware;

use Laravel\Spark\Spark;

class VerifyUserHasTeam
{
    /**
     * Verify the incoming request's user belongs to team.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return \Illuminate\Http\Response
     */
    public function handle($request, $next)
    {
        if ($request->url() === "/" || $request->url()  === "https://captifi.net" || $request->url() === "https://captifi.net/" || $request->url() === "http://captifi.net" || $request->url() === "http://captifi.net/") {
            return $next($request);
        }
        if (Spark::usesTeams() && $request->user() && ! $request->user()->hasTeams()) {
            return redirect(Spark::teamsPrefix().'/missing');
        }

        return $next($request);
    }
}
1 like
steve_laracasts's avatar

Cool, thanks for sharing :)

I can't help it, I really want to put all those $request->url on individual lines and line them up :P

if ($request->url() === "/" || 
    $request->url()  === "https://captifi.net" ||
    $request->url() === "https://captifi.net/" ||
    $request->url() === "http://captifi.net" ||
    $request->url() === "http://captifi.net/")
    {
        return $next($request);
    }

There... that's better! :D

Sorry :-$

Please or to participate in this conversation.