Documentation


SEARCH

TABLE OF CONTENT

    1. Getting started 2. Basic concepts 3. Request dispatching 4. Context container 5. Dual MVC 6. Component model: 7. Security 8. Configuration 9. Session handling 10. I18n 11. Cache 12. Logging 13. Error handling 14. Advanced Topics 15. API reference

      Tutorials: Frequently Asqued Questions

      See also:


      Request Intercepting Filters

      Filtering requests

      Table of Contents

      Introduction

      Lion defines a complete infrastructure of modules and classes to dispatch and transform client requests in terms of application components.
      The request dispatching mechanism receives many different types of requests, which require varied types of processing. Some requests are simply forwarded to a concrete controller, while other requests must be modified, audited, or uncompressed before being further processed.

      When a request enters on Lion, it often must pass several entrance tests prior to the main processing stage. i.e. perform the user authentication, switch the language, validate certain constraints, ...

      The key to solving this problem in a flexible and unobtrusive manner is to have a simple mechanism for adding and removing processing components, in which each component completes a specific filtering action. This pattern is known as intercepting filter, which is one of the most interesting features on Lion request dispatcher system.

      Intercepting filter pattern

      Filters intercept incoming requests and outgoing responses, allowing preprocessing and post-processing. We are able to add and remove these filters unobtrusively, without requiring changes to our existing code.

      Execution life-cycle is as shown in the following sequence:

      Filters are executed before giving the control to the front controller (preFilter) and after returning the response to the client (postFilter).

      A filter is a class extending the __Filter abstract class, which contains a template method to allow the execution of code before and after giving the control to the front controller.
      A filter class has the following appearance:

      1. <?php
      2.  
      3. class MyFilter extends __Filter {
      4.     
      5.     /**
      6.      * This method is executed BEFORE giving the control to the front controller
      7.      * 
      8.      */
      9.     public function preFilter(__IRequest &$request__IResponse &$response{
      10.         ...
      11.     }
      12.  
      13.     /**
      14.      * This method is executed AFTER giving the control to the front controller
      15.      * 
      16.      */
      17.     public function postFilter(__IRequest &$request__IResponse &$response{
      18.         ...
      19.     }
      20.     
      21. }

      However, we are able to just implement one of those methods, depending on the concrete filter.
      i.e., imagine a filter to intercept when the currency parameter is present within the request in order to execute specific code before giving the control to the front controller:

      1. <?php
      2.  
      3. class CurrencyFilter extends __Filter {
      4.     
      5.     /**
      6.      * Change the currency in session by a given one if the
      7.      * currency parameter is present within the request.
      8.      *
      9.      */
      10.     public function preFilter(__IRequest &$request__IResponse &$response{
      11.         if($request->hasParameter('currency')) {
      12.             $currency_iso_code $request->getParameter('currency');
      13.             //set the currency iso code within the CurrencyManager
      14.             //singleton instance:
      15.             CurrencyManager::getInstance()->
      16.                              setCurrencyIsoCode($currency_iso_code);
      17.         }
      18.     }
      19.     
      20. }

      Intercepting filter and Routes

      Filters are associated to routes declaratively within the configuration.
      When lion resolves a route depending on the request, it recover the filter chain associated to the route and executes it.

      The way to associate a filter to a route is within the configuration, inside the filters section.

      i.e.

      1. <?xml version "1.0" standalone="yes"?>
      2.  
      3. <configuration>
      4.  
      5.   <filters>
      6.  
      7.     <filter name="myFilter" class="MyFilter">
      8.       <apply-to>
      9.         <route id="myRoute"/>
      10.       </apply-to>
      11.     </filter>  
      12.     
      13.   </filters>
      14.   
      15. </configuration>
      in this example, we are associating our filter to the myRoute route, which means that lion will execute this filter just when the request belong to the myRoute route.

      We can also associate a filter to all the available routes, for all of those filters which must be executed always.
      i.e., imagine that the currency filter of the example above must be executed for every request:

      1. <?xml version "1.0" standalone="yes"?>
      2.  
      3. <configuration>
      4.  
      5.   <filters>
      6.  
      7.     <!-- apply currency filter to all existing routes,
      8.          which means that it will be always executed -->
      9.     <filter name="currency_filter" class="CurrencyFilter">
      10.       <apply-to>
      11.         <all-routes/>
      12.       </apply-to>
      13.     </filter>  
      14.     
      15.   </filters>
      16.   
      17. </configuration>