The post has been translated automatically. Original language: English
Routing in PHP plays a key role in the organization of web applications, allowing users to direct requests to the necessary handlers without using third-party packages. This is especially important for lightweight projects where the use of additional libraries may be redundant.
Basic principles
To create your own routing system in PHP, you can use a simple file-based structure. index.php , which will serve as the entry point for all requests. In this file, routes are defined using standard PHP constructs such as switch or if-else. Requests to different URLs are processed by the corresponding files or functions.
An example is a simple router that checks the request URL and connects the corresponding file.:
$request = $_SERVER['REQUEST_URI'];
switch ($request) {
case '/':
require __DIR__ . '/views/home.php';
break;
case '/users':
require __DIR__ . '/views/users.php';
break;
default:
http_response_code(404);
require __DIR__ . '/views/404.php';
}
This code redirects requests to the appropriate handlers, providing simplicity and flexibility.
Server Configuration
For routing to work correctly, you need to configure the server so that all requests are processed via index.php . This is achieved using a file.htaccess on the Apache server or settings in the NGINX configuration file. For example, you can use the following code for Apache:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
This code redirects all requests that do not lead to an existing file or directory to index.php , where routing takes place.
Conclusion
Creating your own routing system in PHP without using third—party packages is an effective solution for lightweight applications that require minimal resources. It allows you to control every aspect of query processing and keep the code base simple and understandable.
Routing in PHP plays a key role in the organization of web applications, allowing users to direct requests to the necessary handlers without using third-party packages. This is especially important for lightweight projects where the use of additional libraries may be redundant.
Basic principles
To create your own routing system in PHP, you can use a simple file-based structure. index.php , which will serve as the entry point for all requests. In this file, routes are defined using standard PHP constructs such as switch or if-else. Requests to different URLs are processed by the corresponding files or functions.
An example is a simple router that checks the request URL and connects the corresponding file.:
$request = $_SERVER['REQUEST_URI'];
switch ($request) {
case '/':
require __DIR__ . '/views/home.php';
break;
case '/users':
require __DIR__ . '/views/users.php';
break;
default:
http_response_code(404);
require __DIR__ . '/views/404.php';
}
This code redirects requests to the appropriate handlers, providing simplicity and flexibility.
Server Configuration
For routing to work correctly, you need to configure the server so that all requests are processed via index.php . This is achieved using a file.htaccess on the Apache server or settings in the NGINX configuration file. For example, you can use the following code for Apache:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
This code redirects all requests that do not lead to an existing file or directory to index.php , where routing takes place.
Conclusion
Creating your own routing system in PHP without using third—party packages is an effective solution for lightweight applications that require minimal resources. It allows you to control every aspect of query processing and keep the code base simple and understandable.