-
Notifications
You must be signed in to change notification settings - Fork 1
Attrib: RouteHandler
Bob Magic II edited this page Feb 8, 2023
·
5 revisions
Classes that support request handling should extend the Route
base class. Then methods may be annotated using the RouteHandler
attribute to mark them as willing to answer requests.
#[Nether\Avenue\Meta\RouteHandler(string $Path, string $Domain, string $Verb)]
use Nether\Avenue\Response;
use Nether\Avenue\Meta\RouteHandler;
class Home
extends Nether\Avenue\Route {
// answer index requests with the home page.
#[RouteHandler('/index')]
public function
Index():
void {
echo 'Home Page';
return;
}
// answer index requests on a specific domain with its own home page.
#[RouteHandler('/index', 'pegasusgate.net')]
public function
Index():
void {
echo 'Home Page for PegasusGate';
return;
}
// consume a post request and spit out json.
#[RouteHandler('/submit', Verb: 'POST')]
public function
Submit():
void {
$this->Response->SetContentType(Response::ContentTypeJSON);
echo json_encode([
'Error' => 0,
'Message' => 'OK'
]);
return;
}
}
If methods ask for an $ExtraData
argument they will be given a Common\Datastore
that exists for the duration of the request. It must come after any path slot arguments.
class Home
extends Nether\Avenue\Route {
#[RouteHandler('/news/:ArticleID:')]
public function
Article(int $ArticleID, Nether\Common\Datastore $ExtraData):
void {
echo 'News Article View';
return;
}
}
If a WillAnswerRequest method adds data to $ExtraData, you can ask for the keys it adds to be expanded by including them as arguments on the final request handler as well.
class Home
extends Nether\Avenue\Route {
#[RouteHandler('/news/:ArticleID:')]
#[WillConfirmAnswerRequest]
public function
Article(int $ArticleID, News\Article $Article):
void {
echo 'News Article View';
return;
}
public function
ArticleWillAnswerRequest(int $ArticleID, Nether\Common\Datastore $ExtraData):
int {
$Article = News\Article::GetByID($ArticleID);
if(!$Article)
return Nether\Avenue\Response::CodeNotFound;
$ExtraData['Article'] = $Article;
return Nether\Avenue\Response::CodeOK;
}
}