what should i write to redirect the page from Controller in CI?

陌路散爱 提交于 2019-12-11 02:39:35

问题


here is my Controller

<?php 
class First_Controller extends CI_Controller{
    public function index(){

        $this->load->view('firstpage');
    }

    public function validate_credentials()
    {
        $data[] =array(

            $username=$this->input->post('username'),
            $password=$this->input->post('password')
            );
        $this->load->model('First_Model');
        $result=$this->First_Model->validate($data);

        if($result==true)
        {
            //redirect('welcome');//Error Page is not found
            echo "found";
        }
        else{

            echo "not found";
        }
    }

}
?>

and here is the second controller 



   <?php 
    class Welcome_Controller extends CI_Controller{
        public function index(){

            $this->load->model('First_Model');
            $data['results']=$this->First_Model->display();

            $this->load->view('welcome',$data);
    }       }   
    ?>

if i write indirect to my page it show the error page is not found , if i give it a controller same error , and if i gave page redirect('welcome'); show error . i dont know what to do , could anyone guide me . thanks


回答1:


Follow These Steps:

  1. Did you load url helper ?

    if not then first load the url helper

    $this->load->helper('url');

    in your function or constructer.

    if you want to use it in whole application then load it in autoload.php file

  2. In your config.php file give your site url

    $config['base_url']='your site url';

  3. in redirect function give axact path of file name use url helper function

    redirect(base_url().'welcome');

    if you want to redirect on another controller's function pass it like

    redirect('Welcome_Controller');

    if the function is index no need to define it, to redirect on another function

    redirect('Welcome_controller/anotherFunction');




回答2:


Try this redirect(base_url() . 'index.php/welcome'); if you use htaccess and do not need index.php then make it like redirect(base_url() . 'welcome');




回答3:


redirect('Welcome_Controller');

Thats enough. please check weather your controller name is'Welcome_Controller'




回答4:


For example:

redirect(base_url('cms/lists')); 

//for edit or updating the form - you can pass ID like this

$ID=$this->uri->segment(3);
redirect(base_url("cms/edit/".$ID));


来源:https://stackoverflow.com/questions/41276202/what-should-i-write-to-redirect-the-page-from-controller-in-ci

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!