- Easy tu use PHP Router
If you are looking for an easy start for your PHP router. Then this PHP Class is for you.
Sure!
This software is distributed under the GNU General Public License v3.0 license. Please read LICENSE for information on the software availability and distribution.
- PHP >= 5.6
- Apache2
Please disclose any vulnerabilities found responsibly – report security issues to the maintainers privately.
Using Composer:
composer require laswitchtech/php-router
Let's start with the skeleton of your project directory.
├── .htaccess
├── index.php
├── config
│ ├── requirements.json
│ └── routes.json
├── dist
│ ├── img
│ ├── css
│ └── js
├── Template
│ └── index.php
├── View
│ ├── index.php
│ └── 404.php
└── webroot
- .htaccess: This file will enable mod_rewrite in your project. If the file does not exist, phpRouter will create it.
- index.php: This file initiates the Router.
- config: This directory contains all the configuration files.
- config/requirements.json: This file contains a list of server requirements in JSON format.
- config/routes.json: This file contains a list of routes for the Router.
- dist: This directory contains directories for the webroot directory.
- Template/: This directory will contain your templates for your views.
- Template/index.php: This is a template file. These are used to load common Front-End components. For example a Sidebar.
- View/: This directory will contain your 404 view. But you can also use it to store additional views.
- View/404.php: This is the default 404 view file provided by the Router.
- View/index.php: This is the default index view file provided by the Router.
- webroot: This directory will be generated by the router. This intends to prevent directory traversal attacks.
Requirements are what indicates the router if it can run the application or not. phpRouter can check both Apache2 modules and PHP modules. If some requirements are not met, phpRouter will throw a HTTP/1.1 500 Internal Error header. Along with some information about the unmet requirement.
You can define your requirements in a JSON file named requirements.json
in the config directory.
{
"APACHE": [
"mod_rewrite"
]
}
Routes are what indicates the router which file to provide. For example for a route /
the router will load with the file View/index.php
.
Routes have multiple parameters. Here is a list:
-
view = NULL
: This indicates the view file you want to render. -
template = NULL
: This indicates the template file you want to render. The template file always overwrites the view file. -
public = true
: This indicates wether or not a route is public. This is done by looking if$_SESSION['ID']
exist. -
error = NULL
: This indicates the route to use if the public parameter is not met. -
label = NULL
: This provides a label for your route. Useful when setting up templates. -
icon = NULL
: This provides an icon field for your route. Useful when setting up templates.
You can define your routes in a JSON file named routes.json
in the config directory.
[config/routes.json]
{
"404": {
"view": "View/404.php",
"label": "404 - Not Found"
},
"403": {
"view": "View/403.php",
"label": "403 - Access Denied"
},
"500": {
"view": "View/500.php",
"label": "500 - Internal Server Error"
},
"/": {
"view": "View/index.php",
"template": "Template/index.php",
"label": "Dashboard"
},
"/info": {
"view": "View/info.php",
"label": "Information"
}
}
In the View
directory let's create index.php
. In it you can insert some HTML to render.
[View/index.php]
<h1><?= $this->getLabel(); ?></h1>
<ul>
<?php foreach($this->getRoutes() as $route => $param){ ?>
<?php if(str_starts_with($route,'/')){ ?>
<li><a href="<?= $route ?>"><?= $param['label'] ?></a></li>
<?php } ?>
<?php } ?>
</ul>
<p>Welcome to the platform.</p>
In the Template
directory let's create index.php
. In it you can insert some HTML to render.
[Template/index.php]
<html>
<head>
<title><?= $this->getLabel() ?></title>
</head>
<body>
<?php require $this->getView(); ?>
</body>
</html>
These methods are available within your templates and views.
This is done using the getURI
method.
$this->getURI();
This is done using the parseURI
method.
$this->parseURI();
-
$this->getRoute()
: Returns the name of the currently loaded route. -
$this->getRoutes()
: Returns a list of all the available routes. -
$this->getLabel()
: Returns the label of the currently loaded route. -
$this->getIcon()
: Returns the icon of the currently loaded route. -
$this->getTemplate()
: Returns the template file of the currently loaded route. -
$this->getView()
: Returns the view file of the currently loaded route.
This is done using the isConnected
method.
$this->isConnected();
// Return Boolean
This is done using the load
method.
$this->load('info');