CodeIgniter Forums

Full Version: Tiny tweak that's saved me a lot of time
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]mattalexx[/eluser]
I extend the core controller and add a method called internal_redirect. If you only use this everywhere you are redirecting to another method within the same controller, you will make your controller a lot more portable and rename-able.

Here 'tis:

Code:
class MY_Controller extends Controller {

   function MY_Controller() {
      parent::Controller();
   }

   function internal_redirect($url) {
      redirect($this->uri->segment(1).'/'.$url);
   }

}

El Forum

[eluser]Colin Williams[/eluser]
Quote:If you only use this everywhere you are redirecting to another method within the same controller

Another method in the same controller? Um, why not just call $this->other_method(), and exit if needed? I do this all the time.

Code:
class User extends Controller {

  function User()
  {
    parent::Controller();
    $this->load->library('users');
  }

  function index()
  {
    if ($this->users->check_access())
    {
      $this->dashboard();
    }
    else
    {
      $this->login();
    }
  }

  function logout()
  {
    $this->users->logout();
    $this->index();
  }

}

El Forum

[eluser]mattalexx[/eluser]
Boy do I feel dumb. Well, thanks for that.

El Forum

[eluser]Colin Williams[/eluser]
Smile This is why I like to encourage writing documentation as you build because you catch yourself writing something like "call another method in the same controller" and you slap your forehead and go about it in a more clear way.

El Forum

[eluser]mattalexx[/eluser]
Actually come to think about it, Controller::internal_redirect does have a place, in that it resets the POST and changes the URL, which is useful after say processing a form.