← Back to Course

Defining Routes in web.php and api.php

Routing is how Laravel decides what should happen when a specific URL is requested. Every route connects a URL pattern to a piece of logic, typically a closure or a controller method, and Laravel keeps these definitions organized across a few dedicated files.

web.php vs api.php

Laravel separates routes based on how they'll be consumed. Routes in web.php are meant for the browser and automatically get session handling, CSRF protection, and cookie encryption. Routes in api.php are designed for stateless requests, such as those from a mobile app or JavaScript frontend, and skip session-based features by default.

FileMiddleware / Use
routes/web.phpSession, CSRF protection, cookie encryption — for browser pages
routes/api.phpStateless, token-based auth — for APIs and SPAs

Basic Route Syntax

A route definition pairs an HTTP verb with a URL pattern and the code that should run. Laravel supports all standard HTTP verbs, so routes can be tailored precisely to how a resource should be accessed.

Basic routes only get you so far — real applications usually need routes that accept dynamic values and can be referenced by name, which is exactly what we explore next.

Ready to master Laravel Training Course?

Join Uncodemy's hands-on Laravel program and build real, production-ready applications.

Explore Course