Code Monkey home page Code Monkey logo

router's Introduction

Warning: This code is very old and has not been maintained for a long time.
You probably do not want to use this anymore. Consider looking at https://github.com/auraphp/Aura.Router.

A simple url router

This file contains a small url router, which is intended to be used in very small projects, where no real MVC framework fits the needs of the project.

Comments

To create a route, you just have to call the “Router::add” function and provide the intended url and the name of the function which is supposed to be called if the route matches.

The function name can contain a regular expression. Every matching argument, that can be extracted will be stored in the $args array.

Every function, that is supposed to be the endpoint of an url has to accept a single variable, which will contain the array of extracted arguments.

If you want to use class methods as your endpoints, just add the class’s name in front of the method’s name and add a dot after the class’s name.

For the bar method of this class:

class Foo {
  function bar($args) {...}
}

the endpoint has to be “Foo.bar”. See example usage in the example file (index.php).

The Router has only static methods, because that makes the api super nice (at least IMHO).

Usage:

require_once('Router.php');
Router::add('/example', 'example_function');
function example_function($args) {
  echo "Hello!<br />";
  print_r($args);
}
Router::run()

router's People

Contributors

florian-zz avatar florianh avatar harikt avatar siyuanw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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