Jump to:

23379 Posts in 18298 Topics by 2867 members

General Questions

SilverStripe Forums » General Questions » Extending a Controller that extends Page_Controller

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 460 Views
  • IOTI
    Avatar
    Community Member
    189 Posts

    Extending a Controller that extends Page_Controller Link to this post

    Hi,

    Perhaps I'm missing something obvious, but I'm trying to extend a Controller class that extends Page_Controller. So for example

    Class ForumHolder_Controller extends Page_Controller{
    function init(){
    parent::init();
    }
    }

    and then I have:

    class ForumHolderExtension_Controller extends ForumHolder_Controller{
    function init(){
    parent::init();
    Requirements::CSS('themes/'. SSViewer::current_theme() .'/css/test.css');
    }
    }

    my test.css will not be included on the page. Is this possible?

    In the end what I am trying to accomplish is in my ForumHolderExtension_Controller I want to add some Permission:: checks to the init function. Is there an alternative approach to solving this?

    Thank you.

  • IOTI
    Avatar
    Community Member
    189 Posts

    Re: Extending a Controller that extends Page_Controller Link to this post

    I managed to get something working, but I'm not sure if this is the best approach - here is my code.

    class ForumHolder_Controller extends Page_Controller{
       function init(){
          parent::init();
          
          $allowed = true;
          
          $this->extend('updateInit', $allowed);
          
          if($allowed){
             //do nothing
          } else {
             Security::permissionFailure($this);      
          }
       }
    }

    and the extension:

    class ForumHolderExtension_Controller extends Extension{   
       
       
       function updateInit(&$allowed){
          //checking permission using custom PlanPermission class
          $allowed = PlanPermission::check('FORUM_VIEW');   
          return $allowed;
       }      
       
    }

    and of course in my _config.php I have:

    Object::extension('ForumHolder_Controller', 'ForumHolderExtension_Controller');

    This works, if I'm signed in as a member with the proper permissions I can view the page, and members without the proper permissions are blocked. One problem though is that if i'm signed in as a member without proper permissions I get a white screen, but if I'm not signed in I get the SS login form.

    460 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.