xp-forge / web

Web applications for the XP Framework
2 stars 1 forks source link

Actions from classes #24

Closed thekid closed 6 years ago

thekid commented 6 years ago

Introduce the following as web.handler.Action implementation:

class ActionClassesIn implements Actions {
  public function __construct(private $package, private $base= '/') { }
  public function from($req) {
    $action= 'Index';
    sscanf($req->uri()->path(), $this->base.'%s', $action);
    $class= '';
    foreach (explode('/', $action) as $segment) {
      $class.= ucfirst($segment);
    }
    return $this->package->loadClass($class.'Action')->newInstance($action);
  }
}

Also make web.handler.Action an abstract base class, with its name method returning whatever was passed to its constructor.

thekid commented 6 years ago

Not flexible for parameterized URLs such as /by/date/2006 or /blog/article/1345-slug.

Idea:

<<action('/')>>
class Home implements \web\handler\Action {

  public function name() ==> 'home';

  public function perform($req, $res) {
    return [];
  }
}

<<action('/page/{number}')>>
class Page implements \web\handler\Action {

  public function name() ==> 'page';

  public function perform($req, $res) {
    return ['page' => $req->value('number')];
  }
}

class Search implements \web\handler\Action {

  public function name() ==> 'search';

  public function perform($req, $res) {
    return ['query' => $req->param('q')];
  }
}
thekid commented 6 years ago

What about separating GET and POST requests?

https://fatfreeframework.com/3.6/routing-engine#ReST:RepresentationalStateTransfer

class Item {
    function get() {}
    function post() {}
    function put() {}
    function delete() {}
}

$f3=require('lib/base.php');
$f3->map('/cart/@item','Item');
$f3->run();
thekid commented 6 years ago

The Actions class should be something castable to a Routing instance.

thekid commented 6 years ago

Usecases

Photoblog

REST API

...with methods for CRUD:

thekid commented 6 years ago

See #33 - this should be part of its own library.