Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

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

Extending a Controller that extends Page_Controller


Reply


2 Posts   512 Views

Avatar
copernican

Community Member, 189 Posts

1 December 2011 at 5:07am

Edited: 01/12/2011 6:04am

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.

Avatar
copernican

Community Member, 189 Posts

1 December 2011 at 7:11am

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.