0
votes

I've got some problems with getting the template handling to work... I created a Controller (without a page, like here: http://www.ssbits.com/tutorials/2011/controllers-instead-of-pages/ )

Now I would like to have the url /package/something/something_else to map to the index function and show the PackageController_index.ss template (package is the name I gave to the slug for this controller using Director::addRules). This does not work, Silverstripe shows a 404. /package/test does work, and it shows the PackageController_test.ss template.

Strangely, /package/something/something_else DOES end up in de controller's index function, as uncommenting that line in the index function shows the text.

Anyone a clue on how to fix this or what I am doing wrong here? I am using version 3.1.4 3.1.5.

class PackageController extends Page_Controller {

    static $allowed_actions = array(
        'index',
        'test',
        '$PackageID/$PackageName/',
    );
    protected static $url_handlers = array(
        'test'                      => 'test',
        '$PackageID/$PackageName/'  => 'index',
    );

    public function init() {
        parent::init();
        // Requirements, etc. here
    }

    public function index() { 
        //echo "woohoo!;die; //uncommenting this one shows "woohoo!"
        return array();
    }

    public function test() {
        return array();
    }

}

Update: I enabled debug_request, and this is what I get when uncommenting the line in index():

Debug (line 250 of RequestHandler.php): Testing 'test' with 'test3/test4' on PackageController

Debug (line 250 of RequestHandler.php): Testing 'filter' with 'test3/test4' on PackageController

Debug (line 250 of RequestHandler.php): Testing '$PackageID/$PackageName/' with 'test3/test4' on PackageController

Debug (line 258 of RequestHandler.php): Rule '$PackageID/$PackageName/' matched to action 'index' on PackageController. Latest request params: array ( 'PackageID' => 'test3', 'PackageName' => 'test4', )

woohoo!

When I comment the line again, this happens:

Debug (line 250 of RequestHandler.php): Testing 'test' with 'test3/test3' on PackageController

Debug (line 250 of RequestHandler.php): Testing 'filter' with 'test3/test3' on PackageController

Debug (line 250 of RequestHandler.php): Testing '$PackageID/$PackageName/' with 'test3/test3' on PackageController

Debug (line 258 of RequestHandler.php): Rule '$PackageID/$PackageName/' matched to action 'index' on PackageController. Latest request params: array ( 'PackageID' => 'test3', 'PackageName' => 'test3', )

Debug (line 250 of RequestHandler.php): Testing '$Action//$ID/$OtherID' with '' on ErrorPage_Controller

Debug (line 258 of RequestHandler.php): Rule '$Action//$ID/$OtherID' matched to action 'handleAction' on ErrorPage_Controller. Latest request params: array ( 'Action' => NULL, 'ID' => NULL, 'OtherID' => NULL, )

Debug (line 184 of RequestHandler.php): Action not set; using default action method name 'index'

Update 2: I tried updating to 3.1.5, to no avail.

1

1 Answers

1
votes

To fix this remove the trailing slash from your $url_handlers '$PackageID/$PackageName/' so its just '$PackageID/$PackageName'.

You can also remove it the entire thing from $allowed_actions.

As a side note the article you're following is for an old version of SilverStripe (2.4) and Director::addRules() is now deprecated. You should instead use the YAML config system.

Director:
  rules:
    'package': 'PackageController'

More on the config system here: http://doc.silverstripe.com/framework/en/topics/configuration#setting-configuration-via-yaml-files