Download the PHP package netolabs/php-lambda-router without Composer

On this page you can find all versions of the php package netolabs/php-lambda-router. 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 php-lambda-router

php-lambda-router

Simple handler-based router middleware for the php-lambda-runtime library.

Installation

Run composer require netolabs/php-lambda-router netolabs/php-lambda-runtime from the root of your project.

Using the Router Middleware

The Router middleware is used to route our Lambda to a Controller and Action, much like an MVC framework would. The difference being that instead of routing based on the request path, we use the Lambda handler name. This allows us to have multiple Lambda functions inside the same repository and also allowing more reuse of code.

The handler name is expected in the format of "controller.action". Internally, the router will attempt to load the controller and check if the action method exists. If not, it will pass the request on to the next middleware in the queue; usually a fallback or a 404 middleware.

For example: if the handler name is helloworld.get, the router will attempt to load the \App\Controller\HelloworldController class and execute the method getAction($request).

The default namespace is \App\Controller\, however this is configurable via constructor parameter.

Example

Adding the Router middleware

In this example we're using the Router to route our Lambda to a Controller and Action. It includes an exception handler and a fallback which returns a 404 response if the route isn't matched. In your app.php use the following code:

<?php
$app = \Neto\Lambda\Application\AppFactory::create();
$app->addMiddleware(new \Neto\Lambda\Middleware\ExceptionHandler(true))
    ->addMiddleware(new \Neto\Lambda\Middleware\HandlerRouter(getenv('_HANDLER')))
    ->addMiddleware(new \Neto\Lambda\Middleware\FileNotFound())
    ->run();

Creating a Controller

Controllers are autoloaded using the Lambda handler name and must conform to the PSR-4 standard.

mkdir -p src/App/Controller
touch src/App/Controller/AppController.php

src/App/Controller/AppController.php:

<?php
namespace App\Controller;

use GuzzleHttp\Psr7\Response;
use Neto\Lambda\Controller\AbstractController;

class AppController extends AbstractController
{
    public function helloworldAction(RequestInterface $request)
    {
        return new Response(200, [], '<h1>Hello world!</h1>');
    }
}

Send it

Now run it using vendor/bin/invoke -h app.helloworld. You'll notice we are using "app.helloworld" as our handler name - this is used to resolve our controller class (AppController) and action method (HelloworldAction).

PSR-11 Containers and auto-wiring

Both the Router middleware and the AbstractController implement the ContainerAwareInterface. If you'd like to use a DI container, you only need to inject it into the HandlerRouter either via constructor or method. The container object (when set) will also be injected automatically into controllers during instantiation.
Because this library uses PHP-DI's invoker, you can use typed parameters for your action methods in any order and we will attempt to resolve them in your PSR-11 container using either the parameter type or the parameter name.

License

The MIT License (MIT). Please see the License File for more information.


All versions of php-lambda-router with dependencies

PHP Build Version
Package Version
Requires php Version >=7.3
psr/http-message Version 1.0.*
psr/http-server-handler Version 1.0.*
psr/http-server-middleware Version 1.0.*
psr/container Version ^1.0
psr/log Version ^1.1
netolabs/simple-container Version 0.2.3
php-di/invoker Version ^2.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 netolabs/php-lambda-router contains the following files

Loading the files please wait ....