PHPackages                             arjunacoding/bedigas - 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. arjunacoding/bedigas

ActiveLibrary[Framework](/categories/framework)

arjunacoding/bedigas
====================

Bedigas is a fast, simple, extensible framework for PHP. Bedigas enables you to quickly and easily build RESTful web applications.

06PHP

Since Nov 24Pushed 6y ago1 watchersCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

What is Bedigas?
================

[](#what-is-bedigas)

Bedigas is a fast, simple, extensible framework for PHP. Bedigas enables you to quickly and easily build RESTful web applications.

```
require 'bedigas/Bedigas.php';

Bedigas::route('/', function(){
    echo 'hello world!';
});

Bedigas::start();
```

[Learn more](http://bedigasphp.com/learn)

Requirements
============

[](#requirements)

Bedigas requires `PHP 5.3` or greater.

License
=======

[](#license)

Bedigas is released under the [MIT](http://bedigasphp.com/license) license.

Installation
============

[](#installation)

1\. Download the files.

If you're using [Composer](https://getcomposer.org/), you can run the following command:

```
composer require arjunacoding/bedigas

```

OR you can [download](https://github.com/arjunacoding/bedigas/archive/master.zip) them directly and extract them to your web directory.

2\. Configure your webserver.

For *Apache*, edit your `.htaccess` file with the following:

```
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]

```

**Note**: If you need to use bedigas in a subdirectory add the line `RewriteBase /subdir/` just after `RewriteEngine On`.

For *Nginx*, add the following to your server declaration:

```
server {
    location / {
        try_files $uri $uri/ /index.php;
    }
}

```

3\. Create your `index.php` file.

First include the framework.

```
require 'bedigas/Bedigas.php';
```

If you're using Composer, run the autoloader instead.

```
require 'vendor/autoload.php';
```

Then define a route and assign a function to handle the request.

```
Bedigas::route('/', function(){
    echo 'hello world!';
});
```

Finally, start the framework.

```
Bedigas::start();
```

Routing
=======

[](#routing)

Routing in Bedigas is done by matching a URL pattern with a callback function.

```
Bedigas::route('/', function(){
    echo 'hello world!';
});
```

The callback can be any object that is callable. So you can use a regular function:

```
function hello(){
    echo 'hello world!';
}

Bedigas::route('/', 'hello');
```

Or a class method:

```
class Greeting {
    public static function hello() {
        echo 'hello world!';
    }
}

Bedigas::route('/', array('Greeting', 'hello'));
```

Or an object method:

```
class Greeting
{
    public function __construct() {
        $this->name = 'John Doe';
    }

    public function hello() {
        echo "Hello, {$this->name}!";
    }
}

$greeting = new Greeting();

Bedigas::route('/', array($greeting, 'hello'));
```

Routes are matched in the order they are defined. The first route to match a request will be invoked.

Method Routing
--------------

[](#method-routing)

By default, route patterns are matched against all request methods. You can respond to specific methods by placing an identifier before the URL.

```
Bedigas::route('GET /', function(){
    echo 'I received a GET request.';
});

Bedigas::route('POST /', function(){
    echo 'I received a POST request.';
});
```

You can also map multiple methods to a single callback by using a `|` delimiter:

```
Bedigas::route('GET|POST /', function(){
    echo 'I received either a GET or a POST request.';
});
```

Regular Expressions
-------------------

[](#regular-expressions)

You can use regular expressions in your routes:

```
Bedigas::route('/user/[0-9]+', function(){
    // This will match /user/1234
});
```

Named Parameters
----------------

[](#named-parameters)

You can specify named parameters in your routes which will be passed along to your callback function.

```
Bedigas::route('/@name/@id', function($name, $id){
    echo "hello, $name ($id)!";
});
```

You can also include regular expressions with your named parameters by using the `:` delimiter:

```
Bedigas::route('/@name/@id:[0-9]{3}', function($name, $id){
    // This will match /bob/123
    // But will not match /bob/12345
});
```

Optional Parameters
-------------------

[](#optional-parameters)

You can specify named parameters that are optional for matching by wrapping segments in parentheses.

```
Bedigas::route('/blog(/@year(/@month(/@day)))', function($year, $month, $day){
    // This will match the following URLS:
    // /blog/2012/12/10
    // /blog/2012/12
    // /blog/2012
    // /blog
});
```

Any optional parameters that are not matched will be passed in as NULL.

Wildcards
---------

[](#wildcards)

Matching is only done on individual URL segments. If you want to match multiple segments you can use the `*` wildcard.

```
Bedigas::route('/blog/*', function(){
    // This will match /blog/2000/02/01
});
```

To route all requests to a single callback, you can do:

```
Bedigas::route('*', function(){
    // Do something
});
```

Passing
-------

[](#passing)

You can pass execution on to the next matching route by returning `true` from your callback function.

```
Bedigas::route('/user/@name', function($name){
    // Check some condition
    if ($name != "Bob") {
        // Continue to next route
        return true;
    }
});

Bedigas::route('/user/*', function(){
    // This will get called
});
```

Route Info
----------

[](#route-info)

If you want to inspect the matching route information, you can request for the route object to be passed to your callback by passing in `true` as the third parameter in the route method. The route object will always be the last parameter passed to your callback function.

```
Bedigas::route('/', function($route){
    // Array of HTTP methods matched against
    $route->methods;

    // Array of named parameters
    $route->params;

    // Matching regular expression
    $route->regex;

    // Contains the contents of any '*' used in the URL pattern
    $route->splat;
}, true);
```

Extending
=========

[](#extending)

Bedigas is designed to be an extensible framework. The framework comes with a set of default methods and components, but it allows you to map your own methods, register your own classes, or even override existing classes and methods.

Mapping Methods
---------------

[](#mapping-methods)

To map your own custom method, you use the `map` function:

```
// Map your method
Bedigas::map('hello', function($name){
    echo "hello $name!";
});

// Call your custom method
Bedigas::hello('Bob');
```

Registering Classes
-------------------

[](#registering-classes)

To register your own class, you use the `register` function:

```
// Register your class
Bedigas::register('user', 'User');

// Get an instance of your class
$user = Bedigas::user();
```

The register method also allows you to pass along parameters to your class constructor. So when you load your custom class, it will come pre-initialized. You can define the constructor parameters by passing in an additional array. Here's an example of loading a database connection:

```
// Register class with constructor parameters
Bedigas::register('db', 'PDO', array('mysql:host=localhost;dbname=test','user','pass'));

// Get an instance of your class
// This will create an object with the defined parameters
//
//     new PDO('mysql:host=localhost;dbname=test','user','pass');
//
$db = Bedigas::db();
```

If you pass in an additional callback parameter, it will be executed immediately after class construction. This allows you to perform any set up procedures for your new object. The callback function takes one parameter, an instance of the new object.

```
// The callback will be passed the object that was constructed
Bedigas::register('db', 'PDO', array('mysql:host=localhost;dbname=test','user','pass'), function($db){
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
});
```

By default, every time you load your class you will get a shared instance. To get a new instance of a class, simply pass in `false` as a parameter:

```
// Shared instance of the class
$shared = Bedigas::db();

// New instance of the class
$new = Bedigas::db(false);
```

Keep in mind that mapped methods have precedence over registered classes. If you declare both using the same name, only the mapped method will be invoked.

Overriding
==========

[](#overriding)

Bedigas allows you to override its default functionality to suit your own needs, without having to modify any code.

For example, when Bedigas cannot match a URL to a route, it invokes the `notFound`method which sends a generic `HTTP 404` response. You can override this behavior by using the `map` method:

```
Bedigas::map('notFound', function(){
    // Display custom 404 page
    include 'errors/404.html';
});
```

Bedigas also allows you to replace core components of the framework. For example you can replace the default Router class with your own custom class:

```
// Register your custom class
Bedigas::register('router', 'MyRouter');

// When Bedigas loads the Router instance, it will load your class
$myrouter = Bedigas::router();
```

Framework methods like `map` and `register` however cannot be overridden. You will get an error if you try to do so.

Filtering
=========

[](#filtering)

Bedigas allows you to filter methods before and after they are called. There are no predefined hooks you need to memorize. You can filter any of the default framework methods as well as any custom methods that you've mapped.

A filter function looks like this:

```
function(&$params, &$output) {
    // Filter code
}
```

Using the passed in variables you can manipulate the input parameters and/or the output.

You can have a filter run before a method by doing:

```
Bedigas::before('start', function(&$params, &$output){
    // Do something
});
```

You can have a filter run after a method by doing:

```
Bedigas::after('start', function(&$params, &$output){
    // Do something
});
```

You can add as many filters as you want to any method. They will be called in the order that they are declared.

Here's an example of the filtering process:

```
// Map a custom method
Bedigas::map('hello', function($name){
    return "Hello, $name!";
});

// Add a before filter
Bedigas::before('hello', function(&$params, &$output){
    // Manipulate the parameter
    $params[0] = 'Fred';
});

// Add an after filter
Bedigas::after('hello', function(&$params, &$output){
    // Manipulate the output
    $output .= " Have a nice day!";
});

// Invoke the custom method
echo Bedigas::hello('Bob');
```

This should display:

```
Hello Fred! Have a nice day!

```

If you have defined multiple filters, you can break the chain by returning `false`in any of your filter functions:

```
Bedigas::before('start', function(&$params, &$output){
    echo 'one';
});

Bedigas::before('start', function(&$params, &$output){
    echo 'two';

    // This will end the chain
    return false;
});

// This will not get called
Bedigas::before('start', function(&$params, &$output){
    echo 'three';
});
```

Note, core methods such as `map` and `register` cannot be filtered because they are called directly and not invoked dynamically.

Variables
=========

[](#variables)

Bedigas allows you to save variables so that they can be used anywhere in your application.

```
// Save your variable
Bedigas::set('id', 123);

// Elsewhere in your application
$id = Bedigas::get('id');
```

To see if a variable has been set you can do:

```
if (Bedigas::has('id')) {
     // Do something
}
```

You can clear a variable by doing:

```
// Clears the id variable
Bedigas::clear('id');

// Clears all variables
Bedigas::clear();
```

Bedigas also uses variables for configuration purposes.

```
Bedigas::set('bedigas.log_errors', true);
```

Views
=====

[](#views)

Bedigas provides some basic templating functionality by default. To display a view template call the `render` method with the name of the template file and optional template data:

```
Bedigas::render('hello.php', array('name' => 'Bob'));
```

The template data you pass in is automatically injected into the template and can be reference like a local variable. Template files are simply PHP files. If the content of the `hello.php` template file is:

```
Hello, ''!
```

The output would be:

```
Hello, Bob!

```

You can also manually set view variables by using the set method:

```
Bedigas::view()->set('name', 'Bob');
```

The variable `name` is now available across all your views. So you can simply do:

```
Bedigas::render('hello');
```

Note that when specifying the name of the template in the render method, you can leave out the `.php` extension.

By default Bedigas will look for a `views` directory for template files. You can set an alternate path for your templates by setting the following config:

```
Bedigas::set('bedigas.views.path', '/path/to/views');
```

Layouts
-------

[](#layouts)

It is common for websites to have a single layout template file with interchanging content. To render content to be used in a layout, you can pass in an optional parameter to the `render` method.

```
Bedigas::render('header', array('heading' => 'Hello'), 'header_content');
Bedigas::render('body', array('body' => 'World'), 'body_content');
```

Your view will then have saved variables called `header_content` and `body_content`. You can then render your layout by doing:

```
Bedigas::render('layout', array('title' => 'Home Page'));
```

If the template files looks like this:

`header.php`:

```

```

`body.php`:

```

```

`layout.php`:

```

```

The output would be:

```

Home Page

Hello
World

```

Custom Views
------------

[](#custom-views)

Bedigas allows you to swap out the default view engine simply by registering your own view class. Here's how you would use the [Smarty](http://www.smarty.net/)template engine for your views:

```
// Load Smarty library
require './Smarty/libs/Smarty.class.php';

// Register Smarty as the view class
// Also pass a callback function to configure Smarty on load
Bedigas::register('view', 'Smarty', array(), function($smarty){
    $smarty->template_dir = './templates/';
    $smarty->compile_dir = './templates_c/';
    $smarty->config_dir = './config/';
    $smarty->cache_dir = './cache/';
});

// Assign template data
Bedigas::view()->assign('name', 'Bob');

// Display the template
Bedigas::view()->display('hello.tpl');
```

For completeness, you should also override Bedigas's default render method:

```
Bedigas::map('render', function($template, $data){
    Bedigas::view()->assign($data);
    Bedigas::view()->display($template);
});
```

Error Handling
==============

[](#error-handling)

Errors and Exceptions
---------------------

[](#errors-and-exceptions)

All errors and exceptions are caught by Bedigas and passed to the `error` method. The default behavior is to send a generic `HTTP 500 Internal Server Error`response with some error information.

You can override this behavior for your own needs:

```
Bedigas::map('error', function(Exception $ex){
    // Handle error
    echo $ex->getTraceAsString();
});
```

By default errors are not logged to the web server. You can enable this by changing the config:

```
Bedigas::set('bedigas.log_errors', true);
```

Not Found
---------

[](#not-found)

When a URL can't be found, Bedigas calls the `notFound` method. The default behavior is to send an `HTTP 404 Not Found` response with a simple message.

You can override this behavior for your own needs:

```
Bedigas::map('notFound', function(){
    // Handle not found
});
```

Redirects
=========

[](#redirects)

You can redirect the current request by using the `redirect` method and passing in a new URL:

```
Bedigas::redirect('/new/location');
```

By default Bedigas sends a HTTP 303 status code. You can optionally set a custom code:

```
Bedigas::redirect('/new/location', 401);
```

Requests
========

[](#requests)

Bedigas encapsulates the HTTP request into a single object, which can be accessed by doing:

```
$request = Bedigas::request();
```

The request object provides the following properties:

```
url - The URL being requested
base - The parent subdirectory of the URL
method - The request method (GET, POST, PUT, DELETE)
referrer - The referrer URL
ip - IP address of the client
ajax - Whether the request is an AJAX request
scheme - The server protocol (http, https)
user_agent - Browser information
type - The content type
length - The content length
query - Query string parameters
data - Post data or JSON data
cookies - Cookie data
files - Uploaded files
secure - Whether the connection is secure
accept - HTTP accept parameters
proxy_ip - Proxy IP address of the client
host - The request host name

```

You can access the `query`, `data`, `cookies`, and `files` properties as arrays or objects.

So, to get a query string parameter, you can do:

```
$id = Bedigas::request()->query['id'];
```

Or you can do:

```
$id = Bedigas::request()->query->id;
```

RAW Request Body
----------------

[](#raw-request-body)

To get the raw HTTP request body, for example when dealing with PUT requests, you can do:

```
$body = Bedigas::request()->getBody();
```

JSON Input
----------

[](#json-input)

If you send a request with the type `application/json` and the data `{"id": 123}` it will be available from the `data` property:

```
$id = Bedigas::request()->data->id;
```

HTTP Caching
============

[](#http-caching)

Bedigas provides built-in support for HTTP level caching. If the caching condition is met, Bedigas will return an HTTP `304 Not Modified` response. The next time the client requests the same resource, they will be prompted to use their locally cached version.

Last-Modified
-------------

[](#last-modified)

You can use the `lastModified` method and pass in a UNIX timestamp to set the date and time a page was last modified. The client will continue to use their cache until the last modified value is changed.

```
Bedigas::route('/news', function(){
    Bedigas::lastModified(1234567890);
    echo 'This content will be cached.';
});
```

ETag
----

[](#etag)

`ETag` caching is similar to `Last-Modified`, except you can specify any id you want for the resource:

```
Bedigas::route('/news', function(){
    Bedigas::etag('my-unique-id');
    echo 'This content will be cached.';
});
```

Keep in mind that calling either `lastModified` or `etag` will both set and check the cache value. If the cache value is the same between requests, Bedigas will immediately send an `HTTP 304` response and stop processing.

Stopping
========

[](#stopping)

You can stop the framework at any point by calling the `halt` method:

```
Bedigas::halt();
```

You can also specify an optional `HTTP` status code and message:

```
Bedigas::halt(200, 'Be right back...');
```

Calling `halt` will discard any response content up to that point. If you want to stop the framework and output the current response, use the `stop` method:

```
Bedigas::stop();
```

JSON
====

[](#json)

Bedigas provides support for sending JSON and JSONP responses. To send a JSON response you pass some data to be JSON encoded:

```
Bedigas::json(array('id' => 123));
```

For JSONP requests you, can optionally pass in the query parameter name you are using to define your callback function:

```
Bedigas::jsonp(array('id' => 123), 'q');
```

So, when making a GET request using `?q=my_func`, you should receive the output:

```
my_func({"id":123});

```

If you don't pass in a query parameter name it will default to `jsonp`.

Configuration
=============

[](#configuration)

You can customize certain behaviors of Bedigas by setting configuration values through the `set` method.

```
Bedigas::set('bedigas.log_errors', true);
```

The following is a list of all the available configuration settings:

```
bedigas.base_url - Override the base url of the request. (default: null)
bedigas.case_sensitive - Case sensitive matching for URLs. (default: false)
bedigas.handle_errors - Allow Bedigas to handle all errors internally. (default: true)
bedigas.log_errors - Log errors to the web server's error log file. (default: false)
bedigas.views.path - Directory containing view template files. (default: ./views)
bedigas.views.extension - View template file extension. (default: .php)

```

Framework Methods
=================

[](#framework-methods)

Bedigas is designed to be easy to use and understand. The following is the complete set of methods for the framework. It consists of core methods, which are regular static methods, and extensible methods, which are mapped methods that can be filtered or overridden.

Core Methods
------------

[](#core-methods)

```
Bedigas::map($name, $callback) // Creates a custom framework method.
Bedigas::register($name, $class, [$params], [$callback]) // Registers a class to a framework method.
Bedigas::before($name, $callback) // Adds a filter before a framework method.
Bedigas::after($name, $callback) // Adds a filter after a framework method.
Bedigas::path($path) // Adds a path for autoloading classes.
Bedigas::get($key) // Gets a variable.
Bedigas::set($key, $value) // Sets a variable.
Bedigas::has($key) // Checks if a variable is set.
Bedigas::clear([$key]) // Clears a variable.
Bedigas::init() // Initializes the framework to its default settings.
Bedigas::app() // Gets the application object instance
```

Extensible Methods
------------------

[](#extensible-methods)

```
Bedigas::start() // Starts the framework.
Bedigas::stop() // Stops the framework and sends a response.
Bedigas::halt([$code], [$message]) // Stop the framework with an optional status code and message.
Bedigas::route($pattern, $callback) // Maps a URL pattern to a callback.
Bedigas::redirect($url, [$code]) // Redirects to another URL.
Bedigas::render($file, [$data], [$key]) // Renders a template file.
Bedigas::error($exception) // Sends an HTTP 500 response.
Bedigas::notFound() // Sends an HTTP 404 response.
Bedigas::etag($id, [$type]) // Performs ETag HTTP caching.
Bedigas::lastModified($time) // Performs last modified HTTP caching.
Bedigas::json($data, [$code], [$encode], [$charset], [$option]) // Sends a JSON response.
Bedigas::jsonp($data, [$param], [$code], [$encode], [$charset], [$option]) // Sends a JSONP response.
```

Any custom methods added with `map` and `register` can also be filtered.

Framework Instance
==================

[](#framework-instance)

Instead of running Bedigas as a global static class, you can optionally run it as an object instance.

```
require 'bedigas/autoload.php';

use bedigas\Engine;

$app = new Engine();

$app->route('/', function(){
    echo 'hello world!';
});

$app->start();
```

So instead of calling the static method, you would call the instance method with the same name on the Engine object.

###  Health Score

18

—

LowBetter than 8% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity35

Early-stage or recently created project

 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://avatars.githubusercontent.com/u/28798039?v=4)[arjunacoding](/maintainers/arjunacoding)[@arjunacoding](https://github.com/arjunacoding)

---

Top Contributors

[![arjunacoding](https://avatars.githubusercontent.com/u/28798039?v=4)](https://github.com/arjunacoding "arjunacoding (2 commits)")

### Embed Badge

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

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

###  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)
