GithubHelp home page GithubHelp logo

pmall / handlers-action Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 8 KB

The action request handler factory component of the ellipse framework

Home Page: https://github.com/ellipsephp/handlers-action

License: MIT License

PHP 100.00%
psr-15 request-handler factory controller action

handlers-action's Introduction

Action request handler factory

This package provides an action request handler factory which can be used to produce Psr-15 request handler from an action string.

Require php >= 7.1

Installation composer require ellipse/handlers-action

Run tests ./vendor/bin/peridot tests

Using the action request handler factory

Action strings are string containing a class name and method name spaced by @. A comma separated list of request attributes to inject can be added, spaced by :.

Example: 'App\Controller@index' or 'App\Controller@show:category_id,post_id'.

Many things to note:

  • The action's controller instance is constructed using ellipse/container auto-wiring feature. See auto-wiring documentation for more details.
  • The action's method is called using ellipse/container callable dependency injection feature. See callable dependency injection documentation for more details.
  • When one parameter of the action's controller constructor or action's method is type hinted as Psr\Http\Message\ServerRequestInterface, the request currently processed by the request handler is injected
  • The non type hinted parameters of the action's controller constructor or action's method will be the specified request attributes, in the order they are listed
  • When a 'router.controllers.namespace' alias is registered in the container, its value will be prepended to all controller class names
<?php

namespace App;

class SomeController
{
    private $response;

    public function __construct(ResponseFactory $response)
    {
        // Dependencies are automatically injected.

        $this->response = $response;
    }

    public function index(ServerRequestInterface $request, $request_attribute)
    {
        // $request is the request available at the time the middleware is being processed.
        // $request_attribute is the first url attribute passed to the Action instance.

        // some processing ...

        // returns a response.
        return $this->response->create();
    }
}
<?php

namespace App;

use Simplex\Container;

use Ellipse\Handlers\ActionFactory;

// Get a Psr-11 container.
$container = new Container;

// Register a base controllers namespace.
$container->set('router.controllers.namespace', 'App');

// Get an action factory using this container.
$factory = new ActionFactory($container);

// Create a request handler based on SomeController's index method.
$handler = $factory('SomeController@index:request_attribute');

handlers-action's People

Contributors

pmall avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.