The examples on this site are currently tested to work on Phalcon V3.4 and Phalcon Devtools V3.2 Some issues may arise when using later versions.

Please get in touch or post a comment below the post if you encounter a problem.

Now that we have managed to include the search parameters in the URL we just need to tidy up the URL. The following example would find all members under 21.

http://localhost/tennisClub/member/findAge/findUnderage/21

The findAgeAction which is called by this routertakes two parameters named $overUnder and $age

public function findAgeAction($overUnder,$age)
{
    //code for findAgeAction

}

The router gives us the ability to use the names of the parameters within the route. This ability to parameterise certain aspects of an action means that we can avoid having two separate actions that do almost the same thing bar some slight differences. 

Edit the file /app/config/router.php and add the following code snippet where the comment says "define your routes here"

$router->add(
    '/findMembersAgedOver/([0-9]{2})',
    [
        'controller' => 'member',
        'action'     => 'findAge',
        'overUnder'  => 'findOverAge',
	'age'		 => 1
    ]
);

The name of the route determines whether we're looking for members over or under a certain age. The second argument separated by a forward slash is a regular expression which simply describes an integer from 0-99. If the number passed was say a three digit number the regular expression would not match and therefore the route would not match. The line 'age' => 1 will assign the first matched argument in the URL to the $age parameter when calling the action. Where there are multiple arguments that need to be matched within the URL they are numbered 1,2,3..... and so on. This approach uses the named parameters "overUnder" and "age". The parameter 'overUnder' is passed a string literal which, in this case, contains 'findOverAge'. In this way we can use the route to pass arguments to the action function and control it's behaviour. The named parameter 'age' is passed 1 which is a reference to the first matched regular expression in the route. If there were additional matched regular expressions they would be 2, 3.... and so on. The regular expression here will match any 1 or 2 digit number. If something other than a one or 2 digit number is passed the route will not match.

Now add the following route:

$router->add(
    '/findMembersAgedUnder/([0-9]{2})',
    [
        'controller' => 'member',
        'action'     => 'findAge',
        'overUnder'  => 'findUnderAge',
	'age'		 => 1
    ]
);

The only difference between this route and the route above is that the 'overUnder' parameter is set to 'findUnderAge'. This will be passed to the controller and the findUnderAge method will be called on the Member model ensuring only members under the age passed are returned. Passing the search parameters in this way means one less 20 line action function within the member controller. Organising the routes in this way keeps the code neat and organised and follows the Don't Repeat Yourself principle.