Route Parameters for single_pages

Permalink 2 users found helpful
I'm likely missing something obvious here but I'm not seeing a way to register a route for a single page with optional parameters. Such that route /singlepage can passively accept route /singlepage/{param1?}/{param2?}/

We used to simply be able to define optional vars within any given single_page's methods specifically: public function actionMethod($var1=null,$var2=null){...}

This does not work now obviously.

Anyone have any ideas on how to set this up in a route register? Or is this simply a feature missing from dispatcher? A bug?


View Replies:
DavidMIRV replied on at Permalink Reply
You ever figure this out ? I'm wondering same thing..
andrew replied on at Permalink Reply
Hmm, this should be working as it always has been. I would think we'd see more broken dashboard pages if it weren't.
DavidMIRV replied on at Permalink Reply
Oh it works for ajax'y & restyurls..
My personal implementation I'm trying to render a single page for a given URL .
(Trying to remap the profile urls)

Thus far the only way I've found to do this is to emulate the code in Application::dispatch
Route::register('/{username}', function ($username) {
   $request = Request::getInstance();
   $request->setCurrentPage(Page::getByPath('/members/' . $username));
   $collection = Route::getList();
   $context = new \Symfony\Component\Routing\RequestContext();
   $matcher = new UrlMatcher($collection, $context);
   $path = rtrim($request->getPathInfo(), '/') . '/';
   $matched = $matcher->match($path);
   $route = $collection->get($matched['_route']);
   // nope! this loops! $response = Route::execute($route, $matched);
   $callback = new DispatcherRouteCallback('dispatcher');
   $response = $callback->execute($request);

If there's a better way to do this , please enlighten me with the knowledge!

**Edit I am still having trouble getting it to accept and pass on the $username route param to the controller view()
DavidMIRV replied on at Permalink Reply
On another note if you just try SinglePageController::View you get an error about __construct as it doesn't go through the usual page-controller creation process..
DavidMIRV replied on at Permalink Reply
Well I sort of got this working by effectively turning C5 into a MVVR (sort of)
by :

1. Overriding the core Request Class and setting the alias to my Application specific instance which just adds a ::setInstance() Method. (Be nice if it were in a DI container and could just replace it but this works)
2. Create a new request and setInstance
3. encapsulating the code from bootstrap/start.php to reroute/render the request

Route::register('/{username}', function ($username) {
   global $cms,$c;
   if ($username) {
      /** @var Request $request */
      $r = Request::create('/members/test');
      $c = Page::getByPath('/members');
      $c = $r->setCurrentPage($c);
      $response = $cms->dispatch($r);
      return $response->send();

be nice if there was a route for the 404 page that I could just override though currently this is grabbing all requests.. It needs conditional logic on detecting existing paths..

Actually .. looking at ->dispatch I think I can just throw an exception and bounce out of the route.. (if username doesn't exist then throw new ResourceNotFoundException (per Application.php:341)
DavidMIRV replied on at Permalink Reply
I put the final details of this working solution up at incase anyone searches for this..
jero replied on at Permalink Reply
Maybe I'm a bit late to the party, but it appears all you need do is add the parameters into the view() function's argument list:

namespace Concrete\Package\Jez\Controller\SinglePage;
use Concrete\Core\Page\Controller\PageController;
class Products extends PageController {
   public function view ($mfr=false,$colour=false) {

There is no need to add any special routing logic - it just works.

requests for /products/acer/red will run the view() method, and the $mfr and $colour arguments are set to "acer" and "red" respectively.

The *critical* thing is to declare the parameters in the function definition. Leaving them out results in a page not found error.