All Projects → apsdehal → Link

apsdehal / Link

Licence: mit
A PHP router that helps you create webapps and APIs effortlessly

Labels

Projects that are alternatives of or similar to Link

Shgf
Simple HTTP golang framework
Stars: ✭ 13 (-95.39%)
Mutual labels:  api, router
Routegen
Define your API and SPA routes in one place. Use them anywhere. Only 1.3kb.
Stars: ✭ 86 (-69.5%)
Mutual labels:  api, router
Altair
Lightweight and Robust API Gateway written in Go
Stars: ✭ 34 (-87.94%)
Mutual labels:  api, router
Diet
A tiny, fast and modular node.js web framework. Good for making fast & scalable apps and apis.
Stars: ✭ 394 (+39.72%)
Mutual labels:  api, router
Pure Http
✨ The simple web framework for Node.js with zero dependencies.
Stars: ✭ 139 (-50.71%)
Mutual labels:  api, router
Rapid.js
An ORM-like Interface and a Router For Your API Requests
Stars: ✭ 700 (+148.23%)
Mutual labels:  api, router
Flowa
🔥Service level control flow for Node.js
Stars: ✭ 66 (-76.6%)
Mutual labels:  api, router
Gearbox
Gearbox ⚙️ is a web framework written in Go with a focus on high performance
Stars: ✭ 455 (+61.35%)
Mutual labels:  api, router
Foxify
The fast, easy to use & typescript ready web framework for Node.js
Stars: ✭ 138 (-51.06%)
Mutual labels:  api, router
Chi
lightweight, idiomatic and composable router for building Go HTTP services
Stars: ✭ 10,581 (+3652.13%)
Mutual labels:  api, router
Copper
Copper is a set of Go packages that help you build backend APIs quickly and with less boilerplate.
Stars: ✭ 35 (-87.59%)
Mutual labels:  api, router
Falco
A functional-first toolkit for building brilliant ASP.NET Core applications using F#.
Stars: ✭ 214 (-24.11%)
Mutual labels:  api, router
Go Tgbot
Golang telegram bot API wrapper, session-based router and middleware
Stars: ✭ 90 (-68.09%)
Mutual labels:  api, router
Component
🔥🔥🔥A powerful componentized framework.一个强大、100% 兼容、支持 AndroidX、支持 Kotlin并且灵活的组件化框架
Stars: ✭ 2,434 (+763.12%)
Mutual labels:  api, router
Clevergo
👅 CleverGo is a lightweight, feature rich and high performance HTTP router for Go.
Stars: ✭ 246 (-12.77%)
Mutual labels:  api, router
Hubspot Php
HubSpot PHP API Client
Stars: ✭ 273 (-3.19%)
Mutual labels:  api
Avenging
MVP pattern example on Android: no Dagger or RxJava example
Stars: ✭ 279 (-1.06%)
Mutual labels:  api
Chn Eolinker Ams Lite 4.0 For Java
中国最大的API接口管理平台,3.x开源发行版,支持多国语言[英语、简体中文、繁体中文]
Stars: ✭ 275 (-2.48%)
Mutual labels:  api
Tools For Instagram
Automation scripts for Instagram
Stars: ✭ 274 (-2.84%)
Mutual labels:  api
Simple Php Router
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.
Stars: ✭ 279 (-1.06%)
Mutual labels:  router

Link

A minimal router for your php webapps and APIs that effortlessly links all your project. Its fast and to the point.

Features

  • RESTful routing
  • Wildcards for your limitless creativity
  • Named routes to help you create links easily
  • Self documented, speaks its own legacy
  • Before and after routes function support
  • Tested with PHP >5.3

HHVM Version

HHVM version of Link can be found at https://github.com/bmbsqd/Link-Hack . Thanks to Andy Hawkins for creating it.

Installation

Composer

For install from composer just add the following line to your project's composer.json file

	"require" : {
    	"link/link" : "dev-master"
    }

Then run php composer.phar install

Manually

Run git clone https://github.com/apsdehal/Link.git in your project's home directory and include it using

	require("Link/src/Link.php");

Basics

Simple Routing

Routing is too simple with Link, following example supports it:

<?php

function routeMe(){
	echo 'I am routed';
}

Link::all( array(
	'/' => 'routeMe'
));

Named Routing

In Link routes can be named and then further used generatings links in a simple and elegant way.

