Optional parameters for Route::register?

Permalink 1 user found helpful
I'm attempting to create a class extended from Controller in Concrete5 with optional parameters. I've attempted to scour both the Concrete5 and Symfony documentation, but either the answer is not there, or I'm daft when it comes to oop in PHP. So, I'm looking for a little hand-holding to get me through this process.

the tail of my application\bootstrap\app.php
Route::register(
     '/plotdata/{start}/{end}' //1 below
   , 'Application\Controller\SinglePage\getplotdata::getData' //2
   , NULL //3
   , array() //4
   , array('var1' <= NULL, 'var2' <= NULL, 'var3' <= NULL, 'var4' <= NULL) //5 
//1 rtPath | path - start and end are required params /{var1}/{var2}/{var3}/{var4} are optional,
//2 callback
//3 rtHandle
//4 requirements = array() see e.g. line below
//      array('start' => '[0-9]{4}(-[0-9]{2}){2} [ 0-2][0-9](:[0-5][0-9]){2}(.[0-9]{3})?'
//5 options | additionalAttributes
);


results in any URI with parameters beyond end producing an error. e.g.

plotdata/2015-02-01 00%3A00%3A00/2016-02-09 23%3A59%3A00/blah


produces a 404 error. It will load if I leave off the /blah at the end (albeit without setting $var1=blah), assuming getplotdata.php exists in the appropriate directory and has these lines near the head.

namespace Application\Controller\SinglePage;
class getplotdata extends Controller {


If line "1" (in the first block) is modified to read

'/plotdata/{start}/{end}/{var1}/{var2}/{var3}/{var4}'


leaving the other lines (including 5) the same, I need to parameterize each. e.g. neither
plotdata/2015-02-01 00%3A00%3A00/2016-02-09 23%3A59%3A00/blah


nor
plotdata/2015-02-01 00%3A00%3A00/2016-02-09 23%3A59%3A00
work,

but
plotdata/2015-02-01 00%3A00%3A00/2016-02-09 23%3A59%3A00/1/2/3/4
does

so, how do I specify optional parameters (with or without default values [by exposing $defaults from Symfony?]), or is there a way to produce an "overloaded" class definition in Concrete5? Do I do something like

$bob = Route::register(
    '/plotdata/{start}/{end}'
  , 'Application\Controller\SinglePage\getplotdata::getData'
); //end $bob
$bob->addDefaults(array('var1' <= NULL, 'var2' <= NULL, 'var3' <= NULL, 'var4' <= NULL));



Cross-posted to http://stackoverflow.com/q/37174465/4228193...

 
mpagel replied on at Permalink Best Answer Reply
Solved.

(tail of) app.php:
Route::register(
    '/plotdata/{start}/{end}{optparams}'
   , 'Application\Controller\SinglePage\getplotdata::getData'
 )->addRequirements(
    array('optparams' => '.*')
 )->addDefaults(
    array('optparams' => '/')
 );


code to extract the optional parameters in getplotdata.php (in appropriate directory based on namespace, etc):
$in_ar = explode('/',Request::getInstance()->get('optparams'));
array_shift($in_ar);    //get rid of "empty" parameter at start


Please see Stack Overflow link for notes and more details.
hutman replied on at Permalink Reply
hutman
Thank you for posting the solution to this, it will help many people in the future I am sure.
MrKDilkington replied on at Permalink Reply
MrKDilkington
Hi mpagel,

I want to second what hutman said. Crossposting to the forums and Stack Overflow is very useful. Thank you.