Welcome Guest, Not a member yet? Register   Sign In
Codeigniter redirects best approach to choose
#1

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

<?php
class MY_Controller extends CI_Controller
{
    protected $data;

    function __construct()
    {
        parent::__construct();
    }

    public function _remap($method, $parameters)
    {
        if($this->view === FALSE)
        {
            redirect($this->redirect);
        }else{
            $this->load->view('my_view');
        } 

    }

Using the first method i am unable to access the second line
Reply
#2

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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB