Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Routing Basics | Project Setup
Quizzes & Challenges
Quizzes
Challenges
/
PHP MVC Development

bookRouting Basics

Note
Definition

Routing in web applications is the process of determining how incoming URLs are mapped to specific code that handles the request, usually by directing them to the appropriate controller and action.

When a user visits a web page, the browser sends a request to your server. In an MVC application, routing is what decides which controller and action should handle that request. Instead of having a separate PHP file for every page, you use a single entry point (like index.php) and a router to interpret the URL. The router directs requests to the correct controller and action, keeping your application organized and easy to expand.

index.php

index.php

controllers/HomeController.php

controllers/HomeController.php

controllers/AboutController.php

controllers/AboutController.php

copy
1234567891011121314151617
<?php // A very simple router example // Get the requested URL path $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); // Remove leading slash $path = ltrim($uri, '/'); // Map URLs to controller files if ($path === 'home') { require 'controllers/HomeController.php'; } elseif ($path === 'about') { require 'controllers/AboutController.php'; } else { echo "404 Not Found"; }

In this example, the router checks the URL path and includes the correct controller file. If the user visits /home, the router includes HomeController.php; if the user visits /about, it includes AboutController.php. If the route does not match any known path, it displays a simple 404 message. This approach keeps your logic separated and makes it easier to manage as your application grows.

The router works by parsing the incoming URL, extracting the path, and then using conditional logic to determine which controller to include. This approach means you do not need a separate PHP file for every possible page, and you can centralize your application logic.

index.php

index.php

controllers/UserController.php

controllers/UserController.php

controllers/EditPostController.php

controllers/EditPostController.php

copy
1234567891011121314151617181920212223242526
<?php // Improved router with dynamic parameters $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); $path = ltrim($uri, '/'); // Match routes with dynamic parameters, e.g., /user/42 $routes = [ '#^user/(\d+)$#' => 'UserController.php', '#^post/(\d+)/edit$#' => 'EditPostController.php', ]; $matched = false; foreach ($routes as $pattern => $controller) { if (preg_match($pattern, $path, $matches)) { // Pass dynamic parameters to the controller $_GET['param'] = $matches[1] ?? null; require "controllers/$controller"; $matched = true; break; } } if (!$matched) { echo "404 Not Found"; }

Routing gives your MVC application flexibility because you can easily add, change, or remove routes without rearranging your file structure. You can define how URLs look, make them user-friendly, and pass necessary data to controllers in a consistent way. This separation of concerns makes your application easier to maintain and extend.

question mark

What is the main purpose of a router in MVC?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 2

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

bookRouting Basics

Deslize para mostrar o menu

Note
Definition

Routing in web applications is the process of determining how incoming URLs are mapped to specific code that handles the request, usually by directing them to the appropriate controller and action.

When a user visits a web page, the browser sends a request to your server. In an MVC application, routing is what decides which controller and action should handle that request. Instead of having a separate PHP file for every page, you use a single entry point (like index.php) and a router to interpret the URL. The router directs requests to the correct controller and action, keeping your application organized and easy to expand.

index.php

index.php

controllers/HomeController.php

controllers/HomeController.php

controllers/AboutController.php

controllers/AboutController.php

copy
1234567891011121314151617
<?php // A very simple router example // Get the requested URL path $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); // Remove leading slash $path = ltrim($uri, '/'); // Map URLs to controller files if ($path === 'home') { require 'controllers/HomeController.php'; } elseif ($path === 'about') { require 'controllers/AboutController.php'; } else { echo "404 Not Found"; }

In this example, the router checks the URL path and includes the correct controller file. If the user visits /home, the router includes HomeController.php; if the user visits /about, it includes AboutController.php. If the route does not match any known path, it displays a simple 404 message. This approach keeps your logic separated and makes it easier to manage as your application grows.

The router works by parsing the incoming URL, extracting the path, and then using conditional logic to determine which controller to include. This approach means you do not need a separate PHP file for every possible page, and you can centralize your application logic.

index.php

index.php

controllers/UserController.php

controllers/UserController.php

controllers/EditPostController.php

controllers/EditPostController.php

copy
1234567891011121314151617181920212223242526
<?php // Improved router with dynamic parameters $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); $path = ltrim($uri, '/'); // Match routes with dynamic parameters, e.g., /user/42 $routes = [ '#^user/(\d+)$#' => 'UserController.php', '#^post/(\d+)/edit$#' => 'EditPostController.php', ]; $matched = false; foreach ($routes as $pattern => $controller) { if (preg_match($pattern, $path, $matches)) { // Pass dynamic parameters to the controller $_GET['param'] = $matches[1] ?? null; require "controllers/$controller"; $matched = true; break; } } if (!$matched) { echo "404 Not Found"; }

Routing gives your MVC application flexibility because you can easily add, change, or remove routes without rearranging your file structure. You can define how URLs look, make them user-friendly, and pass necessary data to controllers in a consistent way. This separation of concerns makes your application easier to maintain and extend.

question mark

What is the main purpose of a router in MVC?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 2
some-alt