https://github.com/co0lc0der/router-component
Router php component
https://github.com/co0lc0der/router-component
php php-oop router routing
Last synced: 3 months ago
JSON representation
Router php component
- Host: GitHub
- URL: https://github.com/co0lc0der/router-component
- Owner: co0lc0der
- License: mit
- Created: 2021-04-13T12:37:49.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-05-02T18:40:37.000Z (about 3 years ago)
- Last Synced: 2025-01-13T13:49:03.065Z (4 months ago)
- Topics: php, php-oop, router, routing
- Language: PHP
- Homepage:
- Size: 3.91 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Router php component
This is easy-to-use php component for routing URLs in your project. See `index.php` for examples.
### Public methods:
- `run()` - runs routing
## How to use
### 1. Include config file and set `tplpath` parameter. Add routes into the section below.
- `index.php`
```php
$config = require __DIR__ . '/Router/config.php';
```
- `config.php`
```php
'tplpath' => __DIR__ . '/../templates/',
'routes' => [
'' => 'index',
'/login' => 'login',
'/profile' => 'profile',
]
```
### 2. Create files according the routes you added.
A file must be called `2nd_part_of_route.view.php`. For example we have a route `'/about' => 'about_us'` therefore the file must be `about_us.view.php` and be placed into `templates` folder (`tplpath` parameter). If a file cannot be found Router shows `404.view.php` from `templates` folder.
```
templates/
404.view.php
index.view.php
login.view.php
```
### 3. Include Router class and init it with `routes` array and `tplpath` from config.
```php
require __DIR__ . '/Router/Router.php';$router = new Router($config['routes'], $config['tplpath']);
```
### 4. Now just run it.
```php
$router->run();
```