Silverstripe:无法使用自定义路径访问控制器中的页面字段

时间:2016-05-23 16:07:25

标签: php silverstripe

我正在使用SilverStripe 3.3.1,并设置了自定义路由来处理包含许多参数的网址。这很有效。

但是,路由规则会导致Page_Controller和模板中的页面字段和函数无法访问。任何想法如何解决这个问题?

//MyPage class
class MyPage extends Page {

    //Not accessible if route to controller specified in config.yml
    private static $db = array(
        'MyPageVar' => 'Int',
    );

     //Not accessible if route to controller specified in config.yml
     public function getMySpecialVar() {
         return $this->MyPageVar;
     } 
}

//MyPage_Controller class
class MyPage_Controller extends Page_Controller {

    private static $allowed_actions = array(
        'index',
        'detailsearch',
    );

    private static $url_handlers = array (
        'detailsearch/$Key1/$Value1/$Key2/$Value2/$Key3/$Value3/$Key4/$Value4/$Key5/$Value5' => 'detailsearch',
    );


    /**
     * UseMyPageVar() 
     * 
     * @return Boolean
     */
    public function UseMyPageVar() {
        //Empty if route to controller specified in config.yml
        Debug::show($this->MyPageVar);
        Debug::show($this->Title);
        Debug::show($this->Content);
        //Error if route to controller specified in config.yml
        Debug::show($this->getMySpecialVar());

        return true;
    }
}

MyPage.ss

<!-- This work as expected if no route is specified. -->
<!-- But all vars are empty if route is specified in config.yml -->
<p>MyVar: $MyPageVar</p>
<p>Title: $Title</p>
<p>Content: $Content</p>

config.yml中的路由规则

Director:
  rules:
    'mypage': 'MyPage_Controller'

此问题也发布在Silverstripe论坛上: http://www.silverstripe.org/community/forums/general-questions/editpost/413506

2 个答案:

答案 0 :(得分:0)

它不漂亮,但是现在我通过在Controller类中使用私有var来保存对页面的引用来解决问题。

//MyPage_Controller class
class MyPage_Controller extends Page_Controller {

    private $_page; //reference to page that's lost with custom routing        

    //ContentController uses route, which has been changed to
    //   'MyPage_Controller' by routing rule, to initialize 
    //   page reference. Can't find anything so reference
    //   not set. (set to -1)
    public function init() {
        parent::init();

        //Initialize using default route overwritten in routing rule
        //    This will break if URL segment changed in CMS  
        $route = array_search($this->URLSegment,
                        Config::inst()->get('Director', 'rules'));
        $link = str_replace($this->URLSegment, $route, $this->Link());
        $this->_page = $this->Page($link);
    }


    //Use private var to access page fields 
    public function MyPageVar() {  
        Debug::show($this->_page->MyPageVar);
    }

    //expose $Content to templates
    public function Content() {
        return $this->_page->Content;
    }

    //Can't use Title() so expose Page Title as $PageTitle
    public function PageTitle() {
        return $this->_page->Title;
    }
}

答案 1 :(得分:0)

当我查看你的代码时,我会想到三件事:

  1. &#34; mypage&#34;在config.yml中应该是MyPage_Controller上的公共方法的名称。事实上,SilverStripe无法找到名为mypage的匹配方法,而是默认调用index()

  2. 路由应该放在一个单独的routes.yml文件中,这样你就可以&#34;命名空间&#34;它在SilverStripe自己的核心路线之前或之后被调用。如果您不这样做,那么可能会导致您遇到的奇怪行为。

  3. 您是否知道可以使用?debug_request=1网址参数调试路线?请参阅:deval