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.
| File | Middleware / Use |
|---|---|
| routes/web.php | Session, CSRF protection, cookie encryption — for browser pages |
| routes/api.php | Stateless, 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.
Route::get()— retrieves a resource or renders a pageRoute::post()— creates a new resourceRoute::put()/Route::patch()— updates an existing resourceRoute::delete()— removes a resource
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.
