0

I am having some difficulties rerouting to a dynamic URL from within my controller.

in routes.ini

GET /admin/profiles/patient/@patientId/insert-report = Admin->createReport

in the controller Admin.php, in method createReport():

$patientId = $f3->get('PARAMS.patientId');

My attempt (in Admin.php):

$f3->reroute('admin/profiles/patient/' . echo (string)$patientId . '/insert-report');

Question: How to reroute to the same URL (where some error messages will be displayed) without changing completely the routing, that is attaching patientId as a URL query parameter ?

Thanks, K.

Kami
  • 1
  • 2

1 Answers1

2

The echo statement is not needed to concatenate strings:

$f3->reroute('admin/profiles/patient/' . $patientId . '/insert-report');

Here are 3 other ways to get the same result:

1) build the URL from the current pattern

(useful for rerouting to the same route with a different parameter)

// controller
$url=$f3->build($f3->PATTERN,['patientId'=>$patientId]);
$f3->reroute($url);

2) reroute to the same pattern, same parameters

(useful for rerouting from POST/PUT/DELETE to GET of the same URL)

// controller
$f3->reroute();

3) build the URL from a named route

(useful for rerouting to a different route)

;config file
GET @create_report: /admin/profiles/patient/@patientId/insert-report = Admin->createReport
// controller
$url=$f3->alias('create_report',['patientId'=>$patientId']);
$f3->reroute($url);

or shorthand syntax:

// controller
$f3->reroute(['create_report',['patientId'=>$patientId']]);
xfra35
  • 3,833
  • 20
  • 23