PHPackages                             net-tools/simple\_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. net-tools/simple\_framework

ActiveLibrary

net-tools/simple\_framework
===========================

Composer library for simple app framework

1.3.4(3mo ago)0383MITPHPPHP &gt;= 8.0

Since Feb 27Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/net-tools/simple_framework)[ Packagist](https://packagist.org/packages/net-tools/simple_framework)[ RSS](/packages/net-tools-simple-framework/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (2)Versions (61)Used By (0)

net-tools/simple\_framework
===========================

[](#net-toolssimple_framework)

Composer library to create simple web applications
--------------------------------------------------

[](#composer-library-to-create-simple-web-applications)

This package defines a framework that can be used to create simple web applications. The end-user can focus on the "business" coding part, and forget about sanitizing user submitted-data, responding to XMLHttpRequest and so on.

Setup instructions
------------------

[](#setup-instructions)

To install net-tools/simple\_framework package, just require it through composer : `require net-tools/simple_framework:^1.0.0`.

Sample files
------------

[](#sample-files)

There's a `samples` subdirectory in the package with a very simple app. Please read first this readme and then you may refer to this sample file.

How to use ?
------------

[](#how-to-use-)

### Framework preview

[](#framework-preview)

The framework focuses on commands, that is to say PHP code "responding" to a request sent to the application. The request could be a GET/POST request (possibly with file uploads) or a XMLHttpRequest. The command "answers" to the request with a returned value that can be selected among :

- **PHP value** (any kind of data type) : used when the command only does some back-office stuff, such as computing something and returning the result of the computation
- **JSON string** : used to answer to a XMLHttpRequest
- **File download** : so that the end-user can download some data from your application (either a file content or some string generated on-the-fly)
- **HTML content** : used to answer with some formatting to be outputed later on screen (for example, the commands generates a list of products)

This is a simple framework, and you may say that generating view content should not be mixed with computations. You are correct, but the goal is to create a simple framework with basic stuff.

### Create command classes

[](#create-command-classes)

#### Common cases

[](#common-cases)

To answer to a command (such as a GET/POST request), you just have to create a class named on the command name, and inherit from `Command` class :

```
namespace Myapp\Commands;

use \Nettools\Simple_Framework\Command;
use \Nettools\Simple_Framework\Request;
use \Nettools\Simple_Framework\Application;

class Test extends Command
{
    public function execute(Request $req, Application $app)
    {
        return $this->returnHTML("Command 'test' is called with parameter '{$req->param}' !");
    }
}
```

As you may guess, this command returns HTML content. This content is not outputted to screen yet. This is done later, in your page template.

The string returned will contain the value of querystring 'param', which is accessible through the `Request $req` object. This object is filled with all GET/POST parameters, as PHP properties. Data is sanitized before being set to the `Request` object.

---

If you want to answer to XMLHttpRequests, return a JSON value with either on the the following lines. The `returnJson()` method of `Command` class is smart enough to allow different data types and then converting them internally to a JSON-formatted string.

```
return $this->returnJson('{"value":"'. $req->param . '"}');        // string
return $this->returnJson(array('value' => $req->param));           // associative array
return $this->returnJson((object)array('value' => $req->param));   // object litteral
```

---

To respond to a command with a download, use either the `returnFileDownload()` or `returnStringDownload()` depending on whether the data is contained in a file or a string generated on-the-fly :

```
// downloading a file with Mimetype 'text/plain', the browser will suggest the name 'test.txt' as filename with 'my file content' as data downloaded.
return $this->returnStringDownload("my file content", 'test.txt', 'text/plain');

// downloading a file from path '/tmp/compute.bin', with Mimetype 'application/octet-stream' ; when saved, the browser will suggest 'data.bin' as filename
return $this->returnFileDownload('/tmp/compute.bin', 'data.bin', 'application/octet-stream');
```

#### Handling file uploads

[](#handling-file-uploads)

If you want to handle files uploaded by user, use the `getFileUpload()` method of `Request` class to fetch a specific `FileUploadRequest` object describing the file uploaded :

```
namespace Myapp\Commands;

use \Nettools\Simple_Framework\Command;
use \Nettools\Simple_Framework\Request;
use \Nettools\Simple_Framework\Application;

class Upload extends Command
{
    public function execute(Request $req, Application $app)
    {
        // the input named 'upload' should always be in the request, even if no file has been submitted.
        // $f will contain a FileUploadRequest object.
        if ( $f = $req->getFileUpload('upload') )
            // if a file has been submitted
            if ( $f->uploaded() )
            {
                // we erase the temp file, this is just a test
                unlink($f->tmp_name);
                return $this->returnString('File was sent');
            }

            // if no file has been submitted
            else if ( $f->no_file() )
                return $this->returnString('The user has not uploaded a file');

            // unknown other error
            else
                return $this->returnString('Upload error');
        else
            return $this->returnString('Field upload does not exist');
    }
}
```

### Send commands

[](#send-commands)

To execute the commands defined before, you have to send a HTTP request to the URI of the application. If 'index.php' is the application file :

```
index.php?cmd=test&param=hello+world

```

As you can see, the name of the command should be set in the `cmd` URI querystring parameter. Other parameters are meant to be used during the command execution (such as `$req->param` for `param` querystring value).

Requests can also be sent with POST verb or XMLHttpRequest from Javascript.

The first two examples on this page (HTML response and XMLHttpResponse) would output :

`Command 'test' is called with parameter 'hello world' !`

and

`{"value":"hello world"}`

### Launch application to handle requests

[](#launch-application-to-handle-requests)

When sending commands to your application with an HTTP request such as `index.php?cmd=test&param=hello+world`, you need to launch the application framework so that it could handle request and return output from command execution :

```
