Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Stopping template render, switch template file, render?
  • I started working with permissions and can't figure out the best way to basically say "You can't see this page" in Fuel. In my before(), I attempted to change the controllers $template and then call parent::after() as I figured that would bubble up and output before my action_index was called... It was called anyways. I think I'm misunderstanding how Fuel works. Here's my code:
     public function before()
     {
      parent::before();
      
      // Check login
      if(!$this->template->logged_in){
       \Response::redirect('/users/login');
      }
      
      // Check permissions
      if(!\Auth::instance()->has_access(array('admin', array('read')))){
       // $this->response->body = \View::factory('denied');
       // parent::after();
       // return FALSE;
       
       // Here's where I gave up
       die('You do not have permission to access this page.');
      }
     }
    
    
  • before() doesn't stop the main method from being executed, after() as well is just a method and doesn't do anything to stop the method from being executed. The only thing special about those 2 methods is that they're ALWAYS executed before & after the requested controller method. Now if you want to take over whether your controller method is executed you should create a router() method, that gets 2 params: the method & arguements for it. Btw: I don't get this part: if ( ! $this->template->logged_in)
    Why is your logged_in value part of your template? Why not ask Auth::check()?
  • Thank you, I'll look into this more. If this is the 3rd of a set of inherited controllers, will I need to make a router function for every level or just the deepest child? My logged_in value gets set in a parent controller like so;
    if(\Auth::check())
                {
                    $user = \Auth::instance()->get_user_id();
                    $this->user_id = $user[1];
        $this->template->username = \Auth::instance()->get_screen_name();
                    $this->template->logged_in = true;
                }
                else
                {
                    $this->template->logged_in = false;
                }
    

    Edit: I should mention I'm extending the Template controller. I think that's important. I'm basically trying to make a universal access denied class that will end the output whenever and wherever I want. Is this possible the way I have it set up?
  • Do you fully grasp how inheritance works? I can't decide for you when you need the router() method, that's up to you. Do you need it for all children? Then it should be in the uppermost parent controller class. Do you only need it in some? Then it should be in the middle or the actual called class. Also remember that when overwriting parent class methods the parent method won't get called, because you've overwritten it. If you do need the parent method called use parent::method_name(). The before() method is actually used by the Controller_Template to work, so if you overwrite that method you need to call parent::before() in the child.
  • I understand inheritance, wasn't sure if Fuel itself would break it for a level or not, or if it's pure PHP. I've still not had any luck with this. How does one simple say "stop, do this instead" ? The router() function isn't what I'm looking for. Though it will allow me to stop the script (essentially), I can't call it from say, the middle of a controller action. Going to read the docs all the way through again. Thanks for your help.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion