Git Product home page Git Product logo

tonik's Introduction

TonikRouter Build Status

Tonik is a simple PHP router with support for optional parameters, patterns and url resolving by route name.

Basics

Using Tonik is simple.

  • Instantiate the Tonik\Router object,
  • Call the run method on it.

But Tonik\Router has two dependencies, which you should provide. First - a routes collection object and second - a dispatcher object.

Here's an example:

<?php

$url = $_SERVER['REQUEST_URI']; // The requested url.

$method = $_SERVER['REQUEST_METHOD']; // The HTTP request method (GET, POST, etc...).

$routes = [
    // HTTP method, path, handler, route name.
	['GET', '/', 'HomeController@index', 'homepage'],
    ['GET', '/about', 'PagesController@about', 'about'],
];

$routesCollection = new Tonik\RoutesCollection($routes);
$dispatcher = new Tonik\Dispatcher($url, $method);
$router = new Router($routesCollection, $dispatcher);

$match = $router->run();

Now, the matched route and it's specifics should be available to you in the $match variable.

The match

The return value of the Tonik\Router's run() method is a simple array, containing usefull information for the matched route.

Let's say the requested url /about/Borislav matches the route

['GET', '/about/{name}', 'PagesController@about', 'about']

The match will be represented by a simple array like this:

$match = [
	'handler' => 'PagesController@about',
    'params' => [
    	'name' => 'Borislav',
    ],
    'routeName' => 'about',
];

Route tricks

These were the basics. But tonik provides some nice route tricks you can make. Let's see.

['GET', '/about/{name?}', 'PagesController@about', 'about'] # {name?} is an optional parameter.
['GET', '/about/*', 'PagesController@about', 'about'] # Matches every route, starting with /about.
['GET', '/about/( word:(s) )', 'PagesController@about', 'about'] # The 'word' parameter must be a string.

The format (something:(constraint)) is a way to restrict route parameters. Say you want the 'something' parameter to be a string - you declare it like this - (something:(s)) or ( something(s) ). Other built in constraint is 'n' (for numbers).

If you need something custom, you can pass a regular expression to the brackets. Like this:

['POST', 'foo/{slug:( ([a-z]+)-([a-z]+)-([012]+) )?}', 'PatternsController@complex', 'complex'],

This will match only POST requests. The slug parameter should be in the form "something a-z"-"something a-z again"-"something 0-9". You should notice it's also optional. So with or without it this route will be matched. For example /foo/bar-baz-1 is a valid route. Also just /foo. But /foo/bar is invalid route, because it doesn't match the regex.

That's it.

Well, that's pretty much all you would need for a simple app. If you want to dive deeper, take a look at the tests folder to see how the router works in greater details.

Happy routing, all.

tonik's People

Contributors

brslv avatar

Watchers

James Cloos 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.