How to pass additional url parameters to page?

Permalink
Hi folks :)

I wonder if there is any way how can I pass additional url parameter to page? (NOT single page)

Example:

I add a page to website tree - example.com/test-page
Then I want to pass var1 and var2 like this

example.com/test-page/var1/var2

Then I would like to get this var1 and var2 values in some of my custom blocks.

What approach should I follow?

I develop now on 8.0.3

Thank you

olegsUNIWEB
 
JohntheFish replied on at Permalink Reply
JohntheFish
A couple of ways I have used to get round that:
a) Hack the dispatcher to make that page behave like single pages do
b) Create a single page at the location with some dummy parameters in the view() method and code areas into the single page template. It may also be possible to code areas into the single page view, but I have never tried that.

(a) is a trick I used on a 5.4 site with several such page/paths. It worked, but wasn't convenient to maintain.

(b) Is less flexible for adding pages because they need to be coded, but easier to maintain the code and has been my preference on more recently.
olegsUNIWEB replied on at Permalink Reply
olegsUNIWEB
Would you mind to give more explanation about a) variant.

Does it mean changing core files? If I upgrade to a new version afterwards it brakes?
JohntheFish replied on at Permalink Reply
JohntheFish
(a) does involve changing core files, not just overrides. I have only tried it on 5.4 where the core was much simpler.
olegsUNIWEB replied on at Permalink Best Answer Reply
olegsUNIWEB
Found a solution. Not elegant but works. I used single page to define url.

PageController has "replace" method.

/**
     * Given either a path or a Page object, this is a shortcut to
     * 1. Grab the controller of THAT page.
     * 2. Grab the view of THAT controller
     * 3. Render that view.
     * 4. Exit – so we immediately stop all other output in the controller that
     * called render().
     *
     * @param @string|\Concrete\Core\Page\Page $var
     */
    public function replace($var)
    {
        if ($var instanceof Page) {
            $page = $var;
            $path = $var->getCollectionPath();


So I use this method in my single page class constructor to render page I need.
I parse REQUEST_URI for variables put them into $_GET array. I get them in my custom block when I need.

My custom URL is "example.com/testpage/var1/var2"
and my page in website tree has address "example.com/page"

Single page controller looks like this:

class Testpage extends PageController
{
    public function __construct()
    {
        $uri_segments = explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
        $_GET['var1'] = $uri_segments[2];
        $_GET['var1'] = $uri_segments[3];
        $this->replace('/page');
    }
}