Download the PHP package net-tools/simple_framework without Composer

On this page you can find all versions of the php package net-tools/simple_framework. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package simple_framework

net-tools/simple_framework

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

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

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 ?

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 :

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

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 :

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.


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 :

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 :

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 :

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 <b>'test'</b> is called with parameter 'hello world' !

and

{"value":"hello world"}

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 :

The WebApplication object is created so that the application could run. Its first parameter is the namespace where to look for command classes (please refer to the first examples here, the namespace for the commands is Myapp\Commands), and the second parameter is a Registry object used to store config data (to keep the example simple, the registry is empty ; we don't need any config data for this test). If you need to handle sensitive config data, create your registry (with Config\ConfigObject, JsonFile, or Json) and then encapsulate it in a top-level Config\PrivateConfig registry (see specific sample file).

Then, the run() method is called on the Application $app object : the command refered by the cmd querystring parameter is searched in the Myapp\Commands namespace, invoked, and its results set into $output.

For web applications (which will probably be the case), some returned values should be sent immediately to the browser (Json, Download, either FileDownload or StringDownload) ; the process to do that output or not (depending on the returned value type) is handled by the classes in ReturnedValuesOutput sub-namespace. If a class named WebController_xxxx, where xxxx stands for the returned value class (Download, Json, etc.), this class is responsible for the output. If no class is found for a returned value, there's no output to browser, and the value will be returned by $app->run().

Please refer to classes in the ReturnedValues sub-namespace of Nettools\Simple_Framework for a complete list of acceptable returned values (all inheriting from ReturnedValues\Value).

In other cases, the command returns a value, which is fetched from $app->run() execution. In most cases this will be some HTML content or a primitive PHP type (string, int, etc.), that you can include or use in your page template later : echo $output will cast the ReturnedValues\Value object to a string.

Handling error cases and exceptions

When an exception occurs or when you want a command to fail on purpose, the framework will do some specific stuff.

Exceptions : the framework handles then

Exceptions thrown (and not catched by your code) during code execution are catched in the run() method of Controller object and a specific screen with all required data for debugging is displayed (and the script is halted). For your information, this debugging data is formatted by Nettools\Core\ExceptionHandlers\SimpleExceptionHandler (you may refer to the Nettools\Core package to read more documentation : http://net-tools.ovh/api-reference/net-tools/Nettools/Core/ExceptionHandlers.html ).

You may set another exception handler (for example, you don't want debug data displayed on screen and prefer sending debug data to the webadmin by email) ; to do so, when creating the $app object, create a Registry named appcfg and set its application->exceptionHandler value to a handler of your choice (however, it must inherit from Nettools\Core\ExceptionHandlers\ExceptionHandler, such as Nettools\Core\ExceptionHandlers\PublicExceptionHandler which display on screen a short message about exception, with no debug data ; debug data is sent to [email protected] as email body and attachment with stack trace).

Error cases : failures

Sometimes, you want to say that a command execution has not succeeded. To do so, during your command execute call, call the fail() on $this with the appropriate error message :

The application controller will then process the error :

If you want to answer with an error feedback other than a string, you can't use the fail mechanism. However, you can reply to the command with a ReturnedValues\Value with an unsuccessful state ; all returnXXX methods of Command class have a second parameter to set the state of the execution (successful/true by default).

Security features

The framework can be used with no security features at all (except request being sanitized) or can be configured to send request with a special computed token identifying the user (thus preventing unauthorized requests being made on behalf of other users).

The token is created through a secret (to be configured in the appcfg registry, in controller->userSecurityHandlers array of security handlers) and the current user ID in the request (can be any request parameter ; by default, it's the i parameter). It's passed in the request, along with other parameters, as a h parameter (by default, but can be named with anything else). See appropriate sample for use case.

This token-based security feature only prevents hackers to issue request for another user ID without being authorized (logged), since they don't know how to compute the token value (don't know the secret nor the hash process).

However, it does not prevent the request to be issued several times (the token is not a nonce value), nor does it prevent the request to be vulnerable to CSRF attacks.

In future releases, CSRF security features and nonce token may be implemented.

PHPUnit

To test with PHPUnit, point the -c configuration option to the /phpunit.xml configuration file.


All versions of simple_framework with dependencies

PHP Build Version
Package Version
Requires php Version >= 8.0
net-tools/core Version ^1.0.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package net-tools/simple_framework contains the following files

Loading the files please wait ....