From 11da4f3a642a51b5e40dd7b049b924d80a9d96fb Mon Sep 17 00:00:00 2001 From: Skylear Date: Thu, 15 Jul 2021 14:40:58 -0500 Subject: [PATCH] Update README and remove examples for now --- README.md | 32 ++++-- example/include-example.php | 10 -- example/index.php | 214 ------------------------------------ webconfigs/.htaccess | 15 --- webconfigs/readme.md | 5 - webconfigs/web.config | 16 --- 6 files changed, 21 insertions(+), 271 deletions(-) delete mode 100644 example/include-example.php delete mode 100644 example/index.php delete mode 100644 webconfigs/.htaccess delete mode 100644 webconfigs/readme.md delete mode 100644 webconfigs/web.config diff --git a/README.md b/README.md index e0bd348..81fe446 100644 --- a/README.md +++ b/README.md @@ -6,23 +6,21 @@ As this implementation is very simple, it works great as boilerplate for a more ## Usage ```php -// Either include the class... +// Include the class... (this can also be done via autoloading) include 'src\Splashsky\Router.php'; -// Or use the namespace... +// Use the namespace... use Splashsky\Router; -// Add the first route -Router::add('/user/([0-9]*)/edit', function($id) { - echo 'Edit user with id '.$id.'
'; -}, 'get'); +// Add the first GET route... +Router::get('/user/{id}/edit', function ($id) { + return 'Edit user with id '.$id.'
'; +}); -// Run the router -Router::run('/'); +// Run the router! +Router::run(); ``` -There are more complex examples in the `example` directory, in the `index.php` file. - ## Installation The easiest way to use SimpleRouter is to install it in your project via Composer. @@ -33,11 +31,23 @@ composer require splashsky/simplerouter-php Otherwise, download the latest Release and use `include` or `require` in your code. +## Caveats + +Using SimpleRouter is... simple! There's a couple of principles to note, however. + +### Root Route + +You can't have an empty route (`Router::get('', ...);`), as the router **always assumes you at least have a `/` in your URI**. The root route should always be `/`, such as in `Router::get('/', function () {});`. + +### Parameters are in order they appear + +In the example of `/api/hello/{name}`, your first instinct when getting this parmeter in your action is that the variable will be named `$name`. This isn't the case - route parameters are in the order they are found in the route, and names are irrelevant. + ## Routing for subfolders If you're wanting to route for seperate uses (such as an api), you can create another entrypoint (in `/api/v1` for example) and pass a custom base path to the router. ```php -Route::run('/api/v1'); +Router::run('/api/v1'); ``` Ensure that your web server points traffic from `/api/v1` to this entrypoint appropriately. diff --git a/example/include-example.php b/example/include-example.php deleted file mode 100644 index df2e312..0000000 --- a/example/include-example.php +++ /dev/null @@ -1,10 +0,0 @@ - -

Blog

- -
- Hey! Read my new blog post with this cool slug: "" -
- -

- Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. -

