【问题标题】:SilverStripe: 404 on template, but reaches controller functionSilverStripe:模板上的 404,但达到控制器功能
【发布时间】:2014-05-20 09:36:54
【问题描述】:

我在让模板处理工作时遇到了一些问题...我创建了一个控制器(没有页面,比如这里:http://www.ssbits.com/tutorials/2011/controllers-instead-of-pages/

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

奇怪的是,/package/something/something_else 最终会出现在 de 控制器的 index 函数中,因为在 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,但无济于事。

【问题讨论】:

    标签: url-routing silverstripe


    【解决方案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

    【讨论】:

    • 谢谢!就是这样。该死的,不知道为什么我没有自己尝试。考虑到您的旁注,再次感谢。我会调查的。
    猜你喜欢
    • 1970-01-01
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-04
    • 1970-01-01
    • 2015-10-07
    相关资源
    最近更新 更多