laswitchtech/php-router

Router Class for PHP


License
GPL-3.0-only

Documentation

GitHub repo logo

phpRouter

License GitHub repo size GitHub top language Version

Features

  • Easy tu use PHP Router

Why you might need it

If you are looking for an easy start for your PHP router. Then this PHP Class is for you.

Can I use this?

Sure!

License

This software is distributed under the GNU General Public License v3.0 license. Please read LICENSE for information on the software availability and distribution.

Requirements

  • PHP >= 5.6
  • Apache2

Security

Please disclose any vulnerabilities found responsibly – report security issues to the maintainers privately.

Installation

Using Composer:

composer require laswitchtech/php-router

How do I use it?

Skeleton

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

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.

Adding Requirements

You can define your requirements in a JSON file named requirements.json in the config directory.

{
  "APACHE": [
    "mod_rewrite"
  ]
}

Routes

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.

Available Parameters

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.

How do I start?

First, Setup your routes

You can define your routes in a JSON file named routes.json in the config directory.

Example

[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"
  }
}

Create your first view

In the View directory let's create index.php. In it you can insert some HTML to render.

Example

[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>

Create your first template

In the Template directory let's create index.php. In it you can insert some HTML to render.

Example

[Template/index.php]

<html>
  <head>
    <title><?= $this->getLabel() ?></title>
  </head>
  <body>
    <?php require $this->getView(); ?>
  </body>
</html>

Available methods

These methods are available within your templates and views.

Parsing URL for Route

This is done using the getURI method.

$this->getURI();

Parsing URL for Variables

This is done using the parseURI method.

$this->parseURI();

Getters

  • $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.

Check if there is an active session

This is done using the isConnected method.

$this->isConnected();
// Return Boolean

Load a specific route

This is done using the load method.

$this->load('info');