CodeIgniter Forums
Codeigniter redirects best approach to choose - Printable Version

+- CodeIgniter Forums (
+-- Forum: Using CodeIgniter (
+--- Forum: Choosing CodeIgniter (
+--- Thread: Codeigniter redirects best approach to choose (/showthread.php?tid=85911)

Codeigniter redirects best approach to choose - SnowranCruzick - 12-20-2022

In the same controller i have found there are two ways to redirect.

Class Home extends mY_Controller{

    public $view;
    public $redirect;
    function first_method
        $this->view =  FALSE;
        //  $this->second_method();
        //  redirect('Home/second_method');           

    function second_method()
        echo 'Second one';
I want to know which is the best approach also i have a redirect method in the my_controller if certain conditions do not match redirect the user using redirect variable just for simplicity i am posting this

class MY_Controller extends CI_Controller
    protected $data;

    function __construct()

    public function _remap($method, $parameters)
        if($this->view === FALSE)


Using the first method i am unable to access the second line

RE: Codeigniter redirects best approach to choose - KammalJeriya - 03-23-2023

Regarding the two approaches for redirecting in the same controller, both are valid and can be used depending on your specific use case.

The first method involves calling the second method directly from the first method using the object-oriented approach. This can be useful if you need to execute some logic in the first method before redirecting to the second method. However, as you mentioned, if you uncomment the line $this->second_method();, you will not be able to redirect to the second_method() as it will be called before the redirect.

On the other hand, the second method uses the built-in redirect() function of CodeIgniter to redirect to the second_method(). This method is useful when you don't need to execute any additional logic in the first method before the redirect, and you want to avoid calling other methods before the redirect.

Regarding your MY_Controller class, it looks like you have a redirect variable that you are using to redirect the user if certain conditions are not met. This is a valid approach and can be useful for simplicity and readability of your code. However, it's important to make sure that the redirect variable is set properly before using it to redirect the user, to avoid any unexpected behavior.