<?php

function nameMe(){
	echo 'I am named';
}

Link::all( array(
	'/named' => ['nameMe', 'Its my name']
));

Names to routes must be given as second argument in array while the first being the route handler.

Usage

These named routes can be used in creating in hassle free links.

	<a href="<?php echo Link::route('Its my name') ?>">Go to named route</a>

Routing with classes

Link can handle classes as Route handler easily, but remember non-static class will be handled RESTfully.

<?php

$routes = array(
	'/' => 'IndexController::getMeHome', //Static function
    '/home' => 'HomeController', //RESTful class
    '/office' => 'OfficeController'
);

Link::all($routes)

RESTful routing

RESTful routing is a breeze for Link.

<?php

class HomeController
{
	
    function get(){
    	echo 'You have got to home :)';
    }
    
    function post(){
    	echo 'You have posted to home';
    }
    
    function put(){
    	echo 'You have put to home';
    }
    
    function delete(){
    	echo 'You have deleted the home :(';
    }
}

Link::all( array (
	'/' => ['HomeController', 'HomeRoute']
));

WildCards

Link supports numbers, string and alphanumeric wildcards which can be used as {i} {s} {a} respectively and of course it can render regex also. Example will clear away your doubts

$routes = array(
	'/' => 'IndexController',
    '/{i}' => 'IndexController',
    //Parameter in place of {i} will be passed to IndexController
	'/posts/{a}/{i}/{s}' => 'PostsController'
);

Link::all($routes);

Supplimentary Handlers

Universal Extra Handlers

Through Link, universal before and after handlers can be added, such that these are executed always before any route is routed. This can be done as follows:

<?php
function universalBeforeHandler( $id ) {
    echo 'Hello I occured before with ' . $id . '\n';
}

function universalAfterHandler( $id ) {
    if( $id )
        echo 'Hello I occured after with ' . $id;
    else
        echo 'I simply occured after';
}

function main(){
    echo 'I simply occured\n'
}

Link::before( 'universalBeforeHandler', ['12'] ); //If you want to pass parameters to them, pass them as arrays
Link::before( 'universalBeforeHandler'); //else don't even pass them

Link::all( array(
    '/' => 'main'
    ))

Now go to '/' in your browser to find:

Hello I occured before with 12

I simply occured

I simply occured after.

Single Route

You can add a before (middle) handler to a specific route, just pass the before handler to routes array as third parameters. The wildcards extracted from route will be passed to to before handler and if it return some array, this array will be passed further to main handler but if not the original extracted wildcards would be passed away. Make sure you return an array from before handler.

<?php 
function beforeHandler( $name ) {
    return [ $name . ' Link' ];
}

function mainHandler( $name ){
    echo $name;
}

Link::all(array(
    '/{s}' => ['mainHandler', 'Main', 'beforHandler']
    ));

Go to '/aps' in browser, you will get aps Link.

Passing Parameters to Named Routes

You can pass parameters to named routes if the have wildcards in the route path, this will thus generate dynamic links through a single named route.

<?php

function nameMe( $i, $s ){
	echo 'I am named and I have been passed ' . $i . $s ;
}

Link::all( array(
	'/named/{i}/{s}' => ['nameMe', 'Its my name']
));

Now generate a link through Link

echo Link::route( 'Its my name', array(1, 'Me') );

This in turn will generate YOUR_DOMAIN/named/1/Me.

404 Errors

You should probably add a 404 handler to your routes array, rest Link will take care of handling routes that are not found. In case, Link doesn't find a 404 route defined, it will just send a 404 header.

Server Configuration

Apache

You should add the following code snippet in your Apache HTTP server VHost configuration or .htaccess file.

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond $1 !^(index\.php)
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

Alternatively, in a version of Apache greater than 2.2.15, then you can use this:

FallbackResource /index.php

Notes

If you are planning to use non-Restful method and non-static classes, then use them as follows:

class HelloHandler 
{
    public function home(){
        echo 'Hello home';
    }
}

$helloObject = new HelloHandler();

Link::all( array(
    '/' => array( $helloObejct, 'home' )
    ))

So you need to pass such functions as array( $object, 'functionName' )

Contributions

Thanks to all people below for contributing to Link.

License

Link is available under MIT license, so feel free to contribute and modify it as you like. Free software, Yeah!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].