Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
global controller question like cakephp has
  • Hi, Does Fuel have a "global" controller that all request will go through before the called controller? Basically i want to valid sessions and user permission in this "global" controller. Simular to Cakephp's app_controller. Thanks,
    Trav.
  • Controllers can extend other controllers, so the way I do it is I have a Controller_Session that extends Controller
    class Controller_Session extends Controller {
    
        /**
         * @var string page template
         */
        public $template = 'maintemplate';
        /**
         * @var boolean auto render template
         * */
        public $auto_render = true;
        public $user_id;
    
        // Load the template and create the $this->template object
        public function before($data = null) {
    
            $session = Session::instance();
    
            $authenticated = Session::get('authenticated');
            $this->user_id = Session::get('user_id');
    
            if ($authenticated != true || !$this->user_id) {
                Response::redirect('welcome');
                return false;
            }
    
            if ($this->auto_render === true) {
                // Load the template
                $this->template = \View::factory($this->template);
    
                // Set the data to the template if provided
                $data and $this->template->set_global($data);
            }
    
            return parent::before();
        }
    
        // After contorller method has run output the template
        public function after() {
            if ($this->auto_render === true) {
                $this->response->body($this->template);
            }
    
            return parent::after();
        }
    
    }
    

    Then all my other controllers once logged in will extend the session controller
  • thanks bperin, i thought that may be the case. Question: can you extend a controller that is an extenstion of an other controller? eg:
    Controller_Session extends Controller
    -> Controller_Admin extends Controller_Session
    -> Controller_User extends Controller_Admin
  • Trav Johnston wrote on Thursday 21st of July 2011:
    thanks bperin, i thought that may be the case. Question: can you extend a controller that is an extenstion of an other controller? eg:
    Controller_Session extends Controller
    -> Controller_Admin extends Controller_Session
    -> Controller_User extends Controller_Admin
    I'd ask: how would Fuel stop you? It's still PHP and while Fuel does a little stuff behind the scenes, a class that extends another class couldn't be prevented and why would we ever want to? Checkout the docs in General on classes, core classes and the controllers.
  • @trav, yes you can have multiple controller extensions... But everyone is right, those docs will help a lot. Question though, if you're doing all that Session work for authentication, why not just extend the Auth package?
  • Trav Johnston wrote on Thursday 21st of July 2011:
    Question: can you extend a controller that is an extenstion of an other controller? eg:
    Controller_Session extends Controller
    -> Controller_Admin extends Controller_Session
    -> Controller_User extends Controller_Admin

    Did you try? That will work perfectly but you might want to re-think things. User extends Admin? Huh? Either way, try stuff before asking :)
  • sorry guys, i have been away and have not had a chance to try this out. in future i will test before post. cheers.

Howdy, Stranger!

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

In this Discussion