diff --git a/example/index.php b/example/index.php deleted file mode 100644 index c4351d3..0000000 --- a/example/index.php +++ /dev/null @@ -1,214 +0,0 @@ - -
  • home
  • -
  • index.php
  • -
  • edit user 3
  • -
  • foo 5 bar
  • -
  • long route example
  • -
  • contact form
  • -
  • get+post example
  • -
  • test.html
  • -
  • How to push data to included files
  • -
  • PHP Info
  • -
  • Non english route: german
  • -
  • Non english route: arabic
  • -
  • Inject variables to local scope
  • -
  • Return instead of echo test
  • -
  • Arrow function test (please enable this route first)
  • -
  • aTrailingSlashDoesNotMatter
  • -
  • aTrailingSlashDoesNotMatter/
  • -
  • theCaseDoesNotMatter
  • -
  • thecasedoesnotmatter
  • -
  • 404 Test
  • -
  • 405 Test
  • -
  • known routes
  • - - '; -} - -// Add base route (startpage) -Route::add('/', function() { - navi(); - echo 'Welcome :-)'; -}); - -// Another base route example -Route::add('/index.php', function() { - navi(); - echo 'You are not really on index.php ;-)'; -}); - -// Simple test route that simulates static html file -Route::add('/test.html', function() { - navi(); - echo 'Hello from test.html'; -}); - -// This example shows how to include files and how to push data to them -Route::add('/blog/([a-z-0-9-]*)', function($slug) { - navi(); - include('include-example.php'); -}); - -// This route is for debugging only -// It simply prints out some php infos -// Do not use this route on production systems! -Route::add('/phpinfo', function() { - navi(); - phpinfo(); -}); - -// Get route example -Route::add('/contact-form', function() { - navi(); - echo '
    '; -}, 'get'); - -// Post route example -Route::add('/contact-form', function() { - navi(); - echo 'Hey! The form has been sent:
    '; - print_r($_POST); -}, 'post'); - -// Get and Post route example -Route::add('/get-post-sample', function() { - navi(); - echo 'You can GET this page and also POST this form back to it'; - echo '
    '; - if (isset($_POST['input'])) { - echo 'I also received a POST with this data:
    '; - print_r($_POST); - } -}, ['get','post']); - -// Route with regexp parameter -// Be aware that (.*) will match / (slash) too. For example: /user/foo/bar/edit -// Also users could inject SQL statements 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.'
    '; -}); - -// 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.'
    '; -}); - -// Long route example -// By default this route gets never triggered because the route before matches too -Route::add('/foo/bar/foo/bar', function() { - echo 'This is the second match (This route should only work in multi match mode)
    '; -}); - -// Route with non english letters: german example -Route::add('/äöü', function() { - navi(); - echo 'German example. Non english letters should work too
    '; -}); - -// Route with non english letters: arabic example -Route::add('/الرقص-العربي', function() { - navi(); - echo 'Arabic example. Non english letters should work too
    '; -}); - -// Use variables from global scope -// You can use for example use() to inject variables to local scope -// You can use global to register the variable in local scope -$foo = 'foo'; -$bar = 'bar'; -Route::add('/global/([a-z-0-9-]*)', function($param) use($foo) { - global $bar; - navi(); - echo 'The param is '.$param.'
    '; - echo 'Foo is '.$foo.'
    '; - echo 'Bar is '.$bar.'
    '; -}); - -// Return example -// Returned data gets printed -Route::add('/return', function() { - navi(); - return 'This text gets returned by the add method'; -}); - -// Arrow function example -// Note: You can use this example only if you are on PHP 7.4 or higher -// $bar = 'bar'; -// Route::add('/arrow/([a-z-0-9-]*)', fn($foo) => navi().'This is a working arrow function example.
    Parameter: '.$foo. '
    Variable from global scope: '.$bar ); - -// Trailing slash example -Route::add('/aTrailingSlashDoesNotMatter', function() { - navi(); - echo 'a trailing slash does not matter
    '; -}); - -// Case example -Route::add('/theCaseDoesNotMatter',function() { - navi(); - echo 'the case does not matter
    '; -}); - -// 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) { - // Do not forget to send a status header back to the client - // The router will not send any headers by default - // So you will have the full flexibility to handle this case - header('HTTP/1.0 404 Not Found'); - navi(); - echo 'Error 404 :-(
    '; - echo 'The requested path "'.$path.'" was not found!'; -}); - -// Add a 405 method not allowed route -Route::methodNotAllowed(function($path, $method) { - // Do not forget to send a status header back to the client - // The router will not send any headers by default - // So you will have the full flexibility to handle this case - header('HTTP/1.0 405 Method Not Allowed'); - navi(); - echo 'Error 405 :-(
    '; - echo 'The requested path "'.$path.'" exists. But the request method "'.$method.'" is not allowed on this path!'; -}); - -// Return all known routes -Route::add('/known-routes', function() { - navi(); - $routes = Route::getAll(); - echo ''; -}); - -// Run the Router with the given Basepath -Route::run(BASEPATH); - -// Enable case sensitive mode, trailing slashes and multi match mode by setting the params to true -// Route::run(BASEPATH, true, true, true); diff --git a/webconfigs/.htaccess b/webconfigs/.htaccess deleted file mode 100644 index dbaa68c..0000000 --- a/webconfigs/.htaccess +++ /dev/null @@ -1,15 +0,0 @@ -DirectoryIndex index.php - -# Enable Apache rewrite engine -RewriteEngine on - -# Set the rewrite base path -# Pass this to the run() method too, if your entry point is in a subfolder -RewriteBase / - -# Deliver the folder or file directly if it exists on the server -RewriteCond %{REQUEST_FILENAME} !-f -RewriteCond %{REQUEST_FILENAME} !-d - -# Push every request to index.php -RewriteRule ^(.*)$ index.php [QSA] \ No newline at end of file diff --git a/webconfigs/readme.md b/webconfigs/readme.md deleted file mode 100644 index ff0ac27..0000000 --- a/webconfigs/readme.md +++ /dev/null @@ -1,5 +0,0 @@ -# Example Web Server Configs -These are example configs you'd want to use when using SimpleRouter. - -`.htaccess` is for Apache-based web servers.
    -`web.config` is for IIS/Microsoft-based web servers. \ No newline at end of file diff --git a/webconfigs/web.config b/webconfigs/web.config deleted file mode 100644 index 96879bd..0000000 --- a/webconfigs/web.config +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - - - - - - - -