Skip to content

Releases: gigili/PHP-routing

v3.0.8

16 Sep 21:31
Compare
Choose a tag to compare

Fix:

  • Can not assign type of bool to type ?array
    • When accessing a route that doesn't exists this error was being thrown instead of the RouteNotFoundException exception
  • Fix GitHub CI
    • Unable to install composer dependencies

Changes:

  • Changed default branch alias for packegist

Full Changelog: v3.0.7...v3.0.8

v3.0.7

05 Sep 15:59
Compare
Choose a tag to compare

Fix: Undefined array index regex when trying to fetch route arguments from routes that don't have any arguments specified.

v3.0.6

08 Aug 18:45
Compare
Choose a tag to compare

FIX:

  • Fix the ability for middleware's to be able to access values inside of route paths when the names match

Full Changelog: v3.0.5...v3.0.6

v3.0.5

17 Jun 21:22
Compare
Choose a tag to compare
  • Fixed a bug where __invoke wasn't getting called but instead throwing a 404 callback not found exception

Full Changelog: v3.0.4...v3.0.5

v3.0.4

05 Jun 00:19
Compare
Choose a tag to compare

This is an improved version of the previous release where the support for nested dependency injection has been added and logic for dependency injection has been moved into a separate class. Some more checks have been implemented into the logic where the parameters that have default or null values, can not be instantiated or are of built in type (ex int, float, array) will be ignored.

Release notes from previous version:

Added support for dependency injection and optional use of the PHP's __invoke method, so you don't have to specify a method name as a second argument when using classes as callbacks.

Dependency injection on route classes

When using classes to handle your route callback, and those classes have some dependencies that need to be injected
through a constructor, you can specify them as an array of arguments to be injected or
let the library try to auto-inject classes.

$routes->add(
    '/demo',
    [ 
        HomeController::class, 
        'dependency_injection_test', 
        [ new InjectedClass() ] 
    ],
    Routes::GET
);

You can also use named arguments or mix and match them

$routes->add(
    '/demo',
    [ 
        HomeController::class, 
        'dependency_injection_test', 
        [ "injected_var" => new InjectedClass(), new Middleware ] 
    ],
    Routes::GET
);

Letting the library auto-inject classes into the constructor

$routes->add(
    '/demo',
    [ InjectController::class ],
    Routes::GET
);

NOTE

The library will always try to auto-inject classes (will skip ones with null as default value) if non are
provided, and you're using a class for callbacks.

Full Changelog: v3.0.3...v3.0.4

v3.0.3

03 Jun 23:13
Compare
Choose a tag to compare

Added support for dependency injection and optional use of the PHP's __invoke method, so you don't have to specify a method name as a second argument when using classes as callbacks.

Dependency injection on route classes

When using classes to handle your route callback, and those classes have some dependencies that need to be injected
through a constructor, you can specify them as an array of arguments to be injected or
let the library try to auto-inject classes.

$routes->add(
    '/demo',
    [ 
        HomeController::class, 
        'dependency_injection_test', 
        [ new InjectedClass() ] 
    ],
    Routes::GET
);

You can also use named arguments or mix and match them

$routes->add(
    '/demo',
    [ 
        HomeController::class, 
        'dependency_injection_test', 
        [ "injected_var" => new InjectedClass(), new Middleware ] 
    ],
    Routes::GET
);

Letting the library auto-inject classes into the constructor

$routes->add(
    '/demo',
    [ InjectController::class ],
    Routes::GET
);

NOTE

The library will always try to auto-inject classes (will skip ones with null as default value) if non are
provided, and you're using a class for callbacks.

v3.0.2

26 Apr 21:39
Compare
Choose a tag to compare

Added ability to send custom header values with the response class

On the Request class has been added a new method called header which accepts 2 parameters:

  • $key which can be of type string, array or object and
  • $value which is of type mixed with null being the default value

The return value of this method call is an instance of the Request class so that the other methods can be chained on it as well

Example:

$routes->add("/headers", function (Request $request) {
    $request->header("Content-type", "text/plain")
            ->header([ "foo" => "bar", "best" => "test" ])
            ->header((object) [ "X-Auth" => "Token {token-123}" ])
            ->status(201)
            ->send([ "message" => "hello" ]);
}, Routes::GET);

v3.0.1

19 Mar 20:51
Compare
Choose a tag to compare

Added append method which lets you append routes from another routes handler.

Example:

Create 2 files index.php and api.php

In the api.php:

$apiRoutes = new Routes();
$apiRoutes->prefix("/api")->get("/", function(Request  $request){})->save();

In the index.php:

require_once "api.php";
$routes = new Routes();

try{
  $routes->get("/", function(){})->add();

  if(isset($apiRoutes){
    $routes->append($apiRoutes->get_routes());
  }

  $routes->handle();
}catch(Exception $ex){}

v3.0.0

26 Oct 12:47
Compare
Choose a tag to compare

Breaking Change

The getRoutes() method got renamed to get_routes() to keep the naming conventions consistent

Changelog

Improved method chaining so that it now supports multiple chain sets of request with a single call without having to end the chain and they can also share global data (prefix(es) and middleware(s))

Example:

$routes
    ->prefix("/test")
    ->middleware([ 'decode_token' ])
    ->get("/t1", function () { }) // route would be: /test/t1
    ->get("/t2", function () { }) // route would be: /test/t2
    ->get("/t3", function () { }) // route would be: /test/t3
    ->save(false) // by passing the false argument here, we keep all the previous shared data from the chain (previous prefix(es) and middlewares)
    ->prefix("/test2")
    ->middleware([ "verify_token" ])
    ->get("/t4", function () { }) // route would be: /test/test2/t4
    ->get("/t5", function () { }) // route would be: /test/test2/t5
    ->get("/t6", function () { }) // route would be: /test/test2/t6
    ->save() // by not passing the false argument here, we are removing all shared data from the previous chains (previous prefix(es) and middlewares)
    ->prefix("/test3")
    ->middleware([ "verify_token" ])
    ->get("/t7", function () { }) // route would be: /test3/t7
    ->get("/t8", function () { }) // route would be: /test3/t8
    ->get("/t9", function () { }) // route would be: /test3/t9
    ->add(); //using save or add at the end makes the chaining stop and allows for other independent routes to be added

v2.0.7

02 Oct 19:07
5f3e1a0
Compare
Choose a tag to compare

You can now pass arguments to middleware methods
When working with middleware methods you can also pass them arguments if you need to

$routes
    ->middleware([
        'test_middleware',
        'has_roles' => 'admin,user',
        [ Middleware::class, 'test_method' ],
        [ Middleware::class, 'has_role', 'Admin', 'Moderator', [ 'User', 'Bot' ] ],
    ])
    ->add('/test', function (Request $request) {
        $request->send([ 'msg' => 'testing' ]);
    });

Every middleware function can also accept an argument of type Gac\Routing\Request at any position as long as it has the proper type specified.