diff --git a/README.md b/README.md index 3b94a41..b7eb3ac 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ If you want a great new feature or experience any issues what-so-ever, please fe - Setting up Nginx - Setting up simple-php-router - Helper methods - + - Routes - Basic routing - Available methods @@ -57,7 +57,7 @@ If you want a great new feature or experience any issues what-so-ever, please fe - Urls - Get by name (single route) - Get by name (controller route) - - Get by class + - Get by class - Get by custom names for methods on a controller/resource route - Getting REST/resource controller urls @@ -101,7 +101,7 @@ We've included a simple demo project for the router which can be found in the `d 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]() +You can find the demo-project here: [https://github.com/skipperbent/simple-router-demo](https://github.com/skipperbent/simple-router-demo) **What we won't cover:** @@ -355,7 +355,7 @@ SimpleRouter::get('/user/{id}/{name}', function ($id, $name) { })->where(['id' => '[0-9]+', 'name' => '[a-z]+']); ``` -### Regular expression route-match +### Regular expression route-match You can define a regular-expression match for the entire route if you wish. @@ -367,7 +367,7 @@ The example below is using the following regular expression: `/ajax/([\w]+)/?([0 **Doesn't match:** `/ajax/` -Match groups specified in the regex will be passed on as parameters: +Match groups specified in the regex will be passed on as parameters: ```php SimpleRouter::all('/ajax/abc/123', function($param1, $param2) { @@ -532,7 +532,7 @@ SimpleRouter::get('/page/404', 'ControllerPage@notFound', ['as' => 'page.notfoun # CSRF Protection -Any forms posting to `POST`, `PUT` or `DELETE` routes should include the CSRF-token. We strongly recommend that you create your enable CSRF-verification on your site. +Any forms posting to `POST`, `PUT` or `DELETE` routes should include the CSRF-token. We strongly recommend that you create your enable CSRF-verification on your site. Create a new class and extend the ```BaseCsrfVerifier``` middleware class provided with simple-php-router. diff --git a/demo-project/README.md b/demo-project/README.md deleted file mode 100644 index ee0ec84..0000000 --- a/demo-project/README.md +++ /dev/null @@ -1,100 +0,0 @@ -# Simple PHP router demo project - -This project is here to give you a basic understanding of how to setup and using simple-php-router. - -Please note that this demo-project only covers how to integrate the `simple-php-router` in a project without a framework. If you are using some sort of PHP framework in your project the implementation might vary. - -**What we won't cover:** - -- How to setup a solution that fits your need. This is a basic demo to help you get started. -- Understanding of MVC; including Controllers, Middlewares or ExceptionHandlers. -- How to integrate into third party frameworks. - -**What we cover:** - -- How to get up and running fast - from scratch. -- How to get ExceptionHandlers, Middlewares and Controllers working. -- How to setup your webservers. - -## Installation - -- Navigate to the `demo-project` folder in terminal and run `composer update` to install the latest version. -- Point your webserver to `demo-project/public`. - -### Setting up Nginx - -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; -} -``` - -### Setting up Apache - -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. - -## Folder structure - -| Folder | Description | -| ------------- |-------------| -| app |Contains projects-specific PHP classes| -| public |Public folder which are accessible through the web.| - -## Notes - -The demo project has it's own `Router` class implementation which extends the `SimpleRouter` class with further functionality. -This class can be useful adding additional functionality that are required before and after routing occurs or any extra functionality belonging to the router itself. - -In this project we also use our custom router-class to autoload the `routes.php` file from our custom location (`app/routes.php`). - -Please check the `routes.php` file in `demo-project/app` for all the urls/rules available in the project. - -### CSRF-verifier - -For the purpose of this demo, we've added a custom CSRF-verifier middleware called `CsrfVerifier` and disabled CSRF checks for all calls to `/api/*`. This will ensure that CSRF form-checks are not applied when calling our demo api url. - -### Exception handlers - -The included `CustomExceptionHandler` class returns a very basic json response for errors received on calls to `/api/*` or otherwise just a simple formatted error response. - -### Middlewares - -`ApiVerification` class is added to all calls to `/api/*`. This simple class just adds some data to the `Request` object, which is returned in one of the methods in the `ApiController` class. We've added this class to demonstrate that you can use middlewares to ensure that the user has the correct authentication - before router loads the controller itself. - -### Urls - -Please see `routes.php` for all routes and rules. - -| URL | -| ------------- | -| / | -| /api/demo | -| /companies | -| /companies/[id] | -| /contact | - -## The MIT License (MIT) - -Copyright (c) 2016 Simon Sessingø / simple-php-router - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/demo-project/app/Controllers/ApiController.php b/demo-project/app/Controllers/ApiController.php deleted file mode 100644 index e163233..0000000 --- a/demo-project/app/Controllers/ApiController.php +++ /dev/null @@ -1,16 +0,0 @@ - request()->authenticated - ]); - } - -} \ No newline at end of file diff --git a/demo-project/app/Controllers/DefaultController.php b/demo-project/app/Controllers/DefaultController.php deleted file mode 100644 index ee1f9ff..0000000 --- a/demo-project/app/Controllers/DefaultController.php +++ /dev/null @@ -1,27 +0,0 @@ - index (?fun=%s)', input()->get('fun')); - } - - public function contact() - { - echo 'DefaultController -> contact'; - } - - public function companies($id = null) - { - echo 'DefaultController -> companies -> id: ' . $id; - } - - public function notFound() - { - echo 'Page not found'; - } - -} \ No newline at end of file diff --git a/demo-project/app/Handlers/CustomExceptionHandler.php b/demo-project/app/Handlers/CustomExceptionHandler.php deleted file mode 100644 index 6eca147..0000000 --- a/demo-project/app/Handlers/CustomExceptionHandler.php +++ /dev/null @@ -1,44 +0,0 @@ -getUri(), '/api') !== false) { - - response()->json([ - 'error' => $error->getMessage(), - 'code' => $error->getCode(), - ]); - - } - - /* The router will throw the NotFoundHttpException on 404 */ - if($error instanceof NotFoundHttpException) { - - /* - * Render your own custom 404-view, rewrite the request to another route, - * or simply return the $request object to ignore the error and continue on rendering the route. - * - * The code below will make the router render our page.notfound route. - */ - - $request->setUri(url('page.notfound')); - return $request; - - } - - throw $error; - - } - -} \ No newline at end of file diff --git a/demo-project/app/Middlewares/ApiVerification.php b/demo-project/app/Middlewares/ApiVerification.php deleted file mode 100644 index ce9e177..0000000 --- a/demo-project/app/Middlewares/ApiVerification.php +++ /dev/null @@ -1,18 +0,0 @@ -authenticated = true; - - return $request; - } - -} \ No newline at end of file diff --git a/demo-project/app/Middlewares/CsrfVerifier.php b/demo-project/app/Middlewares/CsrfVerifier.php deleted file mode 100644 index 636dd12..0000000 --- a/demo-project/app/Middlewares/CsrfVerifier.php +++ /dev/null @@ -1,13 +0,0 @@ -getToken(); -} - -/** - * Get request object - * @return \Pecee\Http\Request - */ -function request() -{ - return SimpleRouter::request(); -} - -/** - * Get response object - * @return \Pecee\Http\Response - */ -function response() -{ - return SimpleRouter::response(); -} - -/** - * Get input class - * @return \Pecee\Http\Input\Input - */ -function input() -{ - return SimpleRouter::request()->getInput(); -} \ No newline at end of file diff --git a/demo-project/app/routes.php b/demo-project/app/routes.php deleted file mode 100644 index 4b5e3f7..0000000 --- a/demo-project/app/routes.php +++ /dev/null @@ -1,25 +0,0 @@ - 'Demo\Handlers\CustomExceptionHandler'], function () { - - Router::get('/', 'DefaultController@index')->setName('home'); - - Router::get('/contact', 'DefaultController@contact')->setName('contact'); - - Router::get('/404', 'DefaultController@notFound')->setName('404'); - - Router::basic('/companies/{id?}', 'DefaultController@companies')->setName('companies'); - - // Api - Router::group(['prefix' => '/api', 'middleware' => 'Demo\Middlewares\ApiVerification'], function () { - Router::resource('/demo', 'ApiController'); - }); - -}); \ No newline at end of file diff --git a/demo-project/composer.json b/demo-project/composer.json deleted file mode 100644 index 61c531c..0000000 --- a/demo-project/composer.json +++ /dev/null @@ -1,26 +0,0 @@ -{ - "name": "pecee/simple-router-demo", - "description": "Simple router demo project", - "keywords": [ - "simple-router", - "php", - "php-simple-router" - ], - "license": "MIT", - "type": "project", - "require": { - "php": ">=5.4.0", - "pecee/simple-router": "2.*" - }, - "require-dev": { - - }, - "config": { - "preferred-install": "dist" - }, - "autoload": { - "psr-4": { - "Demo\\": "app/" - } - } -} \ No newline at end of file diff --git a/demo-project/public/.htaccess b/demo-project/public/.htaccess deleted file mode 100644 index aa7a5f6..0000000 --- a/demo-project/public/.htaccess +++ /dev/null @@ -1,5 +0,0 @@ -RewriteEngine on -RewriteCond %{SCRIPT_FILENAME} !-f -RewriteCond %{SCRIPT_FILENAME} !-d -RewriteCond %{SCRIPT_FILENAME} !-l -RewriteRule ^(.*)$ index.php/$1 \ No newline at end of file diff --git a/demo-project/public/index.php b/demo-project/public/index.php deleted file mode 100644 index ea01d8d..0000000 --- a/demo-project/public/index.php +++ /dev/null @@ -1,7 +0,0 @@ -