Merge remote-tracking branch 'upstream/master'
This commit is contained in:
commit
d3f5e77ad3
20
README.md
20
README.md
|
@ -30,12 +30,28 @@ If your script lives in a subfolder e.g. /api/v1 set this basepath in your run m
|
|||
|
||||
Do not forget to edit the basepath in .htaccess too if you are on Apache2. In order to run the test files correctly inside a basepath you should also adjust the navigation links inside the index.php.
|
||||
|
||||
## Enable case sensitive routes and trailing slashes
|
||||
The second and third parameters of ```Route::run('/', false, false);``` are both set to false by default.
|
||||
You can enable case sensitive mode by setting the second parameter to true.
|
||||
By default the router will ignore trailing slashes. Set the third parameter to true to avoid this.
|
||||
|
||||
## Something does not work?
|
||||
* Dont forget to set the correct basepath as argument in your run method and in your .htaccess file.
|
||||
* Enable mod_rewrite in your Apache2 settings
|
||||
|
||||
## Test setup
|
||||
There is a little Vagrant test setup. Just run ```vagrant up``` to spin up a Apache2 Webserver on Ubuntu. Then navigate to http://router.local after adding the machine IP to your hosts file.
|
||||
## Test setup with Docker
|
||||
I have created a little Docker test setup.
|
||||
|
||||
1. Build the image: ```docker build -t simplephprouter docker/image```
|
||||
|
||||
2. Spin up a container
|
||||
* On Linux / Mac or Windows Powershell use: ```docker run -d -p 80:80 -v $(pwd):/var/www/html --name simplephprouter simplephprouter```
|
||||
* On Windows CMD use ```docker run -d -p 80:80 -v %cd%:/var/www/html --name simplephprouter simplephprouter```
|
||||
|
||||
3. Open your browser and navigate to http://localhost
|
||||
|
||||
## Test setup with Vagrant (not longer maintained)
|
||||
There is a little Vagrant test setup. Just run ```vagrant up``` to spin up a Apache2 Webserver on Ubuntu. Then navigate to http://router.local after adding the machine IP to your hosts file. This test setup is not longer maintained and will probably break in future. Use the docker test setup instead.
|
||||
|
||||
## Todo
|
||||
* Create demo configuration for nginx
|
||||
|
|
12
Route.php
12
Route.php
|
@ -22,13 +22,17 @@ class Route{
|
|||
self::$methodNotAllowed = $function;
|
||||
}
|
||||
|
||||
public static function run($basepath = '/'){
|
||||
public static function run($basepath = '/', $case_matters = false, $trailing_slash_matters = false){
|
||||
|
||||
// Parse current url
|
||||
$parsed_url = parse_url($_SERVER['REQUEST_URI']);//Parse Uri
|
||||
|
||||
if(isset($parsed_url['path']) && $parsed_url['path'] != '/'){
|
||||
$path = rtrim($parsed_url['path'], '/');
|
||||
if($trailing_slash_matters){
|
||||
$path = $parsed_url['path'];
|
||||
}else{
|
||||
$path = rtrim($parsed_url['path'], '/');
|
||||
}
|
||||
}else{
|
||||
$path = '/';
|
||||
}
|
||||
|
@ -57,8 +61,8 @@ class Route{
|
|||
|
||||
// echo $route['expression'].'<br/>';
|
||||
|
||||
// Check path match
|
||||
if(preg_match('#'.$route['expression'].'#',$path,$matches)){
|
||||
// Check path match
|
||||
if(preg_match('#'.$route['expression'].'#'.($case_matters ? '':'i'),$path,$matches)){
|
||||
|
||||
$path_match_found = true;
|
||||
|
||||
|
|
3
docker/image/Dockerfile
Normal file
3
docker/image/Dockerfile
Normal file
|
@ -0,0 +1,3 @@
|
|||
FROM php:7.2-apache
|
||||
|
||||
RUN a2enmod rewrite
|
63
index.php
63
index.php
|
@ -1,44 +1,57 @@
|
|||
|
||||
Navigation:
|
||||
<ul>
|
||||
<li><a href="/">home</a></li>
|
||||
<li><a href="/index.php">index.php</a></li>
|
||||
<li><a href="/user/3/edit">edit user 3</a></li>
|
||||
<li><a href="/foo/5/bar">foo 5 bar</a></li>
|
||||
<li><a href="/foo/bar/foo/bar">long route example</a></li>
|
||||
<li><a href="/contact-form">contact form</a></li>
|
||||
<li><a href="/test.html">test.html</a></li>
|
||||
<li><a href="/this-route-is-not-defined">404 Test</a></li>
|
||||
<li><a href="/this-route-is-defined">405 Test</a></li>
|
||||
</ul>
|
||||
|
||||
<?PHP
|
||||
|
||||
// This function just prints a simple navigation
|
||||
function navi () {
|
||||
?>
|
||||
Navigation:
|
||||
<ul>
|
||||
<li><a href="/">home</a></li>
|
||||
<li><a href="/index.php">index.php</a></li>
|
||||
<li><a href="/user/3/edit">edit user 3</a></li>
|
||||
<li><a href="/foo/5/bar">foo 5 bar</a></li>
|
||||
<li><a href="/foo/bar/foo/bar">long route example</a></li>
|
||||
<li><a href="/contact-form">contact form</a></li>
|
||||
<li><a href="/test.html">test.html</a></li>
|
||||
<li><a href="/aTrailingSlashDoesNotMatters">aTrailingSlashDoesNotMatters</a></li>
|
||||
<li><a href="/aTrailingSlashDoesNotMatters/">aTrailingSlashDoesNotMatters/</a></li>
|
||||
<li><a href="/theCaseDoesNotMatters">theCaseDoesNotMatters</a></li>
|
||||
<li><a href="/thecasedoesnotmatters">thecasedoesnotmatters</a></li>
|
||||
<li><a href="/this-route-is-not-defined">404 Test</a></li>
|
||||
<li><a href="/this-route-is-defined">405 Test</a></li>
|
||||
</ul>
|
||||
<?PHP
|
||||
}
|
||||
|
||||
// Include router class
|
||||
include('Route.php');
|
||||
|
||||
// Add base route (startpage)
|
||||
Route::add('/',function(){
|
||||
navi();
|
||||
echo 'Welcome :-)';
|
||||
});
|
||||
|
||||
// Another base route example
|
||||
Route::add('/index.php',function(){
|
||||
navi();
|
||||
echo 'You are not realy on index.php ;-)';
|
||||
});
|
||||
|
||||
// Simple test route that simulates static html file
|
||||
Route::add('/test.html',function(){
|
||||
navi();
|
||||
echo 'Hello from test.html';
|
||||
});
|
||||
|
||||
// Post route example
|
||||
Route::add('/contact-form',function(){
|
||||
navi();
|
||||
echo '<form method="post"><input type="text" name="test" /><input type="submit" value="send" /></form>';
|
||||
},'get');
|
||||
|
||||
// Post route example
|
||||
Route::add('/contact-form',function(){
|
||||
navi();
|
||||
echo 'Hey! The form has been sent:<br/>';
|
||||
print_r($_POST);
|
||||
},'post');
|
||||
|
@ -48,38 +61,57 @@ Route::add('/contact-form',function(){
|
|||
// Also users could inject mysql-code or other untrusted data if you use (.*)
|
||||
// You should better use a saver expression like /user/([0-9]*)/edit or /user/([A-Za-z]*)/edit
|
||||
Route::add('/user/(.*)/edit',function($id){
|
||||
navi();
|
||||
echo 'Edit user with id '.$id.'<br/>';
|
||||
});
|
||||
|
||||
// Accept only numbers as parameter. Other characters will result in a 404 error
|
||||
Route::add('/foo/([0-9]*)/bar',function($var1){
|
||||
navi();
|
||||
echo $var1.' is a great number!';
|
||||
});
|
||||
|
||||
// Crazy route with parameters
|
||||
Route::add('/(.*)/(.*)/(.*)/(.*)',function($var1,$var2,$var3,$var4){
|
||||
navi();
|
||||
echo 'This is the first match: '.$var1.' / '.$var2.' / '.$var3.' / '.$var4.'<br/>';
|
||||
});
|
||||
|
||||
// Long route example
|
||||
// This route gets never triggered because the route before matches too
|
||||
Route::add('/foo/bar/foo/bar',function(){
|
||||
navi();
|
||||
echo 'This is the second match <br/>';
|
||||
});
|
||||
|
||||
// Trailing slash example
|
||||
Route::add('/aTrailingSlashDoesNotMatters',function(){
|
||||
navi();
|
||||
echo 'a trailing slash does not matters<br/>';
|
||||
});
|
||||
|
||||
// Case example
|
||||
Route::add('/theCaseDoesNotMatters',function(){
|
||||
navi();
|
||||
echo 'the case does not matters<br/>';
|
||||
});
|
||||
|
||||
// 405 test
|
||||
Route::add('/this-route-is-defined',function(){
|
||||
navi();
|
||||
echo 'You need to patch this route to see this content';
|
||||
},'patch');
|
||||
|
||||
// Add a 404 not found route
|
||||
Route::pathNotFound(function($path){
|
||||
navi();
|
||||
echo 'Error 404 :-(<br/>';
|
||||
echo 'The requested path "'.$path.'" was not found!';
|
||||
});
|
||||
|
||||
// Add a 405 method not allowed route
|
||||
Route::methodNotAllowed(function($path, $method){
|
||||
navi();
|
||||
echo 'Error 405 :-(<br/>';
|
||||
echo 'The requested path "'.$path.'" exists. But the request method "'.$method.'" is not allowed on this path!';
|
||||
});
|
||||
|
@ -92,4 +124,7 @@ Route::run('/');
|
|||
// Do not forget to edit the basepath in .htaccess if you are on apache
|
||||
// Route::run('/api/v1');
|
||||
|
||||
// Enable case sensitive mode and trailing slashes by setting both to true
|
||||
// Route::run('/', true, true);
|
||||
|
||||
?>
|
Loading…
Reference in New Issue
Block a user