Loading repository data…
Loading repository data…
skipperbent / repository
Simple, fast and yet powerful PHP router that is easy to get integrated and in any project. Heavily inspired by the way Laravel handles routing, with both simplicity and expand-ability in mind.
A transparent discovery signal based on current public GitHub metadata.
This score does not audit code, security, maintainers, documentation quality, or suitability. Verify the repository and its current documentation before adoption.
Simple, fast and yet powerful PHP router that is easy to get integrated and in any project. Heavily inspired by the way Laravel handles routing, with both simplicity and expand-ability in mind.
With simple-router you can create a new project fast, without depending on a framework.
It only takes a few lines of code to get started:
SimpleRouter::get('/', function() {
return 'Hello world';
});
If you like simple-router and wish to see the continued development and maintenance of the project, please consider showing your support by buying me a coffee. Supporters will be listed under the credits section of this documentation.
You can donate any amount of your choice by clicking here.
Add the latest version of the simple-router project running this command.
composer require pecee/simple-router
The goal of this project is to create a router that is more or less 100% compatible with the Laravel documentation, while remaining as simple as possible, and as easy to integrate and change without compromising either speed or complexity. Being lightweight is the #1 priority.
We've included a simple demo project for the router which can be found here. This project should give you a basic understanding of how to setup and use simple-php-router project.
Please note that the demo-project only covers how to integrate the simple-php-router in a project without an existing framework. If you are using a framework in your project, the implementation might vary.
You can find the demo-project here: https://github.com/skipperbent/simple-router-demo
What we won't cover:
What we cover:
GET, POST, PUT, PATCH, UPDATE, DELETE) with support for custom multiple verbs.GET, POST and FILE values.composer require pecee/simple-router
If you are using Nginx please make sure that url-rewriting is enabled.
You can easily enable url-rewriting by adding the following configuration for the Nginx configuration-file for the demo-project.
location / {
try_files $uri $uri/ /index.php?$query_string;
}
Nothing special is required for Apache to work. We've include the .htaccess file in the public folder. If rewriting is not working for you, please check that the mod_rewrite module (htaccess support) is enabled in the Apache configuration.
Below is an example of an working .htaccess file used by simple-php-router.
Simply create a new .htaccess file in your projects public directory and paste the contents below in your newly created file. This will redirect all requests to your index.php file (see Configuration section below).
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-l
RewriteRule ^(.*)$ index.php/$1
On IIS you have to add some lines your web.config file in the public folder or create a new one. If rewriting is not working for you, please check that your IIS version have included the url rewrite module or download and install them from Microsoft web site.
Below is an example of an working web.config file used by simple-php-router.
Simply create a new web.config file in your projects public directory and paste the contents below in your newly created file. This will redirect all requests to your index.php file (see Configuration section below). If the web.config file already exists, add the <rewrite> section inside the <system.webServer> branch.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<!-- Remove slash '/' from the en of the url -->
<rule name="RewriteRequestsToPublic">
<match url="^(.*)$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
</conditions>
<action type="Rewrite" url="/{R:0}" />
</rule>
<!-- When requested file or folder don't exists, will request again through index.php -->
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^(.*)$" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="/index.php/{R:1}" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
If you do not have a favicon.ico file in your project, you can get a NotFoundHttpException (404 - not found).
To add favicon.ico to the IIS ignore-list, add the following line to the <conditions> group:
<add input="{REQUEST_FILENAME}" negate="true" pattern="favicon.ico" ignoreCase="true" />
You can also make one exception for files with some extensions:
<add input="{REQUEST_FILENAME}" pattern="\.ico|\.png|\.css|\.jpg" negate="true" ignoreCase="true" />
If you are using $_SERVER['ORIG_PATH_INFO'], you will get \index.php\ as part of the returned value.
Example:
/index.php/test/mypage.php
Create a new file, name it routes.php and place it in your library folder. This will be the file where you define all the routes for your project.
WARNING: NEVER PLACE YOUR ROUTES.PHP IN YOUR PUBLIC FOLDER!
In your index.php require your newly-created routes.php and call the SimpleRouter::start() method. This will trigger and do the actual routing of the requests.
It's not required, but you can set SimpleRouter::setDefaultNamespace('\Demo\Controllers'); to prefix all routes with the namespace to your controllers. This will simplify things a bit, as you won't have to specify the namespace for your controllers on each route.
This is an example of a basic index.php file:
<?php
use Pe