PHPackages                             nearbycreative/light-framework - 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. nearbycreative/light-framework

ActiveLibrary[Framework](/categories/framework)

nearbycreative/light-framework
==============================

Light Framework makes it ridiculously easy to throw up a powerful JSON API

0.3.3(4y ago)020MITPHPPHP &gt;=7.2CI failing

Since Feb 2Pushed 4y ago2 watchersCompare

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

READMEChangelogDependencies (7)Versions (16)Used By (0)

Light is a micro-framework for quickly spinning up powerful APIs. It can be used both as a standalone framework or embedded into an existing application.

Frameworks like Laravel and Symfony are powerful, but when you need to build out an API, a web framework with the whole kitchen sink can over complicate with option overload.

Light aims to use familiar open source technologies like Eloquent and Phinx, with simple paradigms like MVC to make building out APIs lightning fast.

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

[](#requirements)

- php7.2+
- A web server with `mod_rewrite` enabled

Quickstart a Fresh Project
==========================

[](#quickstart-a-fresh-project)

If you're starting your project from scratch, the following commands will get you up and running quickly!

```
mkdir myproject && cd myproject

composer require nearbycreative/light-framework

./vendor/bin/light init
php light serve

```

That's it! You can visit  to see Light in action!

#### Directory structure

[](#directory-structure)

`light init` creates the following directory structure:

```
app/
    controller/
        Welcome.php
    model/
database/
    migrations/
    seeds/
public/
    .htaccess
    index.php
resources/
    js/
    sass/
    views/
.env
light

```

Note, this is the default scaffolding to get a new application up and running quickly. If you're including Light into an existing project, you probably don't want to run `light init`, but rather embed it. [Learn how to embed Light in an existing application](docs/embed.md).

### Using route closures:

[](#using-route-closures)

If you look at the generated *public/index.php*, you'll see it provided a few simple routes to start you off. If you wanted to, you could create your entire application in a single file using just route closures, ie:

An example *index.php*:

```
require_once __DIR__ . '/../vendor/autoload.php';

$app = new \Light\App();
$route = $app->routes;

$route->get('/test', function() {
    return ['hello' => 'world'];
});

$route->get('/test/{slug}', function($slug) {
    return ['slug' => $slug];
});

```

Spin up a test server with `php light serve` command.

Visit:

You'll see the following output:

```
{
    "code": 200,
    "status": "success",
    "time": "2020-02-02T05:09:15.457290Z",
    "data": {
        "hello": "world"
    }
}

```

Visit:

You'll see the following output:

```
{
    "code": 200,
    "status": "success",
    "time": "2020-02-02T05:09:15.457290Z",
    "data": {
        "slug": "this-is-cool"
    }
}

```

### Using controllers

[](#using-controllers)

Putting all your API endpoints in route closures works fine for small APIs. Though, this can get pretty narly, pretty quickly. Another way to organize your API code is using *Light Controllers.*

You can create a controller in your application that extends Light\\Controller manually, or use the `php light make:controller` command.

Ie: `php light make:controller Test` will generate the following controller in \_app/Controller/Test.php:

```
