Request Handlers

Routers are used to route incoming requests to the appropriate handler. The default Routers will look at the requested URL and try to find a handler that matches the requested file. The Router looks for the handler in the ApplicationNamespace/Logic folder.

So, for example, if there is a request /Contact-Us the application would look for a server-side Request Handler in a file name ApplicationNamespace/Logic/Contact-Us.php.

If a matching file is found, and it contains a class that extends the DblEj\Communication\Http\RequestHandlerBase class, then the class is instantiated and the request is passed to a method named HandleHttpRequest in that class.

The response from the request handler will be returned to the application which will then send an approriate response to the client. After that, the corresponding Javascript logic script will be triggered on the client-side.

.

Server Side Request Handler

Server side request handler synopsis
  1. <?php
  2. namespace ApplicationNamespace\Logic;
  3.  
  4. use DblEj\Communication\Http\RequestHandlerBase;
  5.  
  6. class ExamplePage
  7. extends RequestHandlerBase
  8. {
  9.     public function HandleHttpRequest(\DblEj\Communication\IRequest $request, $requestString, \DblEj\Application\IApplication $app)
  10.     {
  11.         //Find a Site Page that is mapped to a Logic handler named ExamplePage
  12.         $sitePage = $app->GetSitePageByRequest($request);
  13.  
  14.         //Attach data to be used by the template renderer
  15.         sitePage->AddModel(new ArrayModel(["SOME_VARIABLE"=>"SOME_VALUE", "SOME_VARIABLE"=>"SOME_VALUE"]));
  16.        
  17.         return new \DblEj\Communication\Http\SitePageResponse($sitePage);
  18.     }
  19. }
  20. ?>

Client Side Request Handler

The client-side logic script is "included" on-the-fly and must be self-executing. Usually you'll have a single Start block that contains the code which is to be executed on page load.

Client-side Logic Handler example
  1. Start(function() {
  2.  
  3.     //Handle the click of the "Get Details" button
  4.     $("GetDetailsButton").AddClickHandler(function(e)
  5.     {
  6.         alert("User clicked some button");
  7.     });
  8.  
  9.     //Handle event when the URL contents after the hash-mark change
  10.     DblEj.EventHandling.Events.AddHandler(window, "hashchange", function(e)
  11.     {
  12.         alert("User navigated to in-page anchor");
  13.     });
  14.  
  15. });