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

AlexG834's avatar

Is it OK to have duplicate routes for Web and API?

I have a resource controller that has a show() method and uses route model binding. The method looks like this:

public function show(Resource $resource)
    {
        if (request()->wantsJson()) {
            return $resource;
        }
        return view('dashboard.resource', compact('resource'));
    }

Is it acceptable to have the route to get this resource in both my routes/web.php and routes/api.php? By keeping it only in my routes\web.php file, it feels wrong when calling it from my API and by keeping it only in my routes\api.php file, it feels wrong appending /api/ to the URL.

0 likes
2 replies
takdw's avatar

If the said API path is gonna be used by my application only, I wouldn't with bother creating additional API routes. But if you're exposing this resource to other services/apps, I think it is better to keep it in the api.php file.

AlexG834's avatar

@TAKDW - The route will be exposed externally. So it's acceptable to have it in both?

Please or to participate in this conversation.