Laravel BasicsCore· 40 min read

Routing

Map URLs to code — the entry point of every Laravel app.

What you will learn

  • Define routes in web.php
  • Use route parameters
  • Point routes to controllers

Routes map URLs to actions

Routes live in routes/web.php. Each maps a URL + HTTP method to a function or controller method.

Routes: closures, views and parameters
// routes/web.php
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return 'Welcome to CodingClave!';
});

Route::get('/about', function () {
    return view('about');     // show a Blade view
});

// Route parameter
Route::get('/users/{id}', function ($id) {
    return "User number $id";
});

Read each route as "when this URL is visited, run this code". The first route — Route::get('/', ...) — matches the home page and returns a plain text greeting. The second matches /about and returns a Blade view instead of text. The third is the interesting one: the {id} in the URL is a route parameter — a placeholder. Whatever the visitor types there (like /users/7) is captured and handed to the function as $id, so the page can say "User number 7".

Note: Output: Visiting / shows: Welcome to CodingClave! Visiting /users/7 shows: User number 7 The {id} part changes with the URL — that is how one route can serve many users.

Point to a controller

Writing all your logic inside the route closure gets messy fast. Instead, you usually point a route at a controller method — a named function in a separate file. The route just says "send this request to that method".

Route to a controller method
use App\Http\Controllers\ProductController;

Route::get('/products', [ProductController::class, 'index']);
Route::post('/products', [ProductController::class, 'store']);

The [ProductController::class, 'index'] part means "call the index method on ProductController". So a GET request to /products runs index() (show the list), and a POST request to the same /products runs store() (save a new product). Same URL, different HTTP method, different method — this is the CRUD-to-HTTP mapping (CRUD = Create, Read, Update, Delete, the four basic database actions; each maps to an HTTP method like GET or POST). You will see CRUD in full detail in a later lesson.

  1. The browser requests a URL with a specific method, e.g. GET /products.
  2. Laravel reads routes/web.php top to bottom and finds the first route whose URL and method match.
  3. That route names a controller and method, e.g. [ProductController::class, 'index'].
  4. Laravel runs that method, which does the work and returns text or a view.
  5. Laravel sends the result back to the browser as the response.

Tip: Laravel routes mirror what you learned about HTTP: Route::get, Route::post, Route::put, Route::delete — the same CRUD-to-HTTP mapping from the REST lesson.

Q. Where do Laravel web routes live?

Answer: Web routes are defined in routes/web.php, mapping URLs to closures or controller methods.

✍️ Practice

  1. Add routes for /, /about and /contact returning text or views.
  2. Add a route with a {id} parameter.

🏠 Homework

  1. Create routes for a “posts” resource (list, show one) pointing to a controller.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →