PHPackages                             xx19941215/light-project - 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. xx19941215/light-project

ActiveLibrary

xx19941215/light-project
========================

1.1.3(8y ago)011CSS

Since Mar 8Pushed 8y ago1 watchersCompare

[ Source](https://github.com/xx19941215/light-project)[ Packagist](https://packagist.org/packages/xx19941215/light-project)[ RSS](/packages/xx19941215-light-project/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (3)Versions (5)Used By (0)

[![](https://camo.githubusercontent.com/b22971838c5fb00ae0d3ec4d505bcaa77c06b3b9d0b4bbb2b88c2c8ced5768ed/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732f787831393934313231352f6c696768742d6672616d65776f726b2e737667)](https://github.com/xx19941215/light-framework/releases)[![](https://camo.githubusercontent.com/a8b93c26e896dad2416a4fdab679defd278283d598e4b34d97644a06c52a143b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f666f726b732f787831393934313231352f6c696768742d6672616d65776f726b2e737667)](https://github.com/xx19941215/light-framework/releases)[![](https://camo.githubusercontent.com/0369456ed87dfe1ac9aed30f6bdfedb847e763fdc1796ad5d50100a3b3370e12/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f787831393934313231352f6c696768742d6672616d65776f726b2e737667)](https://github.com/xx19941215/light-framework/releases)[![](https://camo.githubusercontent.com/98e06ab279ef7d8f5dc610b12861b445a427f985f0ced740b6982d8fac4b3fe8/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d372e302532422d626c75652e737667)](https://github.com/xx19941215/light-framework/releases)[![License](https://camo.githubusercontent.com/e613ab3c2d83fd60009d0a4f2d2a7493048d63e7ddf28730e2825721b80bf7aa/68747470733a2f2f696d672e736869656c64732e696f2f636f636f61706f64732f6c2f41464e6574776f726b696e672e737667)](https://opensource.org/licenses/MIT)

 [中文版](./README-CN.md)

Building a production oriented PHP framework from scratch
=========================================================

[](#building-a-production-oriented-php-framework-from-scratch)

It is clear that making wheels is one of the better ways to learn a programming language, not a waste of time.

How can then build a production oriented PHP framework? The general process is as follows:

```
Entry file ----> Load Composer vendor class and function
           ----> Register error(and exception) function
           ----> Load config file
           ----> Request
           ----> Router
           ----> (Controller  Model)
           ----> View Or Json

```

In addition to this, we also need unit testing, some auxiliary scripts, and so on. The final directory of my framework is as follows:

Project Directory Structure
===========================

[](#project-directory-structure)

```
├── app [Application directory]
├── bin [Auxiliary scripts directory]
├── bootstrap [Light bootstrap directory]
│ └── app.php [Light App bootstrap file]
├── config [Core Config directory]
│ └── .gitignore
│ └── app.php[app config]
│ └── config.php [app base config]
│ └── database.php [database config]
│ └── i18n.php [i18n config]
│ └── session.php [session config]
│ └── site.php [site config]
│ └── swoole.php [swoole config]
├── light [Light Framework Core directory]
├── public [public directory]
│ ├── index.php [entry file]
│ ├── css [css resource directory]
│ ├── js [javascript resource directory]
├── resources [resource directory]
│ ├── assets [front resource directory]
│       └── sass [sass resource directory]
│              └── app.scss[entry sass file]
│              └── …
│       └── js [javascript resource directory]
│              └── app.js[entry js file]
│              └── …
│ ├── views [PHP view resource directory]
│       └── layouts [layout directory]
│       └── page [page directory]
storage [Other Framework resource directory]
├── framework [framework cache directory]
│             └──cache [cache directory]
│                   └── router.php [router cache]
│                   └── config.php [config cache]
├── logs [log directory]
│             └──error.log [error log]
│             └──light.log [light log]
│             └──access.log.gz [access log]
│             └──swoole.log [swoole log]
tests [Unit test directory]
vendor [composer vendor directory]
.env.example [the environment variables example file]
.gitignore [git ignore config file]
LICENSE [lincese file]
composer.json [composer file]
composer.lock [composer lock file]
package.json [package.json file]
phpunit.xml [phpunit config file]
README-CN.md [readme chinese]
README.md [readme]
webpack.config.js [webpack config file]
yarn.lock [yarn lock file]

```

Lifecycle：
==========

[](#lifecycle)

[![](https://camo.githubusercontent.com/33644e5008c99e71d3654b75527916f2cd285075260344632d561344c47ca6c0/687474703a2f2f626c6f672e7869616f7869616f2e776f726b2f77702d636f6e74656e742f75706c6f6164732f323031382f30322f6c6966656379636c652e706e67)](https://camo.githubusercontent.com/33644e5008c99e71d3654b75527916f2cd285075260344632d561344c47ca6c0/687474703a2f2f626c6f672e7869616f7869616f2e776f726b2f77702d636f6e74656e742f75706c6f6164732f323031382f30322f6c6966656379636c652e706e67)

Module description：
===================

[](#module-description)

Entrance file
-------------

[](#entrance-file)

```
// Load the bootstrap file
$app = require __DIR__ . '/../bootstrap/app.php';

//Build Request
$request = new \Light\Http\Request(
    $_GET,
    $_POST,
    array(),
    $_COOKIE,
    $_FILES,
    $_SERVER
);

//Light app handle request
$response = $app->handle($request);
//Send Response
$response->send();
```

[public/index.php](https://github.com/xx19941215/light/blob/master/public/index.php)

Error&amp;Exception Handle Module
---------------------------------

[](#errorexception-handle-module)

- Error:

Register a function by used set\_error\_handler to handle error, but it can't handle the following error, E\_ERROR, E\_PARSE, E\_CORE\_ERROR, E\_CORE\_WARNING, E\_COMPILE\_ERROR, E\_COMPILE\_WARNING and the E\_STRICT produced by the file which called set\_error\_handler function. So, we need use register\_shutdown\_function and error\_get\_last to handle this finally error which set\_error\_handler can't handle. When the framework running, we can handle the error by ourself, such as, give a friendly error messge for client.

- Exception:

Register a function by used set\_exception\_handler to handle the exception which is not be catched, which can give a friendly error messge for client.

[light/Concerns/RegistersExceptionHandlers.php](https://github.com/xx19941215/light/blob/master/light/src/Concerns/RegistersExceptionHandlers.php)

Config Module
-------------

[](#config-module)

Load the framework and user defined configuration file.

```
class Config implements \ArrayAccess
{

}
```

The Config class implements ArrayAccess. So we can access configuration files in a way similar to access an array.

[light/Config/Config.php](https://github.com/xx19941215/light/blob/master/light/src/Config/Config.php)

Router Module
-------------

[](#router-module)

Basic usage:

```
$this
    //Route site
    ->site('www')
    //Route privilege
    ->access('public')
    //GET Method
    ->get(
    //Request path
        '/',
    //Route aliases
        'index',
    //Route controller
        'Blog\Index\Ui\IndexController@show'
    );

```

In Light, multi site routing configuration is supported by default. It can be configured in [`site.php`](https://github.com/xx19941215/light/blob/master/config/site.php). Can be used in routing

Use the `site` method to set the site that the current.

[app/blog/post/setting/router/post.php](https://github.com/xx19941215/light/blob/master/app/blog/post/setting/router/post.php)

Light's router is based on [`nikic/fast-route`](https://github.com/nikic/FastRoute)。

[light/src/Routing/Router.php](https://github.com/xx19941215/light/blob/master/light/src/Routing/Router.php)

ORM
---

[](#orm)

Object Relation Mapping(ORM), its main role is in programming, the concept of object-oriented database table with the corresponding concept. For example, I define an object, it corresponds to a table, an instance of this object corresponds to a record in the table. In the framework, chain encapsulation of common SQL operations is implemented. Subsequent operation will be completed through the operation of the database object. The basic usage of a SELECT query in Light

In Repo subclass

```
$ssb = $this->cnn->select()
            ->from('wp_posts')
            ->where('post_status', '=', 'publish')
            ->andWhere('post_type', '=', 'post')
            ->orderBy('post_date', 'desc')
            ->limit(15);
```

In places that do not inherit from Repo, such as View, you can use the DB's [Facade](https://github.com/xx19941215/light/blob/master/light/src/Support/Facades/DB.php)

```
$ssb = DB::select()
            ->from('wp_posts')
            ->where('post_status', '=', 'publish')
            ->andWhere('post_type', '=', 'post')
            ->orderBy('post_date', 'desc')
            ->limit(15);
```

Returns [`DataSet`](https://github.com/xx19941215/light/blob/master/light/src/Database/DateSet.php) and the DataSet instance will do some simple processing of the data returned by `$ssb` .

[app/blog/post/src/repo/ListPostRepo.php](https://github.com/xx19941215/light/blob/master/app/blog/post/src/Repo/ListPostRepo.php)

```
return $this->dataSet($ssb, Post::class);
```

In places that do not inherit from Repo，you can use[`collect`](https://github.com/xx19941215/light/blob/master/light/src/Support/functions.php)to resolve DataSet。

Print the title of article.

```
foreach ($posts->getItems() as $post) {
    echo $post->title . PHP_EOL;
}
```

Service Container Module
------------------------

[](#service-container-module)

Light's core is a service container. The service container provides the entire range of services needed in the framework. In our day-to-day development, creating objects is so ubiquitous that it's so familiar to them that it's very tedious, and it's pretty bad for each time an object needs to be new-handed. But more seriously, the loosely coupled, less intrusive principle that we have always advocated has become useless in this situation.

When it comes to service containers, what I have to mention is control inversion, or IOC for short, which is a commonly used design pattern. Dependency injection is a way to implement IOCs.

The basic attributes and methods in Light's service container are as follows

```
- attributes
    + bindings
    + _instance
    # instances
    # aliases

- methods
    + bind
    + getClosure
    + make
    # getConcrete
    + build
    # getDependencies
    # resolveClass
    # isBuildable
    + singleton
    + _setInstance
    + instance
    + isShared
    # dropStaleInstances
    + getAlias
    + bound
    + isAlias
    + _getInstance
    + call

```

[light/Foundation/App.php](https://github.com/xx19941215/light/blob/master/light/src/Foundation/App.php)

MVC To MVSC
-----------

[](#mvc-to-mvsc)

Software from the development of a transaction to deal with many affairs, between the affairs of the inclusion, order, primary and secondary relationship, become more and more complex. Because of the huge data and logic, in Light, in addition to the traditional MVC three-tier structure, it is recommended to add a new layer of Service to handle tedious business logic. This time, Controller layer can display different views according to the different devices, but Service layer business logic has been reused. In Light, an App can consist of the following structure:

- Model: The mapping exists as a field of the database table
- Repo: execute Model's crud operation
- Controller: handles the presentation of data and views distributed by the Service
- Service: Processing business logic
- View: view

[Service/FetchPostService.php](https://github.com/xx19941215/light/blob/master/app/blog/post/src/Service/FetchPostService.php)

View &amp; Meta &amp; Trans
---------------------------

[](#view--meta--trans)

Light's view layer supports layout, components and other flexible organizational view layer structure, the underlying directly using [`foil`](https://github.com/FoilPHP/Foil) to achieve, You can place your view file in the project's resource/views folder.

Light customizes the `title`,` description`, `keywords` for each page in the view layer, as well as supports internationalization of other text. [light/Meta/Meta.php](https://github.com/xx19941215/light/blob/master/light/src/Meta/Meta.php)

FrontEnd Module
---------------

[](#frontend-module)

Light builds `Javascript` with` webpack` and compiles the `scss` file with` node-sass` to produce front-end style files. The front-end resource files are placed in `resource/assets`.

### build steps

[](#build-steps)

1.Install the dependencies

```
yarn install

```

2.FrontEnd build script

```
npm run build: js
npm run build: css

```

Generated resource files will be stored in the `public` folder under the appropriate directory for public access.

PHPUnit
-------

[](#phpunit)

Based on PHPUnit, Light will continue to refine the test. Run the following command to get started

```
./vendor/bin/phpunit
```

Test example:

```
class ConfigTest extends TestCase
{
    protected $config;
    protected $data;

    public function setUp()
    {
        $this->config = new Config($this->data = [
            'foo' => 'bar',
            'bar' => 'baz',
            'baz' => 'bat',
            'null' => null,
            'associate' => [
                'x' => 'xxx',
                'y' => 'yyy',
            ],
            'array' => [
                'aaa',
                'zzz',
            ],
            'x' => [
                'z' => 'zoo',
            ],
        ]);

        parent::setUp();
    }

    public function testConstruct()
    {
        $this->assertInstanceOf(Config::class, $this->config);
    }
}
```

[light/tests/ConfigTest.php](https://github.com/xx19941215/light/blob/master/light/tests/Config/ConfigTest.php)

How to use?
===========

[](#how-to-use)

1.Create project with composer

```
composer create-project xx19941215/light-project light-project  && cd light-project

```

2.Composer install &amp;&amp; npm i

```
composer install && npm i

```

3.Nginx configuration, the following are examples. You should modify the project path and php-fpm configuration according to your environment.

```
server {
    listen  80;
    #listen [::]:80 ipv6only=on;
    server_name www.light-project.test
		static.light-projecr.test;

    #return 301 https://$server_name$request_uri;

    index   index.php index.html;
    root    /path/to/light-project/public;

    access_log  /path/to/light-project/storage/logs/access.log.gz combined gzip;
    error_log /path/to/light-project/storage/logs/error.log;

    client_max_body_size 20M;

    gzip  on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 6;
    gzip_types  text/plain application/javascript application/x-javascript text/javascript text/xml text/css;
    gzip_disable "MSIE [1-6]\.";
    gzip_vary on;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php(/|$) {
        try_files $uri = 404;
        include fastcgi.conf;
        fastcgi_connect_timeout 60;
        fastcgi_send_timeout 180;
        fastcgi_read_timeout 180;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 4 256k;
        fastcgi_busy_buffers_size 256k;
        fastcgi_temp_file_write_size 256k;

        fastcgi_index   index.php;
        #fastcgi_pass   unix:/run/php/php7.0-fpm.sock;
        fastcgi_pass    127.0.0.1:9000;

        location ~ /\.ht {
            deny all;
        }
    }
}

server {
    listen      80;
    server_name static.light-project.test;

    index index.html index.htm;
    root /path/to/light-project/public/static;

    access_log /path/to/light-project/storage/logs/static.access.log.gz combined gzip;
    error_log /path/to/light-project/storage/logs/static.error.log;

    client_max_body_size 20M;

    location / {
    }

    location ~* \.(eot|svg|ttf|woff|woff2)$ {
        if ($http_origin ~* '^https?://[^/]+\.light-project\.test$') {
            add_header Access-Control-Allow-Origin $http_origin;
        }
    }

    location ~ /\.ht {
        deny all;
    }
}

```

4.Hosts configuration.

```
127.0.0.1 www.light-project.test
127.0.0.1 static.light-project.test

```

5.Copy .env.example to .env, config `APP_BASE_HOST` and database.

```
APP_DEBUG=true
APP_BASE_HOST=light-project.test
DB_HOST=localhost
DB_USERNAME=root
DB_PASSWORD=qwertyuiop
DB_DATABASE=light
CACHE_DRIVER=redis
I18N=false

```

6.Make sure the services as follow are running.

```
redis
mysql
php-fpm
nginx

```

7.Create meta table in your database.

```
CREATE TABLE `meta` (
  `metaId` varbinary(21) NOT NULL,
  `key` varchar(20) NOT NULL,
  `localeKey` varchar(20) NOT NULL,
  `value` varchar(20) NOT NULL,
  `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `changed` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`metaId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
```

8.Open the browser and visit

TODO
====

[](#todo)

- Database Migration
- Security
- Session

DONE
====

[](#done)

- 1.1.3
    - Console Application

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity66

Established project with proven stability

 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.

###  Release Activity

Cadence

Every ~3 days

Total

4

Last Release

2977d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/b025c0dc90f9d3aea8e6afcc2f792a5103a6eda7e2c4349d9368b2fa77d547f3?d=identicon)[xx19941215](/maintainers/xx19941215)

---

Top Contributors

[![xx19941215](https://avatars.githubusercontent.com/u/10104328?v=4)](https://github.com/xx19941215 "xx19941215 (3 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/xx19941215-light-project/health.svg)

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

PHPackages © 2026

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