https://github.com/maximkanet/php-router
Router for php application
https://github.com/maximkanet/php-router
php php-router router
Last synced: 10 months ago
JSON representation
Router for php application
- Host: GitHub
- URL: https://github.com/maximkanet/php-router
- Owner: MaximkaNet
- Created: 2023-11-05T10:21:25.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-11-10T19:24:59.000Z (over 2 years ago)
- Last Synced: 2025-05-17T17:39:47.466Z (about 1 year ago)
- Topics: php, php-router, router
- Language: PHP
- Homepage:
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Simple php router
It is an easy-to-use router for your application in `php`.
### Full documentation is available in `docs-` branch
# Getting started
Before we start working with the router, we need to set up
our application. To do this, we need to create a file in the root of the project
`.htaccess` file in the root of the project,
where we will write a few lines of code for the `Apache` server.
```apacheconf
RewriteEngine on
# Ignore files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.css|\.html|\.png|\.gif|\.jpeg)
# Here we redirect all calls to the server
# to the main file of the application.
RewriteRule ^ index.php
```
The next step is to initialize the router.
To do this, write the following code.
```php
use router\Router; // Connecting the router namespace
```
Next, create a new router using `new Router()`
```php
$router = new Router();
```
The router has such methods as `get`, `post`, `put`, `patch`, `delete`.
You can use them as follows:
```php
$router->get('/', function () {
echo 'Hello world';
});
```
The next and last step is to start the router using the `resolve` method.
```php
$url = Router::getPath();
$method = Router::getMethod();
$router->resolve($url, $method);
```
In the code above, we learned the `$method` and `$url` from the request.
It is not necessary to use the static methods `getPath()` and `getMethod()`,
but we recommend that you write these methods in your code to make it easier to
understanding of the algorithm.
Thus, we have created an application that shows `Hello world` using the `Router` class.