xp-forge / frontend

Web frontends
1 stars 1 forks source link

Delegates #5

Closed thekid closed 6 years ago

thekid commented 6 years ago

Adds shorthand alternative to manually entering all routes:

use web\Application;
use actions\{Users, Blogs};

class Service extends Application {
  public function routes() {
    $templates= ...
    return [
      // Route /users to methods inside Users class, and /blogs to Blogs, and so forth...
      '/users' => new Frontend(new Users(), $templates),
      '/blogs' => new Frontend(new Blogs(), $templates),

      // Sets up routes for all instantiable classes inside the "actions" package
      '/' => new Frontend(new ClassesIn('actions'), $templates),
    ];
  }
}
thekid commented 6 years ago

To integrate well with xp-forge/inject, the ClassesIn constructor accepts an optional creation function. Usage example:

$inject= new Injector(...);

$frontend= new Frontend(new ClassesIn('actions', [$inject, 'get']), $templates);