Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Response problem while using after() in a Controller
  • RESOLVED !
    Hello everyone, I started to use FuelPHP, and it's really an amazing framework in my opionion.
    But I got a problem, my first problem. It happens both in Controller_Template and Controller classes. So, I got a normal controller with after() and before() methods. The problem is that an error happens :
    Fuel\Core\FuelException [ Error ]: Controller_Admin::action_login() or the controller after() method must return a Response object.

    My before() method contains, at the beggining, parent::before(); Same for my after() method (parent::after($response)). Now, I don't know what I can do to correct this... Here is the full code :
    <?php
    
    /**
     * The Admin Controller.
     *
     * Controller of the whole admin
     * 
     * @package  app
     * @extends  Controller
     */
    class Controller_Admin extends Controller_Template
    {
    
     public $template = 'admin/_layout';
    
     public $title = 'Administration de QMCE';
     public $data = array();
    
     /**
      * Before : Check authentification
      * 
      * @access  public
      * @return  Response
      */
     public function before()
     {
      parent::before();
    
      //Check authentification
    
      $auth = Auth::instance();
    
      if (Request::active()->action != 'login')
      {
       if(Auth::check())
       {
        if(!$auth->member('100'))
        {
         Session::set_flash('information', '<b>Information :</b> Ce formulaire vous deconnectera de votre compte actuel.');
         Response::redirect('admin/login');
        }
       }
       else
       {
        Response::redirect('admin/login');
       }
      }
    
      if (Request::active()->action == 'login')
      {
       if(Auth::check())
       {
        if($auth->member('100'))
        {
         $auth->logout();
         Session::set_flash('informationLogin', '<b>Information :</b> Vous etiez deja connecte en tant qu\'admin, vous avez ete deconnecte.');
        }
        else
        {
         $auth->logout();
         Session::set_flash('informationLogin', '<b>Information :</b> Vous etiez deja connecte en tant que non admin, vous avez ete deconnecte.');
        }
       }
      }
    
      if(Auth::check() AND $auth->member('100'))
      {
       $this->template->set_global('loggedIn', $auth->get_screen_name());
      }
      else
      {
       $this->template->set_global('loggedIn', false);
      }
     }
    
     public function after($response)
     {
      parent::after($response);
    
      //Set views infos
    
            $this->template->title = $this->title;
    
      $chemin = Uri::string();
      if(Request::active()->action == 'index') //If it's the index
      {
       $segments = Uri::segments();
       if(end($segments) != 'index')
       {
        $chemin .= '/index';
       }
      }
            $this->template->content = View::forge($chemin, $this->data);
    
            return $response;
     }
    
     /**
      * Index page : Show panel
      * 
      * @access  public
      * @return  Response
      */
     public function action_index()
     {
      $this->title .= ' > Accueil';
     }
    
     /**
      * Login method
      * 
      * @access  public
      * @return  Response
      */
     public function action_login()
     {
      $this->title .= ' > Login';
    
      if(Input::post()) //If it is submitted
      {
       $auth = Auth::instance();
    
       if($auth->login()) //Based on POST name, no need to give ID to it. If login success
       {
        if($auth->member('100')) //If it is a developer
        {
         Response::redirect('admin');
        }
        else //Else, not enough rights
        {
         Session::set_flash('error', '<b>Erreur :</b> Ce compte ne dispose pas des droits necessaires.');
        }
       }
       else //If login fails
       {
        Session::set_flash('error', '<b>Erreur :</b> Les identifiants sont incorrects.');
       }
      }
     }
    
     public function action_logout()
     {
      $auth = Auth::instance();
    
      $auth->logout(); //No need to check if user is connected, because else the before method handle and redirect to login
    
      Response::redirect('admin/login');
     }
    }
    
    
    I know that the before() method is not pretty coded but I'll correct it, I promise ! Thank you in advance. PS : I am sorry for my english, I am french and I am (only) 15 years old, so I am learning but I don't really practice in a real environment.
  • Nothing wrong with your English, and age doesn't matter. Some of us are very young, others are very old... The issue here is that your after() method recieves $response, which is NULL since your action_login() doesn't return anything. Then you call parent::after(), which will create the response object if none was present, but you don't use it's return value, so $response continues to be NULL. Use this instead: http://scrp.at/bGz p.s. if you have large blocks of code, it would be better to use [url=http://scrp.at,]http://scrp.at,[/url] and post the link here.
  • Thank you very much. For my age, It is not about my dev' level, but about my english. I started to learn 3 years ago, and I never went in a country which speaks english, so... Next time i'll use http://crp.at. I thought code coloration was enabled here, I was wrong. Again, thanks a lot !

Howdy, Stranger!

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

In this Discussion