Search code examples
phpurl-routingconcrete5

How to read url-parameters in the controllers on_start() methode?


In my current Contrete5 project, I have a single page, that takes one url parameter. So the urls look like this:

domain.com/[singlepagename]/[id]

and the controller has a corresponding view function:

function view($id) { ... }

now i need access to the id in the on_start() function. but function on_start($id) { ... } throws an error and $this->get() returns an empty array.

any idea how i can get url parts in on_start()?


Solution

  • I've encountered this before... the problem is that C5's Request object is not initialized until after the various package controllers' on_start events are fired. I believe the only solution that people came up with was to manually initialize the Request class yourself in you package controller's on_start function. If you look at the dispatcher.php file, you'll see the following chunk of code around line #129 (in Concrete5.6.0.1):

    // figure out where we need to go
    $req = Request::get();
    if ($req->getRequestCollectionPath() != '') {
        if (ENABLE_LEGACY_CONTROLLER_URLS) {
            $c = Page::getByPath($req->getRequestCollectionPath(), 'ACTIVE');       
        } else {
            $c = $req->getRequestedPage();
        }
    } else {
        $c = Page::getByID($req->getRequestCollectionID(), 'ACTIVE');
    }
    
    $req = Request::get();
    $req->setCurrentPage($c);
    
    if ($c->isError()) {
        // if we've gotten an error getting information about this particular collection
        // than we load up the Content class, and get prepared to fire away
        switch($c->getError()) {
            case COLLECTION_NOT_FOUND:
                $v = View::getInstance();
                $v->render('/page_not_found');
                break;
        }
    }
    

    ...so I think you can copy that all into your package controller's on_start function, then you have the $req object to get your path info and variables from.

    NOTE: I copied that code from Concrete5.6.0.1. If you're using a different version of the system, you should not just take what I pasted above, but instead copy the appropriate code yourself from the /concrete/dispatcher.php file