SilverStripe:404模板,但达到控制器功能

时间:2014-05-20 09:36:54

标签: url-routing silverstripe

我在使模板处理工作方面遇到了一些问题......我创建了一个Controller(没有页面,就像这里:http://www.ssbits.com/tutorials/2011/controllers-instead-of-pages/

现在我想将url / package / something / something_else映射到索引函数并显示PackageController_index.ss模板(包是我使用Director :: addRules为此控制器提供的slug的名称)。这不起作用,Silverstripe显示404. / package / test确实有效,它显示了PackageController_test.ss模板。

奇怪的是,/ package / something / something_else最终会出现在de controller的索引函数中,因为在index函数中取消注释该行会显示文本。

任何人都知道如何解决这个或我在这里做错了什么?我使用的是版本 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();
    }

}

更新:我启用了debug_request,这是我在取消注释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!

当我再次评论该行时,会发生这种情况:

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'

更新2:我尝试更新到3.1.5,但没有用。

1 个答案:

答案 0 :(得分:1)

要解决这个问题,请从$ url_handlers'$ PackageID / $ PackageName /'中删除尾部斜杠,使其只是'$ PackageID / $ PackageName'。

您也可以从$ allowed_actions中删除整个内容。

作为旁注,您所关注的文章是旧版SilverStripe(2.4),现在不推荐使用Director :: addRules()。您应该使用YAML配置系统。

Director:
  rules:
    'package': 'PackageController'

有关配置系统的更多信息:http://doc.silverstripe.com/framework/en/topics/configuration#setting-configuration-via-yaml-files