PHPackages                             button/button - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. button/button

ActiveLibrary[Framework](/categories/framework)

button/button
=============

Fast and lightweight router for PHP

08[7 issues](https://github.com/rogelio-meza-t/button/issues)PHP

Since Sep 29Pushed 11y ago1 watchersCompare

[ Source](https://github.com/rogelio-meza-t/button)[ Packagist](https://packagist.org/packages/button/button)[ RSS](/packages/button-button/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependenciesVersions (1)Used By (0)

Button
======

[](#button)

A fast and lightweight PHP 5.4+ nano router. Build API's and RESTful applications with ease. With Button, you can get one or more JSON responses on the same request.

The hello world!
----------------

[](#the-hello-world)

```
$btn = new Button();
$btn->hook('GET', '/say/hello/:name', function($name){
	return array("greeting" => "Hello ". $name ."!");
});
```

In the previous example, when you access to `/say/hello/john`, Button will run the anonymous function and will return the next JSON object:

```
[
    {"greeting" : "Hello john!"}
]
```

1. [Requirements](#requirements)
2. [Installation](#instalation)
    1. [Composer install](#composer-install)
    2. [Clone Repo](#clone-repository)
    3. [Download Copy](#download-a-copy)
3. [Usage](#usage)
    1. [Parameters](#parameters)
4. [Features](#features)
    1. [The routes](#the-routes)
        1. [Simple routing](#simple-routing)
        2. [Routing parameters](#routing-parameters)
        3. [Optional parameters](#optional-parameters)
    2. [Callbacks](#callbacks)
    3. [Anonymous functions](#anonymous-functions)
    4. [Normal functions](#normal-functions)
    5. [Array of functions](#array-of-functions)

Requirements
------------

[](#requirements)

Button requires PHP 5.4+ to run

Installation
------------

[](#installation)

There are a few ways to install Button in your project.

#### Composer install

[](#composer-install)

Create a `composer.json` file in your project root directory

```
{
    "require": {
        "button/button": "dev-master"
    }
}
```

and make the command `composer install`. Make sure to have installed [composer](https://getcomposer.org)

#### Clone repository

[](#clone-repository)

If you don't use composer, you can clone the repository by doing

```
cd to-your-project-lib-directory
git clone https://github.com/rogelio-meza-t/button.git button

```

#### Download a copy

[](#download-a-copy)

Download a copy of [Button.php](../master/button.php) to your project.

Usage
-----

[](#usage)

```
// first create the Button object
$btn = new Button();

//then, make the hook
$btn->hook($http_method, $pattern, $callback [, $ajax]);
```

### Parameters

[](#parameters)

 $http\_method A string indicating HTTP method. Possible options are GET, POST, PUT and DELETE (lower case is allowed). $pattern The route that matches the requests. Learn more about [patterns](#the-routes). $callback Function or array of functions to be run. Learn more about [callbacks](#callbacks). $ajax Optional. **true** runs the callback when the request is an ajax request. **false** runs when the request is not an ajax call. Otherwise, if it is skipped or a non-boolean value is passed, the *callback* runs always. Features
--------

[](#features)

### The routes

[](#the-routes)

With Button you can map an URI to callback one function or an array of functions. You can define the same route for different HTTP method and Button will execute only the respective callback.

#### Simple routing

[](#simple-routing)

The basic way to make a route is write a simple URI

```
$btn = new Button();
$btn->hook('GET', '/say/hello/', function(){
	return "Hello world!";
});
```

#### Route parameters

[](#route-parameters)

To create a parameter you need to append a colon to the parameter name in the route. You can define as many parameters as you need.

```
$btn = new Button();
$btn->hook('GET', '/say/hello/:name/:surname', function($name, $surname){
  return "Hello ". $name . " " . $surname ;
});
```

The values are extracted from the URI and are passed as arguments to the function in order of occurrence. Isn't important the parameter name in the function, if the route is defined as `/foo/:some/:thing` and the function is defined as `function($a, $b)` the value of `:some` will be passed to `$a` and `:thing` to `$b`.

#### Optional parameters

[](#optional-parameters)

The optional parameters are defined within parentheses. If you use one or more optional parameters you can declare the parameter in the function with a value by default or not.

```
$btn = new Button();
$btn->hook('GET', '/hello(/:name(/:surname))', function($name='John', $surname='Doe'){
  return "Hello ". $name . " " . $surname ;
});
```

This route can accepts requests for:

```
/hello
/hello/Jane
/hello/Jane/Roe

```

### Callbacks

[](#callbacks)

Callbacks are the way to run code when you define a route. You can invoke one or more functions and get multiple JSON responses at the same time.

#### Anonymous functions

[](#anonymous-functions)

The basic way is define the `$callback` parameter as an anonymous function.

```
$btn = new Button();
$btn->hook('GET', '/foo/bar', function(){
  return "Hello World!";
});
```

Even if you have defined an object, you can use it inside the anonymous function using a closure.

```
$btn = new Button();
$foo = new FooClass();
$btn->hook('GET', '/foo/bar/:param', function($param) use ($foo){
  return $foo->someMethod($param);
});
```

Of course, you can store the anonymous function in a variable and use this later as the `$callback` parameter.

```
$callback_function = function($param){
    //some code ...
    return $param;
};
$btn = new Button();
$btn->hook('GET', '/foo/bar/:param', $callback_function);
```

#### Normal functions

[](#normal-functions)

This doesn't need much explanations.

```
function callback_function($param){
    //some code ...
    return $param;
}
$btn = new Button();
$btn->hook('GET', '/foo/bar/:param', 'callback_function');
```

Or you can invoke a static method from some class:

```
class SomeClass{
    public static function callback_function($param){
        //some code ...
        return $param;
    }
}
$btn = new Button();
$btn->hook('GET', '/foo/bar/:param', 'SomeClass::callback_function');
```

#### Array of functions

[](#array-of-functions)

When you need to get more than one response, you can pass an array of functions to the `$callback` parameter. The result is a JSON array the same size as the `$callback` array.

```
function a($param){
  //some code ...
  $return array("name" => $param);
}
function b($param){
  //some code ...
  $return array("user" => $param);
}

$btn = new Button();
$btn->hook('GET', '/foo/bar/:param', ['a', 'b']);
```

The previous example returns the next JSON object

```
[
    {"name" : "rogelio"},
    {"user" : "rogelio"}
]
```

**Hint**: You can mix anonymous functions, normal functions or static methods inside the array.

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/78dc961c77af36944d7cc58c9cf59b4d53e88d3f26e2fc2c2a4c885da701c854?d=identicon)[rogelio-meza-t](/maintainers/rogelio-meza-t)

---

Top Contributors

[![rogelio-meza-t](https://avatars.githubusercontent.com/u/1841137?v=4)](https://github.com/rogelio-meza-t "rogelio-meza-t (14 commits)")

### Embed Badge

![Health badge](/badges/button-button/health.svg)

```
[![Health](https://phpackages.com/badges/button-button/health.svg)](https://phpackages.com/packages/button-button)
```

###  Alternatives

[laravel/telescope

An elegant debug assistant for the Laravel framework.

5.2k67.8M192](/packages/laravel-telescope)[spiral/roadrunner

RoadRunner: High-performance PHP application server and process manager written in Go and powered with plugins

8.4k12.2M84](/packages/spiral-roadrunner)[nolimits4web/swiper

Most modern mobile touch slider and framework with hardware accelerated transitions

41.8k177.2k1](/packages/nolimits4web-swiper)[laravel/dusk

Laravel Dusk provides simple end-to-end testing and browser automation.

1.9k36.7M259](/packages/laravel-dusk)[laravel/prompts

Add beautiful and user-friendly forms to your command-line applications.

708181.8M596](/packages/laravel-prompts)[cakephp/chronos

A simple API extension for DateTime.

1.4k47.7M121](/packages/cakephp-chronos)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
