Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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.

NotificationsYou must be signed in to change notification settings

skipperbent/simple-php-router

Repository files navigation

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';});

Support the project

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 byclicking here.

Table of Contents


Getting started

Add the latest version of the simple-router project running this command.

composer require pecee/simple-router

Notes

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 foundhere. 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 thesimple-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:

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

Requirements

  • PHP 7.1 or greater (version 3.x and below supports PHP 5.5+)
  • PHP JSON extension enabled.

Features

  • Basic routing (GET,POST,PUT,PATCH,UPDATE,DELETE) with support for custom multiple verbs.
  • Regular Expression Constraints for parameters.
  • Named routes.
  • Generating url to routes.
  • Route groups.
  • Middleware (classes that intercepts before the route is rendered).
  • Namespaces.
  • Route prefixes.
  • CSRF protection.
  • Optional parameters
  • Sub-domain routing
  • Custom boot managers to rewrite urls to "nicer" ones.
  • Input manager; easily manageGET,POST andFILE values.
  • IP based restrictions.
  • Easily extendable.

Installation

  1. Navigate to your project folder in terminal and run the following command:
composerrequire pecee/simple-router

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 thepublic folder. If rewriting is not working for you, please check that themod_rewrite module (htaccess support) is enabled in the Apache configuration.

.htaccess example

Below is an example of an working.htaccess file used by simple-php-router.

Simply create a new.htaccess file in your projectspublic directory and paste the contents below in your newly created file. This will redirect all requests to yourindex.php file (see Configuration section below).

RewriteEngine onRewriteCond %{SCRIPT_FILENAME} !-fRewriteCond %{SCRIPT_FILENAME} !-dRewriteCond %{SCRIPT_FILENAME} !-lRewriteRule ^(.*)$ index.php/$1

Setting up IIS

On IIS you have to add some lines yourweb.config file in thepublic folder or create a new one. If rewriting is not working for you, please check that your IIS version have included theurl rewrite module or download and install them from Microsoft web site.

web.config example

Below is an example of an workingweb.config file used by simple-php-router.

Simply create a newweb.config file in your projectspublic directory and paste the contents below in your newly created file. This will redirect all requests to yourindex.php file (see Configuration section below). If theweb.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--><rulename="RewriteRequestsToPublic">  <matchurl="^(.*)$" />  <conditionslogicalGrouping="MatchAll"trackAllCaptures="false">  </conditions>  <actiontype="Rewrite"url="/{R:0}" /></rule><!-- When requested file or folder don't exists, will request again through index.php--><rulename="Imported Rule 1"stopProcessing="true">  <matchurl="^(.*)$"ignoreCase="true" />  <conditionslogicalGrouping="MatchAll"><addinput="{REQUEST_FILENAME}"matchType="IsDirectory"negate="true" /><addinput="{REQUEST_FILENAME}"matchType="IsFile"negate="true" />  </conditions>  <actiontype="Rewrite"url="/index.php/{R:1}"appendQueryString="true" /></rule>  </rules></rewrite>    </system.webServer></configuration>

Troubleshooting

If you do not have afavicon.ico file in your project, you can get aNotFoundHttpException (404 - not found).

To addfavicon.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

Configuration

Create a new file, name itroutes.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 yourindex.php require your newly-createdroutes.php and call theSimpleRouter::start() method. This will trigger and do the actual routing of the requests.

It's not required, but you can setSimpleRouter::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 basicindex.php file:

<?phpusePecee\SimpleRouter\SimpleRouter;/* Load external routes file */require_once'routes.php';/** * The default namespace for route-callbacks, so we don't have to specify it each time. * Can be overwritten by using the namespace config option on your routes. */SimpleRouter::setDefaultNamespace('\Demo\Controllers');// Start the routingSimpleRouter::start();

Helper functions

We recommend that you add these helper functions to your project. These will allow you to access functionality of the router more easily.

To implement the functions below, simply copy the code to a new file and require the file before initializing the router or copy thehelpers.php we've included in this library.

usePecee\SimpleRouter\SimpleRouterasRouter;usePecee\Http\Url;usePecee\Http\Response;usePecee\Http\Request;/** * Get url for a route by using either name/alias, class or method name. * * The name parameter supports the following values: * - Route name * - Controller/resource name (with or without method) * - Controller class name * * When searching for controller/resource by name, you can use this syntax "route.name@method". * You can also use the same syntax when searching for a specific controller-class "MyController@home". * If no arguments is specified, it will return the url for the current loaded route. * * @param string|null $name * @param string|array|null $parameters * @param array|null $getParams * @return \Pecee\Http\Url * @throws \InvalidArgumentException */functionurl(?string$name =null,$parameters =null, ?array$getParams =null):Url{return Router::getUrl($name,$parameters,$getParams);}/** * @return \Pecee\Http\Response */functionresponse():Response{return Router::response();}/** * @return \Pecee\Http\Request */functionrequest():Request{return Router::request();}/** * Get input class * @param string|null $index Parameter index name * @param string|mixed|null $defaultValue Default return value * @param array ...$methods Default methods * @return \Pecee\Http\Input\InputHandler|array|string|null */functioninput($index =null,$defaultValue =null, ...$methods){if ($index !==null) {returnrequest()->getInputHandler()->value($index,$defaultValue, ...$methods);    }returnrequest()->getInputHandler();}/** * @param string $url * @param int|null $code */functionredirect(string$url, ?int$code =null):void{if ($code !==null) {response()->httpCode($code);    }response()->redirect($url);}/** * Get current csrf-token * @return string|null */functioncsrf_token(): ?string{$baseVerifier = Router::router()->getCsrfVerifier();if ($baseVerifier !==null) {return$baseVerifier->getTokenProvider()->getToken();    }returnnull;}

Routes

Remember theroutes.php file you required in yourindex.php? This file be where you place all your custom rules for routing.

Basic routing

Below is a very basic example of setting up a route. First parameter is the url which the route should match - next parameter is aClosure or callback function that will be triggered once the route matches.

SimpleRouter::get('/',function() {return'Hello world';});

Class hinting

You can use class hinting to load a class & method like this:

SimpleRouter::get('/', [MyClass::class,'myMethod']);

Available methods

Here you can see a list over all available routes:

SimpleRouter::get($url,$callback,$settings);SimpleRouter::post($url,$callback,$settings);SimpleRouter::put($url,$callback,$settings);SimpleRouter::patch($url,$callback,$settings);SimpleRouter::delete($url,$callback,$settings);SimpleRouter::options($url,$callback,$settings);

Multiple HTTP-verbs

Sometimes you might need to create a route that accepts multiple HTTP-verbs. If you need to match all HTTP-verbs you can use theany method.

SimpleRouter::match(['get','post'],'/',function() {// ...});SimpleRouter::any('foo',function() {// ...});

We've created a simple method which matchesGET andPOST which is most commonly used:

SimpleRouter::form('foo',function() {// ...});

Route parameters

Required parameters

You'll properly wondering by know how you parse parameters from your urls. For example, you might want to capture the users id from an url. You can do so by defining route-parameters.

SimpleRouter::get('/user/{id}',function ($userId) {return'User with id:' .$userId;});

You may define as many route parameters as required by your route:

SimpleRouter::get('/posts/{post}/comments/{comment}',function ($postId,$commentId) {// ...});

Note: Route parameters are always encased within{} braces and should consist of alphabetic characters. Route parameters can only contain certain characters likeA-Z,a-z,0-9,- and_.If your route contain other characters, please seeCustom regex for matching parameters.

Optional parameters

Occasionally you may need to specify a route parameter, but make the presence of that route parameter optional. You may do so by placing a ? mark after the parameter name. Make sure to give the route's corresponding variable a default value:

SimpleRouter::get('/user/{name?}',function ($name =null) {return$name;});SimpleRouter::get('/user/{name?}',function ($name ='Simon') {return$name;});

Including slash in parameters

If you're working with WebDAV services the url could mean the difference between a file and a folder.

For instance/path will be considered a file - whereas/path/ will be considered a folder.

The router can add the ending slash for the last parameter in your route based on the path. So if/path/ is requested the parameter will contain the value ofpath/ and visa versa.

To ensure compatibility with older versions, this feature is disabled by default and has to be enabled by settingthesetSettings(['includeSlash' => true]) or by using settingsetSlashParameterEnabled(true) for your route.

Example

SimpleRouter::get('/path/{fileOrFolder}',function ($fileOrFolder) {return$fileOrFolder;})->setSettings(['includeSlash' =>true]);
  • Requesting/path/file will return the$fileOrFolder value:file.
  • Requesting/path/folder/ will return the$fileOrFolder value:folder/.

Regular expression constraints

You may constrain the format of your route parameters using the where method on a route instance. The where method accepts the name of the parameter and a regular expression defining how the parameter should be constrained:

SimpleRouter::get('/user/{name}',function ($name) {// ... do stuff    })->where(['name' =>'[A-Za-z]+' ]);SimpleRouter::get('/user/{id}',function ($id) {// ... do stuff    })->where(['id' =>'[0-9]+' ]);SimpleRouter::get('/user/{id}/{name}',function ($id,$name) {// ... do stuff    })->where(['id' =>'[0-9]+','name' =>'[a-z]+']);

Regular expression route-match

You can define a regular-expression match for the entire route if you wish.

This is useful if you for example are creating a model-box which loads urls from ajax.

The example below is using the following regular expression:/ajax/([\w]+)/?([0-9]+)?/? which basically just matches/ajax/ and exspects the next parameter to be a string - and the next to be a number (but optional).

Matches:/ajax/abc/,/ajax/abc/123/

Won't match:/ajax/

Match groups specified in the regex will be passed on as parameters:

SimpleRouter::all('/ajax/abc/123',function($param1,$param2) {// param1 = abc// param2 = 123})->setMatch('/\/ajax\/([\w]+)\/?([0-9]+)?\/?/is');

Custom regex for matching parameters

By default simple-php-router uses the[\w\-]+ regular expression. It will matchA-Z,a-z,0-9,- and_ characters in parameters.This decision was made with speed and reliability in mind, as this match will match both letters, number and most of the used symbols on the internet.

However, sometimes it can be necessary to add a custom regular expression to match more advanced characters like foreign lettersæ ø å etc.

You can test your custom regular expression by using on the siteRegex101.com.

Instead of adding a custom regular expression to all your parameters, you can simply add a global regular expression which will be used on all the parameters on the route.

Note: If you the regular expression to be available across, we recommend using the global parameter on a group as demonstrated in the examples below.

Example

This example will ensure that all parameters use the[\w\-\æ\ø\å]+ (a-z,A-Z,-,_,0-9,æ,ø,å) regular expression when parsing.

SimpleRouter::get('/path/{parameter}','VideoController@home', ['defaultParameterRegex' =>'[\w\-\æ\ø\å]+']);

You can also apply this setting to a group if you need multiple routes to use your custom regular expression when parsing parameters.

SimpleRouter::group(['defaultParameterRegex' =>'[\w\-\æ\ø\å]+'],function() {    SimpleRouter::get('/path/{parameter}','VideoController@home');});

Named routes

Named routes allow the convenient generation of URLs or redirects for specific routes. You may specify a name for a route by chaining the name method onto the route definition:

SimpleRouter::get('/user/profile',function () {// Your code here})->name('profile');

You can also specify names for Controller-actions:

SimpleRouter::get('/user/profile','UserController@profile')->name('profile');

Generating URLs To Named Routes

Once you have assigned a name to a given route, you may use the route's name when generating URLs or redirects via the globalurl helper-function (see helpers section):

// Generating URLs...$url =url('profile');

If the named route defines parameters, you may pass the parameters as the second argument to theurl function. The given parameters will automatically be inserted into the URL in their correct positions:

SimpleRouter::get('/user/{id}/profile',function ($id) {//})->name('profile');$url =url('profile', ['id' =>1]);

For more information on urls, please see theUrls section.

Router groups

Route groups allow you to share route attributes, such as middleware or namespaces, across a large number of routes without needing to define those attributes on each individual route. Shared attributes are specified in an array format as the first parameter to theSimpleRouter::group method.

Middleware

To assign middleware to all routes within a group, you may use the middleware key in the group attribute array. Middleware are executed in the order they are listed in the array:

SimpleRouter::group(['middleware' => \Demo\Middleware\Auth::class],function () {    SimpleRouter::get('/',function ()    {// Uses Auth Middleware    });    SimpleRouter::get('/user/profile',function () {// Uses Auth Middleware    });});

Namespaces

Another common use-case for route groups is assigning the same PHP namespace to a group of controllers using thenamespace parameter in the group array:

Note

Group namespaces will only be added to routes with relative callbacks.For example if your route has an absolute callback like\Demo\Controller\DefaultController@home, the namespace from the route will not be prepended.To fix this you can make the callback relative by removing the\ in the beginning of the callback.

SimpleRouter::group(['namespace' =>'Admin'],function () {// Controllers Within The "App\Http\Controllers\Admin" Namespace});

You can add parameters to the prefixes of your routes.

Parameters from your previous routes will be injectedinto your routes after any route-required parameters, starting from oldest to newest.

SimpleRouter::group(['prefix' =>'/lang/{lang}'],function ($language) {        SimpleRouter::get('/about',function($language) {// Will match /lang/da/about        });    });

Subdomain-routing

Route groups may also be used to handle sub-domain routing. Sub-domains may be assigned route parameters just like route urls, allowing you to capture a portion of the sub-domain for usage in your route or controller. The sub-domain may be specified using thedomain key on the group attribute array:

SimpleRouter::group(['domain' =>'{account}.myapp.com'],function () {    SimpleRouter::get('/user/{id}',function ($account,$id) {//    });});

Route prefixes

Theprefix group attribute may be used to prefix each route in the group with a given url. For example, you may want to prefix all route urls within the group withadmin:

SimpleRouter::group(['prefix' =>'/admin'],function () {    SimpleRouter::get('/users',function ()    {// Matches The "/admin/users" URL    });});

You can also use parameters in your groups:

SimpleRouter::group(['prefix' =>'/lang/{language}'],function ($language) {    SimpleRouter::get('/users',function ($language)    {// Matches The "/lang/da/users" URL    });});

Partial groups

Partial router groups has the same benefits as a normal group, butare only rendered once the url has matchedin contrast to a normal group which are always rendered in order to retrieve it's child routes.Partial groups are therefore more like a hybrid of a traditional route with the benefits of a group.

This can be extremely useful in situations where you only want special routes to be added, but only when a certain criteria or logic has been met.

NOTE: Use partial groups with caution as routes added within are only rendered and available once the url of the partial-group has matched.This can causeurl() not to find urls for the routes added within before the partial-group has been matched and is rendered.

Example:

SimpleRouter::partialGroup('/plugin/{name}',function ($plugin) {// Add routes from plugin});

Form Method Spoofing

HTML forms do not supportPUT,PATCH orDELETE actions. So, when definingPUT,PATCH orDELETE routes that are called from an HTML form, you will need to add a hidden_method field to the form. The value sent with the_method field will be used as the HTTP request method:

<input type="hidden" name="_method" value="PUT" />

Accessing The Current Route

You can access information about the current route loaded by using the following method:

SimpleRouter::request()->getLoadedRoute();request()->getLoadedRoute();

Other examples

You can find many more examples in theroutes.php example-file below:

<?phpusePecee\SimpleRouter\SimpleRouter;/* Adding custom csrfVerifier here */SimpleRouter::csrfVerifier(new \Demo\Middlewares\CsrfVerifier());SimpleRouter::group(['middleware' => \Demo\Middlewares\Site::class,'exceptionHandler' => \Demo\Handlers\CustomExceptionHandler::class],function() {    SimpleRouter::get('/answers/{id}','ControllerAnswers@show', ['where' => ['id' =>'[0-9]+']]);/**     * Class hinting is supported too     */          SimpleRouter::get('/answers/{id}', [ControllerAnswers::class,'show'], ['where' => ['id' =>'[0-9]+']]);/**     * Restful resource (see IRestController interface for available methods)     */    SimpleRouter::resource('/rest', ControllerResource::class);/**     * Load the entire controller (where url matches method names - getIndex(), postIndex(), putIndex()).     * The url paths will determine which method to render.     *     * For example:     *     * GET  /animals         => getIndex()     * GET  /animals/view    => getView()     * POST /animals/save    => postSave()     *     * etc.     */    SimpleRouter::controller('/animals', ControllerAnimals::class);});SimpleRouter::get('/page/404','ControllerPage@notFound', ['as' =>'page.notfound']);

CSRF Protection

Any forms posting toPOST,PUT orDELETE routes should include the CSRF-token. We strongly recommend that you enable CSRF-verification on your site to maximize security.

You can use theBaseCsrfVerifier to enable CSRF-validation on all request. If you need to disable verification for specific urls, please refer to the "Custom CSRF-verifier" section below.

By default simple-php-router will use theCookieTokenProvider class. This provider will store the security-token in a cookie on the clients machine.If you want to store the token elsewhere, please refer to the "Creating custom Token Provider" section below.

Adding CSRF-verifier

When you've created your CSRF-verifier you need to tell simple-php-router that it should use it. You can do this by adding the following line in yourroutes.php file:

SimpleRouter::csrfVerifier(new \Demo\Middlewares\CsrfVerifier());

Getting CSRF-token

When posting to any of the urls that has CSRF-verification enabled, you need post your CSRF-token or else the request will get rejected.

You can get the CSRF-token by calling the helper method:

csrf_token();

You can also get the token directly:

return SimpleRouter::router()->getCsrfVerifier()->getTokenProvider()->getToken();

The default name/key for the input-field iscsrf_token and is defined in thePOST_KEY constant in theBaseCsrfVerifier class.You can change the key by overwriting the constant in your own CSRF-verifier class.

Example:

The example below will post to the current url with a hidden field "csrf_token".

<formmethod="post"action="<?= url(); ?>"><inputtype="hidden"name="csrf_token"value="<?= csrf_token(); ?>"><!-- other input elements here --></form>

Custom CSRF-verifier

Create a new class and extend theBaseCsrfVerifier middleware class provided by default with the simple-php-router library.

Add the propertyexcept with an array of the urls to the routes you want to exclude/whitelist from the CSRF validation.Using* at the end for the url will match the entire url.

Here's a basic example on a CSRF-verifier class:

namespaceDemo\Middlewares;usePecee\Http\Middleware\BaseCsrfVerifier;class CsrfVerifierextends BaseCsrfVerifier{/** * CSRF validation will be ignored on the following urls. */protected$except = ['/api/*'];}

Custom Token Provider

By default theBaseCsrfVerifier will use theCookieTokenProvider to store the token in a cookie on the clients machine.

If you need to store the token elsewhere, you can do that by creating your own class and implementing theITokenProvider class.

class SessionTokenProviderimplements ITokenProvider{/**     * Refresh existing token     */publicfunctionrefresh():void    {// Implement your own functionality here...    }/**     * Validate valid CSRF token     *     * @param string $token     * @return bool     */publicfunctionvalidate($token):bool    {// Implement your own functionality here...    }/**     * Get token token     *     * @param string|null $defaultValue     * @return string|null     */publicfunctiongetToken(?string$defaultValue =null): ?string     {// Implement your own functionality here...    }}

Next you need to set your customITokenProvider implementation on yourBaseCsrfVerifier class in your routes file:

$verifier =new \Demo\Middlewares\CsrfVerifier();$verifier->setTokenProvider(newSessionTokenProvider());SimpleRouter::csrfVerifier($verifier);

Middlewares

Middlewares are classes that loads before the route is rendered. A middleware can be used to verify that a user is logged in - or to set parameters specific for the current request/route. Middlewares must implement theIMiddleware interface.

Example

namespaceDemo\Middlewares;usePecee\Http\Middleware\IMiddleware;usePecee\Http\Request;class CustomMiddlewareimplements IMiddleware {publicfunctionhandle(Request$request):void     {// Authenticate user, will be available using request()->user$request->user = User::authenticate();// If authentication failed, redirect request to user-login page.if($request->user ===null) {$request->setRewriteUrl(url('user.login'));        }    }}

ExceptionHandlers

ExceptionHandler are classes that handles all exceptions. ExceptionsHandlers must implement theIExceptionHandler interface.

Handling 404, 403 and other errors

If you simply want to catch a 404 (page not found) etc. you can use theSimpleRouter::error($callback) static helper method.

This will add a callback method which is fired whenever an error occurs on all routes.

The basic example below simply redirect the page to/not-found if anNotFoundHttpException (404) occurred.The code should be placed in the file that contains your routes.

SimpleRouter::get('/not-found','PageController@notFound');SimpleRouter::get('/forbidden','PageController@notFound');SimpleRouter::error(function(Request$request,\Exception$exception) {switch($exception->getCode()) {// Page not foundcase404:response()->redirect('/not-found');// Forbiddencase403:response()->redirect('/forbidden');    }    });

The example above will redirect all errors with http-code404 (page not found) to/not-found and403 (forbidden) to/forbidden.

If you do not want a redirect, but want the error-page rendered on the current-url, you can tell the router to execute a rewrite callback like so:

$request->setRewriteCallback('ErrorController@notFound');

If you will set the correct status for the browser error use:

SimpleRouter::response()->httpCode(404);

Using custom exception handlers

This is a basic example of an ExceptionHandler implementation (please see "Easily overwrite route about to be loaded" for examples on how to change callback).

namespaceDemo\Handlers;usePecee\Http\Request;usePecee\SimpleRouter\Handlers\IExceptionHandler;usePecee\SimpleRouter\Exceptions\NotFoundHttpException;class CustomExceptionHandlerimplements IExceptionHandler{publicfunctionhandleError(Request$request,\Exception$error):void{/* You can use the exception handler to format errors depending on the request and type. */if ($request->getUrl()->contains('/api')) {response()->json(['error' =>$error->getMessage(),'code'  =>$error->getCode(),]);}/* The router will throw the NotFoundHttpException on 404 */if($errorinstanceof NotFoundHttpException) {// Render custom 404-page$request->setRewriteCallback('Demo\Controllers\PageController@notFound');return;}/* Other error */if($errorinstanceof MyCustomException) {$request->setRewriteRoute(// Add new route based on current url (minus query-string) and add custom parameters.(newRouteUrl(url(null,null, []),'PageController@error'))->setParameters(['exception' =>$error]));return;}throw$error;}}

You can add your custom exception-handler class to your group by using theexceptionHandler settings-attribute.exceptionHandler can be either class-name or array of class-names.

SimpleRouter::group(['exceptionHandler' => \Demo\Handlers\CustomExceptionHandler::class],function() {// Your routes here});

Prevent merge of parent exception-handlers

By default the router will merge exception-handlers to any handlers provided by parent groups, and will be executed in the order of newest to oldest.

If you want your groups exception handler to be executed independently, you can add themergeExceptionHandlers attribute and set it tofalse.

SimpleRouter::group(['prefix' =>'/','exceptionHandler' => \Demo\Handlers\FirstExceptionHandler::class,'mergeExceptionHandlers' =>false],function() {SimpleRouter::group(['prefix' =>'/admin','exceptionHandler' => \Demo\Handlers\SecondExceptionHandler::class],function() {// Both SecondExceptionHandler and FirstExceptionHandler will trigger (in that order).});SimpleRouter::group(['prefix' =>'/user','exceptionHandler' => \Demo\Handlers\SecondExceptionHandler::class,'mergeExceptionHandlers' =>false],function() {// Only SecondExceptionHandler will trigger.});});

Urls

By default all controller and resource routes will use a simplified version of their url as name.

You easily use theurl() shortcut helper function to retrieve urls for your routes or manipulate the current url.

url() will return aUrl object which will return astring when rendered, so it can be used safely in templates etc. butcontains all the useful helpers methods in theUrl class likecontains,indexOf etc.Check theUseful url tricks below.

Get the current url

It has never been easier to get and/or manipulate the current url.

The example below shows you how to get the current url:

# output: /current-urlurl();

Get by name (single route)

SimpleRouter::get('/product-view/{id}','ProductsController@show', ['as' =>'product']);# output: /product-view/22/?category=shoesurl('product', ['id' =>22], ['category' =>'shoes']);# output: /product-view/?category=shoesurl('product',null, ['category' =>'shoes']);

Get by name (controller route)

SimpleRouter::controller('/images', ImagesController::class, ['as' =>'picture']);# output: /images/view/?category=showsurl('picture@getView',null, ['category' =>'shoes']);# output: /images/view/?category=showsurl('picture','getView', ['category' =>'shoes']);# output: /images/view/url('picture','view');

Get by class

SimpleRouter::get('/product-view/{id}','ProductsController@show', ['as' =>'product']);SimpleRouter::controller('/images','ImagesController');# output: /product-view/22/?category=shoesurl('ProductsController@show', ['id' =>22], ['category' =>'shoes']);# output: /images/image/?id=22url('ImagesController@getImage',null, ['id' =>22]);

Using custom names for methods on a controller/resource route

SimpleRouter::controller('gadgets', GadgetsController::class, ['names' => ['getIphoneInfo' =>'iphone']]);url('gadgets.iphone');# output# /gadgets/iphoneinfo/

Getting REST/resource controller urls

SimpleRouter::resource('/phones', PhonesController::class);# output: /phones/url('phones');# output: /phones/url('phones.index');# output: /phones/create/url('phones.create');# output: /phones/edit/url('phones.edit');

Manipulating url

You can easily manipulate the query-strings, by adding your get param arguments.

# output: /current-url?q=carsurl(null,null, ['q' =>'cars']);

You can remove a query-string parameter by setting the value tonull.

The example below will remove any query-string parameter namedq from the url but keep all others query-string parameters:

$url =url()->removeParam('q');

For more information please check theUseful url tricks section of the documentation.

Useful url tricks

Callingurl will always return aUrl object. Upon rendered it will return astring of the relativeurl, so it's safe to use in templates etc.

However this allow us to use the useful methods on theUrl object likeindexOf andcontains or retrieve specific parts of the url like the path, querystring parameters, host etc. You can also manipulate the url like removing- or adding parameters, changing host and more.

In the example below, we check if the current url contains the/api part.

if(url()->contains('/api')) {// ... do stuff}

As mentioned earlier, you can also use theUrl object to show specific parts of the url or control what part of the url you want.

# Grab the query-string parameter id from the current-url.$id =url()->getParam('id');# Get the absolute url for the current url.$absoluteUrl =url()->getAbsoluteUrl();

For more available methods please check thePecee\Http\Url class.

Input & parameters

simple-router offers libraries and helpers that makes it easy to manage and manipulate input-parameters like$_POST,$_GET and$_FILE.

Using the Input class to manage parameters

You can use theInputHandler class to easily access and manage parameters from your request. TheInputHandler class offers extended features such as copying/moving uploaded files directly on the object, getting file-extension, mime-type etc.

Get single parameter value

input($index, $defaultValue, ...$methods);

To quickly get a value from a parameter, you can use theinput helper function.

This will automatically trim the value and ensure that it's not empty. If it's empty the$defaultValue will be returned instead.

Note:This function returns astring unless the parameters are grouped together, in that case it will return anarray of values.

Example:

This example matches both POST and GET request-methods and if name is empty the default-value "Guest" will be returned.

$name =input('name','Guest','post','get');

Get parameter object

When dealing with file-uploads it can be useful to retrieve the raw parameter object.

Search for object with default-value across multiple or specific request-methods:

The example below will return anInputItem object if the parameter was found or return the$defaultValue. If parameters are grouped, it will return an array ofInputItem objects.

$object =input()->find($index,$defaultValue =null, ...$methods);

Getting specific$_GET parameter asInputItem object:

The example below will return anInputItem object if the parameter was found or return the$defaultValue. If parameters are grouped, it will return an array ofInputItem objects.

$object =input()->get($index,$defaultValue =null);

Getting specific$_POST parameter asInputItem object:

The example below will return anInputItem object if the parameter was found or return the$defaultValue. If parameters are grouped, it will return an array ofInputItem objects.

$object =input()->post($index,$defaultValue =null);

Getting specific$_FILE parameter asInputFile object:

The example below will return anInputFile object if the parameter was found or return the$defaultValue. If parameters are grouped, it will return an array ofInputFile objects.

$object =input()->file($index,$defaultValue =null);

Managing files

/** * Loop through a collection of files uploaded from a form on the page like this * <input type="file" name="images[]" /> *//* @var $image \Pecee\Http\Input\InputFile */foreach(input()->file('images', [])as$image){if($image->getMime() ==='image/jpeg')     {$destinationFilname =sprintf('%s.%s',uniqid(),$image->getExtension());$image->move(sprintf('/uploads/%s',$destinationFilename));    }}

Get all parameters

# Get all$values =input()->all();# Only match specific keys$values =input()->all(['company_name','user_id']);

All object implements theIInputItem interface and will always contain these methods:

  • getIndex() - returns the index/key of the input.
  • setIndex() - set the index/key of the input.
  • getName() - returns a human friendly name for the input (company_name will be Company Name etc).
  • setName() - sets a human friendly name for the input (company_name will be Company Name etc).
  • getValue() - returns the value of the input.
  • setValue() - sets the value of the input.

InputFile has the same methods as above along with some other file-specific methods like:

  • getFilename - get the filename.
  • getTmpName() - get file temporary name.
  • getSize() - get file size.
  • move($destination) - move file to destination.
  • getContents() - get file content.
  • getType() - get mime-type for file.
  • getError() - get file upload error.
  • hasError() - returnsbool if an error occurred while uploading (ifgetError is not 0).
  • toArray() - returns raw array

Check if parameters exists

You can easily if multiple items exists by using theexists method. It's simular tovalue as it can be usedto filter on request-methods and supports bothstring andarray as parameter value.

Example:

if(input()->exists(['name','lastname'])) {// Do stuff}/* Similar to code above */if(input()->exists('name') &&input()->exists('lastname')) {// Do stuff}

Events

This section will help you understand how to register your own callbacks to events in the router.It will also cover the basics of event-handlers; how to use the handlers provided with the router and how to create your own custom event-handlers.

Available events

This section contains all available events that can be registered using theEventHandler.

All event callbacks will retrieve aEventArgument object as parameter. This object contains easy access to event-name, router- and request instance and any special event-arguments related to the given event. You can see what special event arguments each event returns in the list below.

NameSpecial argumentsDescription
EVENT_ALL-Fires when a event is triggered.
EVENT_INIT-Fires when router is initializing and before routes are loaded.
EVENT_LOADloadedRoutesFires when all routes has been loaded and rendered, just before the output is returned.
EVENT_ADD_ROUTEroute
isSubRoute
Fires when route is added to the router.isSubRoute is true when sub-route is rendered.
EVENT_REWRITErewriteUrl
rewriteRoute
Fires when a url-rewrite is and just before the routes are re-initialized.
EVENT_BOOTbootmanagersFires when the router is booting. This happens just before boot-managers are rendered and before any routes has been loaded.
EVENT_RENDER_BOOTMANAGERbootmanagers
bootmanager
Fires before a boot-manager is rendered.
EVENT_LOAD_ROUTESroutesFires when the router is about to load all routes.
EVENT_FIND_ROUTEnameFires whenever thefindRoute method is called within theRouter. This usually happens when the router tries to find routes that contains a certain url, usually after theEventHandler::EVENT_GET_URL event.
EVENT_GET_URLname
parameters
getParams
Fires whenever theSimpleRouter::getUrl method orurl-helper function is called and the router tries to find the route.
EVENT_MATCH_ROUTErouteFires when a route is matched and valid (correct request-type etc). and before the route is rendered.
EVENT_RENDER_ROUTErouteFires before a route is rendered.
EVENT_LOAD_EXCEPTIONSexception
exceptionHandlers
Fires when the router is loading exception-handlers.
EVENT_RENDER_EXCEPTIONexception
exceptionHandler
exceptionHandlers
Fires before the router is rendering a exception-handler.
EVENT_RENDER_MIDDLEWARESroute
middlewares
Fires before middlewares for a route is rendered.
EVENT_RENDER_CSRFcsrfVerifierFires before the CSRF-verifier is rendered.

Registering new event

To register a new event you need to create a new instance of theEventHandler object. On this object you can add as many callbacks as you like by calling theregisterEvent method.

When you've registered events, make sure to add it to the router by callingSimpleRouter::addEventHandler(). We recommend that you add your event-handlers within yourroutes.php.

Example:

usePecee\SimpleRouter\Handlers\EventHandler;usePecee\SimpleRouter\Event\EventArgument;// --- your routes goes here ---$eventHandler =newEventHandler();// Add event that fires when a route is rendered$eventHandler->register(EventHandler::EVENT_RENDER_ROUTE,function(EventArgument$argument) {// Get the route by using the special argument for this event.$route =$argument->route;// DO STUFF...    });SimpleRouter::addEventHandler($eventHandler);

Custom EventHandlers

EventHandler is the class that manages events and must inherit from theIEventHandler interface. The handler knows how to handle events for the given handler-type.

Most of the time the basic\Pecee\SimpleRouter\Handler\EventHandler class will be more than enough for most people as you simply register an event which fires when triggered.

Let's go over how to create your very own event-handler class.

Below is a basic example of a custom event-handler calledDatabaseDebugHandler. The idea of the sample below is to logs all events to the database when triggered. Hopefully it will be enough to give you an idea on how the event-handlers work.

namespaceDemo\Handlers;usePecee\SimpleRouter\Event\EventArgument;usePecee\SimpleRouter\Router;class DatabaseDebugHandlerimplements IEventHandler{/**     * Debug callback     * @var \Closure     */protected$callback;publicfunction__construct()    {$this->callback =function (EventArgument$argument) {// todo: store log in database        };    }/**     * Get events.     *     * @param string|null $name Filter events by name.     * @return array     */publicfunctiongetEvents(?string$name):array    {return [$name => [$this->callback,            ],        ];    }/**     * Fires any events registered with given event-name     *     * @param Router $router Router instance     * @param string $name Event name     * @param array ...$eventArgs Event arguments     */publicfunctionfireEvents(Router$router,string$name, ...$eventArgs):void    {$callback =$this->callback;$callback(newEventArgument($router,$eventArgs));    }/**     * Set debug callback     *     * @param \Closure $event     */publicfunctionsetCallback(\Closure$event):void    {$this->callback =$event;    }}

Advanced

Multiple route rendering

If you need multiple routes to be executed on the same url, you can enable this feature by settingSimpleRouter::enableMultiRouteRendering(true)in yourroutes.php file.

This is most commonly used in advanced cases, for example in CMS systems where multiple routes needs to be rendered.

Restrict access to IP

You can white and/or blacklist access to IP's using the build inIpRestrictAccess middleware.

Create your own custom Middleware and extend theIpRestrictAccess class.

TheIpRestrictAccess class contains two propertiesipBlacklist andipWhitelist that can be added to your middleware to change which IP's that have access to your routes.

You can use* to restrict access to a range of ips.

use \Pecee\Http\Middleware\IpRestrictAccess;class IpBlockerMiddlewareextends IpRestrictAccess {protected$ipBlacklist = ['5.5.5.5','8.8.*',    ];protected$ipWhitelist = ['8.8.2.2',    ];}

You can add the middleware to multiple routes by adding yourmiddleware to a group.

Setting custom base path

Sometimes it can be useful to add a custom base path to all of the routes added.

This can easily be done by taking advantage of theEvent Handlers support of the project.

$basePath ='/basepath';$eventHandler =newEventHandler();$eventHandler->register(EventHandler::EVENT_ADD_ROUTE,function(EventArgument$event)use($basePath) {$route =$event->route;// Skip routes added by group as these will inherit the urlif(!$event->isSubRoute) {return;}switch (true) {case$routeinstanceof ILoadableRoute:$route->prependUrl($basePath);break;case$routeinstanceof IGroupRoute:$route->prependPrefix($basePath);break;}});SimpleRouter::addEventHandler($eventHandler);

In the example shown above, we create a newEVENT_ADD_ROUTE event that triggers, when a new route is added.We skip all subroutes as these will inherit the url from their parent. Then, if the route is a group, we change the prefix
otherwise we change the url.

Url rewriting

Changing current route

Sometimes it can be useful to manipulate the route about to be loaded.simple-php-router allows you to easily manipulate and change the routes which are about to be rendered.All information about the current route is stored in the\Pecee\SimpleRouter\Router instance'sloadedRoute property.

For easy access you can use the shortcut helper functionrequest() instead of calling the class directly\Pecee\SimpleRouter\SimpleRouter::router().

request()->setRewriteCallback('Example\MyCustomClass@hello');// -- or you can rewrite by url --request()->setRewriteUrl('/my-rewrite-url');

Bootmanager: loading routes dynamically

Sometimes it can be necessary to keep urls stored in the database, file or similar. In this example, we want the url/my-cat-is-beatiful to load the route/article/view/1 which the router knows, because it's defined in theroutes.php file.

To interfere with the router, we create a class that implements theIRouterBootManager interface. This class will be loaded before any other rules inroutes.php and allow us to "change" the current route, if any of our criteria are fulfilled (like coming from the url/my-cat-is-beatiful).

usePecee\Http\Request;usePecee\SimpleRouter\IRouterBootManager;usePecee\SimpleRouter\Router;class CustomRouterRules implement IRouterBootManager {/**     * Called when router is booting and before the routes is loaded.     *     * @param \Pecee\SimpleRouter\Router $router     * @param \Pecee\Http\Request $request     */publicfunctionboot(\Pecee\SimpleRouter\Router$router,\Pecee\Http\Request$request):void    {$rewriteRules = ['/my-cat-is-beatiful' =>'/article/view/1','/horses-are-great'   =>'/article/view/2',        ];foreach($rewriteRulesas$url =>$rule) {// If the current url matches the rewrite url, we use our custom routeif($request->getUrl()->contains($url)) {$request->setRewriteUrl($rule);            }        }    }}

The above should be pretty self-explanatory and can easily be changed to loop through urls store in the database, file or cache.

What happens is that if the current route matches the route defined in the index of our$rewriteRules array, we set the route to the array value instead.

By doing this the route will now load the url/article/view/1 instead of/my-cat-is-beatiful.

The last thing we need to do, is to add our custom boot-manager to theroutes.php file. You can create as many bootmanagers as you like and easily add them in yourroutes.php file.

SimpleRouter::addBootManager(newCustomRouterRules());

Adding routes manually

TheSimpleRouter class referenced in the previous example, is just a simple helper class that knows how to communicate with theRouter class.If you are up for a challenge, want the full control or simply just want to create your ownRouter helper class, this example is for you.

use \Pecee\SimpleRouter\Router;use \Pecee\SimpleRouter\Route\RouteUrl;/* Create new Router instance */$router =newRouter();$route =newRouteUrl('/answer/1',function() {die('this callback will match /answer/1');});$route->addMiddleware(\Demo\Middlewares\AuthMiddleware::class);$route->setNamespace('\Demo\Controllers');$route->setPrefix('v1');/* Add the route to the router */$router->addRoute($route);

Custom class loader

You can easily extend simple-router to support custom injection frameworks like php-di by taking advantage of the ability to add your custom class-loader.

Class-loaders must inherit theIClassLoader interface.

Example:

class MyCustomClassLoaderimplements IClassLoader{/**     * Load class     *     * @param string $class     * @return object     * @throws NotFoundHttpException     */publicfunctionloadClass(string$class)    {if (\class_exists($class) ===false) {thrownewNotFoundHttpException(sprintf('Class "%s" does not exist',$class),404);        }returnnew$class();    }/**     * Called when loading class method     * @param object $class     * @param string $method     * @param array $parameters     * @return object     */publicfunctionloadClassMethod($class,string$method,array$parameters)    {returncall_user_func_array([$class,$method],array_values($parameters));    }/**     * Load closure     *     * @param Callable $closure     * @param array $parameters     * @return mixed     */publicfunctionloadClosure(Callable$closure,array$parameters)    {return\call_user_func_array($closure,array_values($parameters));    }}

Next, we need to configure ourroutes.php so the router uses ourMyCustomClassLoader class for loading classes. This can be done by adding the following line to yourroutes.php file.

SimpleRouter::setCustomClassLoader(newMyCustomClassLoader());

Integrating with php-di

php-di support was discontinued by version 4.3, however you can easily add it again by creating your own class-loader like the example below:

usePecee\SimpleRouter\ClassLoader\IClassLoader;usePecee\SimpleRouter\Exceptions\ClassNotFoundHttpException;class MyCustomClassLoaderimplements IClassLoader{protected$container;publicfunction__construct()    {// Create our new php-di container$this->container = (new \DI\ContainerBuilder())                    ->useAutowiring(true)                    ->build();    }/**     * Load class     *     * @param string $class     * @return object     * @throws ClassNotFoundHttpException     */publicfunctionloadClass(string$class)    {if ($this->container->has($class) ===false) {thrownewClassNotFoundHttpException($class,null,sprintf('Class "%s" does not exist',$class),404,null);        }return$this->container->get($class);    }/**     * Called when loading class method     * @param object $class     * @param string $method     * @param array $parameters     * @return string     */publicfunctionloadClassMethod($class,string$method,array$parameters)    {return (string)$this->container->call([$class,$method],$parameters);    }/**     * Load closure     *     * @param Callable $closure     * @param array $parameters     * @return string     */publicfunctionloadClosure(callable$closure,array$parameters)    {return (string)$this->container->call($closure,$parameters);    }}

Parameters

This section contains advanced tips & tricks on extending the usage for parameters.

Extending

This is a simple example of an integration into a framework.

The framework has it's ownRouter class which inherits from theSimpleRouter class. This allows the framework to add custom functionality like loading a customroutes.php file or add debugging information etc.

namespaceDemo;usePecee\SimpleRouter\SimpleRouter;class Routerextends SimpleRouter {publicstaticfunctionstart() {// change this to whatever makes sense in your projectrequire_once'routes.php';// change default namespace for all routesparent::setDefaultNamespace('\Demo\Controllers');// Do initial stuffparent::start();    }}

Help and support

This section will go into details on how to debug the router and answer some of the commonly asked questions- and issues.

Common issues and fixes

This section will go over common issues and how to resolve them.

Parameters won't match or route not working with special characters

Often people experience this issue when one or more parameters contains special characters. The router uses a sparse regular-expression that matches letters from a-z along with numbers when matching parameters, to improve performance.

All other characters has to be defined via thedefaultParameterRegex option on your route.

You can read more about adding your own custom regular expression for matching parameters byclicking here.

Multiple routes matches? Which one has the priority?

The router will match routes in the order they're added and will render multiple routes, if they match.

If you want the router to stop when a route is matched, you simply return a value in your callback or stop the execution manually (usingresponse()->json() etc.) or simply by returning a result.

Any returned objects that implements the__toString() magic method will also prevent other routes from being rendered.

If you want the router only to execute one route per request, you candisabling multiple route rendering.

Using the router on sub-paths

Please refer toSetting custom base path part of the documentation.

Debugging

This section will show you how to write unit-tests for the router, view useful debugging information and answer some of the frequently asked questions.

It will also covers how to report any issue you might encounter.

Creating unit-tests

The easiest and fastest way to debug any issues with the router, is to create a unit-test that represents the issue you are experiencing.

Unit-tests use a specialTestRouter class, which simulates a request-method and requested url of a browser.

TheTestRouter class can return the output directly or render a route silently.

publicfunctiontestUnicodeCharacters(){// Add route containing two optional paramters with special spanish characters like "í".    TestRouter::get('/cursos/listado/{listado?}/{category?}','DummyController@method1', ['defaultParameterRegex' =>'[\w\p{L}\s-]+']);// Start the routing and simulate the url "/cursos/listado/especialidad/cirugía local".    TestRouter::debugNoReset('/cursos/listado/especialidad/cirugía local','GET');// Verify that the url for the loaded route matches the expected route.$this->assertEquals('/cursos/listado/{listado?}/{category?}/', TestRouter::router()->getRequest()->getLoadedRoute()->getUrl());// Start the routing and simulate the url "/test/Dermatología" using "GET" as request-method.    TestRouter::debugNoReset('/test/Dermatología','GET');// Another route containing one parameter with special spanish characters like "í".    TestRouter::get('/test/{param}','DummyController@method1', ['defaultParameterRegex' =>'[\w\p{L}\s-\í]+']);// Get all parameters parsed by the loaded route.$parameters = TestRouter::request()->getLoadedRoute()->getParameters();// Check that the parameter named "param" matches the exspected value.$this->assertEquals('Dermatología',$parameters['param']);// Add route testing danish special characters like "ø".    TestRouter::get('/category/økse','DummyController@method1', ['defaultParameterRegex' =>'[\w\ø]+']);// Start the routing and simulate the url "/kategory/økse" using "GET" as request-method.    TestRouter::debugNoReset('/category/økse','GET');// Validate that the URL of the loaded-route matches the expected url.$this->assertEquals('/category/økse/', TestRouter::router()->getRequest()->getLoadedRoute()->getUrl());// Reset the router, so other tests wont inherit settings or the routes we've added.    TestRouter::router()->reset();}

Using the TestRouter helper

Depending on your test, you can use the methods below when rendering routes in your unit-tests.

MethodDescription
TestRouter::debug($url, $method)Will render the route without returning anything. Exceptions will be thrown and the router will be reset automatically.
TestRouter::debugOutput($url, $method)Will render the route and return any value that the route might output. Manual reset required by callingTestRouter::router()->reset().
TestRouter::debugNoReset($url, $method);Will render the route without resetting the router. Useful if you need to get loaded route, parameters etc. from the router. Manual reset required by callingTestRouter::router()->reset().

Debug information

The library can output debug-information, which contains information like loaded routes, the parsed request-url etc. It also contains info which are important when reporting a new issue like PHP-version, library version, server-variables, router debug log etc.

You can activate the debug-information by calling the alternative start-method.

The example below will start the routing an return array with debugging-information

Example:

$debugInfo = SimpleRouter::startDebug();echosprintf('<pre>%s</pre>',var_export($debugInfo));exit;

The example above will provide you with an output containing:

KeyDescription
urlThe parsed request-uri. This url should match the url in the browser.
methodThe browsers request method (example:GET,POST,PUT,PATCH,DELETE etc).
hostThe website host (example:domain.com).
loaded_routesList of all the routes that matched theurl and that has been rendered/loaded.
all_routesAll available routes
boot_managersAll available BootManagers
csrf_verifierCsrfVerifier class
logList of debug messages/log from the router.
router_outputThe rendered callback output from the router.
library_versionThe version of simple-php-router you are using.
php_versionThe version of PHP you are using.
server_paramsList of all$_SERVER variables/headers.

Benchmark and logging

You can activate benchmark debugging/logging by callingsetDebugEnabled method on theRouter instance.

You have to enable debugging BEFORE starting the routing.

Example:

SimpleRouter::router()->setDebugEnabled(true);SimpleRouter::start();

When the routing is complete, you can get the debug-log by calling thegetDebugLog() on theRouter instance. This will return anarray of log-messages each containing execution time, trace info and debug-message.

Example:

$messages = SimpleRouter::router()->getDebugLog();

Reporting a new issue

Before reporting your issue, make sure that the issue you are experiencing aren't already answered in theCommon errors section or by searching theclosed issues page on GitHub.

To avoid confusion and to help you resolve your issue as quickly as possible, you should provide a detailed explanation of the problem you are experiencing.

Procedure for reporting a new issue

  1. Go tothis page to create a new issue.
  2. Add a title that describes your problems in as few words as possible.
  3. Copy and paste the template below in the description of your issue and replace each step with your own information. If the step is not relevant for your issue you can delete it.

Issue template

Copy and paste the template below into the description of your new issue and replace it with your own information.

You can check theDebug information section to see how to generate the debug-info.

### DescriptionThe library fails to render the route `/user/æsel` which contains one parameter using a custom regular expression for matching special foreign characters. Routes without special characters like `/user/tom` renders correctly.### Steps to reproduce the error1. Add the following route:```phpSimpleRouter::get('/user/{name}', 'UserController@show')->where(['name' => '[\w]+']);```2. Navigate to `/user/æsel` in browser.3. `NotFoundHttpException` is thrown by library.### Route and/or callback for failing route*Route:*```phpSimpleRouter::get('/user/{name}', 'UserController@show')->where(['name' => '[\w]+']);```*Callback:*```phppublic function show($username) {    return sprintf('Username is: %s', $username);}```### Debug info```php[PASTE YOUR DEBUG-INFO HERE]```

Remember that a more detailed issue- description and debug-info might suck to write, but it will help others understand- and resolve your issue without asking for the information.

Note: please be as detailed as possible in the description when creating a new issue. This will help others to more easily understand- and solve your issue. Providing the necessary steps to reproduce the error within your description, adding useful debugging info etc. will help others quickly resolve the issue you are reporting.

Feedback and development

If the library is missing a feature that you need in your project or if you have feedback, we'd love to hear from you.Feel free to leave us feedback bycreating a new issue.

Experiencing an issue?

Please refer to ourHelp and support section in the documentation before reporting a new issue.

Contribution development guidelines

  • Please try to follow the PSR-2 codestyle guidelines.

  • Please create your pull requests to the development base that matches the version number you want to change.For example when pushing changes to version 3, the pull request should use thev3-development base/branch.

  • Create detailed descriptions for your commits, as these will be used in the changelog for new releases.

  • When changing existing functionality, please ensure that the unit-tests working.

  • When adding new stuff, please remember to add new unit-tests for the functionality.


Credits

Sites

This is some sites that uses the simple-router project in production.

License

The MIT License (MIT)

Copyright (c) 2016 Simon Sessingø / simple-php-router

Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, 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 THESOFTWARE.

About

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.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp