2017 © Pedro Peláez
 

library hope

Hope microframework

image

pedrofaria/hope

Hope microframework

  • Friday, April 14, 2017
  • by pedrofaria
  • Repository
  • 1 Watchers
  • 3 Stars
  • 2 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Hope

Hope is a very lightweight PHP microframework., (*1)

Check a example of Hope application, (*2)

Installation

TODO!, (*3)


Usage

Very simple usage of Hope microframework., (*4)

$app = new Hope\Application;

$app->setRoute(function(Hope\Router\RouteCollector $route) {
    $route->add('GET', '/ping', function() {
        return ['data' => 'pong'];
    });
});

$app->bootstrap();
$app->run();

And that's it!, (*5)

Routes

All routes are defined with the method setRoute. You can pass a closure (above) or a file name with the route definition., (*6)

Map all your routes to your actions., (*7)

$route->add(METHOD, URI, ACTION);

If you want to map more than one method to the same action, use:, (*8)

$route->add([METHOD1, METHOD2], URI, ACTION);

You can define parameters on URI using braces. Example:, (*9)

/recipes/{id}

Now the parameter id will be passed to your controller action. You can also specify an regular expression to your parameter to have a better controls of your URIs, example:, (*10)

/recipes/{id:\d+}

Your actions should be defined as string, array or closures. example:, (*11)

$route->add('GET', '/recipes', 'App\Controller\RecipesController::index');
$route->add('GET', '/recipes', ['App\Controller\RecipesController', 'index']);
$route->add('GET', '/ping', function () {
    return 'pong';
};

Additionally, you can specify routes inside of a group. All routes defined inside a group will have a common prefix., (*12)

For example, defining your routes as:, (*13)

$route->addGroup('/admin', function (Hope\Router\RouteCollector $route) {
    $route->add('GET', '/do-something', 'handler');
    $route->add('GET', '/do-another-thing', 'handler');
    $route->add('GET', '/do-something-else', 'handler');
});

Will have the same result as:, (*14)

$route->add('GET', '/admin/do-something', 'handler');
$route->add('GET', '/admin/do-another-thing', 'handler');
$route->add('GET', '/admin/do-something-else', 'handler');

Nested groups are also supported, in which case the prefixes of all the nested groups are combined., (*15)

For more informations, access https://github.com/pedrofaria/router, (*16)

Providers

You can extends or replace some funcionality of Hope using the Dependency Injection system. Just two steps and every thing is running., (*17)

1) Create your own Provider., (*18)

<?php
namespace App\Providers;

class MyRequestProvider implements Hope\Contracts\ProviderInterface
{
    public static function register(Hope\Application $app)
    {
        $app->bind('Hope\Http\Request', function() {
            return new App\Http\MyRequest();
        });
    }
}

2) Add it with the method addExternalProviders., (*19)

$app->addExternalProviders([
    App\Providers\YourProviderClassProvider::class,
]);

CAUTION: This method must stay before bootstrap()., (*20)

Running Tests

Codeception was chosen on this project to support all tests. To run, use the command below:, (*21)

$ vendor/bin/codecept run unit, (*22)

If you want run with code coverage, use the follow command and the HTML report will be available at tests/_output/coverage., (*23)

$ vendor/bin/codecept run unit --coverage --coverage-html, (*24)

The Versions