Code Monkey home page Code Monkey logo

web-request-routing's Introduction

Web request routing

A standalone lightweight web request routing system.

Install

Using composer

composer require DavidFricker/Router

Non composer

This package is PSR-4 compliant so creating your own autoloader should easy if you do not wish to use composer.

Usage example

Before any requests can be routed they must be defined and stored within a RouteContainer object.

use DavidFricker\Router\Capsule\RouteContainer;

$RouteContainer = DavidFricker\Router\Capsule\RouteContainer::init();

Create a static route

The following line will create a static route. Static routes are those without any dynamic components that require parsing. The first argument is the URI the client should navigate to. The second argument is the HTTP method they should use to request the. The third argument can be an anonymous function or a string that identifies a method of a given class.

Directing to a class member

// the method, in this instance `getPage`, should accept a $Request object as its single parameter
$RouteContainer->set('/dashboard', RouteContainer::HTTP_METHOD_GET, 'getPage@Namespace\Vendor\Package\Controller\Dashboard');

Directing to an anonymous function

$RouteContainer->set('/dashboard', RouteContainer::HTTP_METHOD_GET, function($Request){
  echo 'Welcome to your dashboard';
});

Create a dynamic route

Dynamic routes are defined in the same fashion as their static counterparts. However, in the defined route any parts that are surrounded by curly braces will be parsed and passed to the called function through the Request object. An example of format and usage can be seen below.

$RouteContainer->set('/confirm/{token}/complete', RouteContainer::HTTP_METHOD_GET, function($Request){
  echo 'Token: ' . $Request->getParsedUrlParameters('token');
});

Request class reference

getMethod

Getter method for the HTTP request method returns string HTTP request method

getUrlElements

Fetch a single part of the path or all in the form of an array. To fetch all parts in an array supply no arguments. To fetch a single part supply the numeric index of the expected position of the part starting from zero. The path elements are split around '/'.

For example, the Request object for the URI /path/to/page would return the string 'to' when calling getUrlElements(1).

getParameters

Fetch parameters sent with the request. This method acts similarly to the $_REQUEST array. To fetch all parameters in an array supply no arguments. To fetch a single value supply the parameter key (in the same way you would fetch a value from the $_POST or $_GET arrays).

getParsedUrlParameters

Fetch parsed elements of the request path. Returns the parsed value from the request URI using the request route. For example, if the template route path were 'path/to/article/{article_id}', the requested URI were 'path/to/article/4455', then the array [article_id => 4455] would be passed to this method. This would then be accessible by the following method call getParsedUrlParameters('article_id')

Bugs

Bug reports welcome, please included details to reproduce the bugs. Commits are also very welcome for bug fixes, features, or refactoring.

License

Released under the MIT license.

web-request-routing's People

Contributors

davidfricker avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.