Knownow
Knownow

Reputation: 385

Codeigniter redirect() not working as intended

I have started working with Codeigniter. So, I have two controller Tasks and Project. In my Task Controller I have a function to edit an existing task and after the task is edited I want to redirect to the Projects controller's display method which takes a parameter as the project_id associated with the task.

Here is my task controller:

 public function edit($task_id,$task_project_id){
    $this->form_validation->set_rules('task_name', 'Task Name','trim|required');
    $this->form_validation->set_rules('task_description', 'Task Description','trim|required');
    if ($this->form_validation->run() == FALSE){
        $result=$this->task_model->get_task($task_id);
        $data['task_name']=$result->task_name;
        $data['due_on']=$result->due_on;
        $data['task_description']=$result->task_description;
        $data['id']=$task_id;
        $data['task_project_id']=$task_project_id;
        $data['main_view']="tasks/edit_task";
        $this->load->view('layout/user', $data);
    } else { 
            $data['task_project_id']=$task_project_id;
            $data['task_name']=$this->input->post('task_name');
            $data['due_on']=$this->input->post('due_on');
            $data['task_description']=$this->input->post('task_description');
            $data['id']=$task_id;
            if ($this->task_model->edit($data)){
            $this->session->set_flashdata('task_edited' ,'Task Edited Succesfully');
            redirect(projects/display/$task_project_id); THIS SEEMS TO BE THE PROBLEM .
        }else{
            $this->session->set_flashdata('task_edited_failure' ,'Task Not Edited Succesfully');
            redirect(projects/edit);
        }
    }
}

Here is my Projects Controller with its index and display function :

    public function index()
{
        $data['projects']=$this->projects_model->get_projects();
        $data['main_view']="projects/home_view";
        $this->load->view('layout/user.php',$data);
}
public function display($project_id){
        $data['task']=$this->task_model->get_tasks($project_id);
        $data['project']=$this->projects_model->get_project($project_id);
        $data['main_view']="projects/display";
        $this->load->view('layout/user.php',$data);
}

So, in my task controller I am trying to redirect to the projects controller's display method using redirect(projects/display/$task_project_id) as the Projects Controller's display function is display($project_id). But this is redirecting to me the Project controller's index function. What is wrong with the redirect ? I thought the URL structure in CI was controller/function/parameter1/parameter2.

Upvotes: 0

Views: 170

Answers (2)

Abhishek
Abhishek

Reputation: 382

redirect($uri = '', $method = 'auto', $code = NULL)

redirect() function in codeigniter uses the url you define in your root. You can't directly jump to controllers.

Upvotes: 1

Talk2Nit
Talk2Nit

Reputation: 1135

You made mistake in writing redirect function. It should be like as below.

redirect('projects/display/'.$task_project_id);

redirect('projects/edit');

Upvotes: 1

Related Questions