ActiveWAFL Crash Course

The WAFL Cones Ice Cream Shop, Version 1.0

5.1 Create the server-side Controllers

All of our pages will have server-side controllers with at least a default action.
Let's start with the landing page (which should already exist from the initial skeleton application).

Edit the file WaflCones/Controllers/LandingPage.php and replace it's contents with the following code.

Server-side controller for the main landing page, WaflCones/Controllers/LandingPage.php.
  1. <?php
  2.  
  3. namespace WaflCones\Controllers;
  4.  
  5. use DblEj\Application\IMvcWebApplication,
  6.     DblEj\Communication\Http\Request,
  7.     DblEj\Mvc\ControllerBase,
  8.     DblEj\Mvc\SitePageResponse;
  9.  
  10. /**
  11.  * The LandingPage server-side controller.
  12.  * All server-side controllers extend ControllerBase.
  13.  */
  14. class LandingPage extends ControllerBase
  15. {
  16.     /**
  17.      * The default action for the landing page.
  18.      * This action is called when the user browses to / or /LandingPage.
  19.      *
  20.      * The convenience method createResponseFromRequest is used to:
  21.      * -use the site structure to determine which page is being requested.
  22.      * -return a simple SitePageResponse (which contains a \DblEj\Mvc\SitePage that
  23.      *  points to the LandingPage Presentation Template).
  24.      *
  25.      * @param Request $request
  26.      * @param IMvcWebApplication $app
  27.      *
  28.      * @return SitePageResponse
  29.      */
  30.     public function DefaultAction(Request $request, IMvcWebApplication $app)
  31.     {
  32.         return $this->createResponseFromRequest($request, $app);
  33.     }
  34. }
  35. ?>

We'll go in the same order that we created the presentation in.
So, next we'll do the code for the new sale controller and the administration menu controller.

Their code follows. Note that you'll need to create the WaflCones/Controllers/Administration directory.

WaflCones/Controllers/Sale.php: server-side controller for new sale page.
  1. <?php
  2.  
  3. namespace WaflCones\Controllers;
  4.  
  5. use DblEj\Application\IMvcWebApplication,
  6.     DblEj\Communication\Http\Request,
  7.     DblEj\Data\ArrayModel,
  8.     DblEj\Mvc\ControllerBase,
  9.     DblEj\Mvc\SitePageResponse,
  10.     WaflCones\FunctionalModel\Flavor;
  11.  
  12. class Sale extends ControllerBase
  13. {
  14.     /**
  15.      * The default action for the Sale controller.
  16.      *
  17.      * @param Request $request
  18.      * @param IMvcWebApplication $app
  19.      *
  20.      * @return SitePageResponse
  21.      */
  22.     public function DefaultAction(Request $request, IMvcWebApplication $app)
  23.     {
  24.         if (\Wafl\Core::$CURRENT_USER->Get_UserId())
  25.         {
  26.             $allFlavors = Flavor::Filter(null,"Title");
  27.             $model = new ArrayModel(["Flavors"=>$allFlavors]);
  28.             $response = $this->createResponseFromRequest($request, $app, $model);
  29.         } else {
  30.             $response = $this->createRedirectUrlResponse("/");
  31.         }
  32.         return $response;
  33.     }
  34. }
  35. ?>
WaflCones/Controllers/Administration/LandingPage.php: server-side controller for administration menu landing page.
  1. <?php
  2.  
  3. namespace WaflCones\Controllers\Administration;
  4.  
  5. use DblEj\Application\IMvcWebApplication,
  6.     DblEj\Communication\Http\Request,
  7.     DblEj\Mvc\ControllerBase;
  8.  
  9. class LandingPage extends ControllerBase
  10. {
  11.     public function DefaultAction(Request $request, IMvcWebApplication $app)
  12.     {
  13.         if (\Wafl\Core::$CURRENT_USER->Get_UserId())
  14.         {
  15.             $response = $this->createResponseFromRequest($request, $app);
  16.         } else {
  17.             $response = $this->createRedirectUrlResponse("/");
  18.         }        
  19.         return $response;
  20.     }
  21. }
  22. ?>

See your progress

You've now done enough to get a sneak peak at your progress.

Open up a web browser and go to d.waflcones.tld (or whatever domain you setup at the beginning of the course).
If everything has gone well, you should see a screen like the following.

The Wafl Cones landing page

Congratulations!
We've covered a lot of ground. All that's left is the client-side controllers and an API Handler.

Next section: Client side controllers