How to redirect from one page to another page in c

2019-08-02 17:06发布

问题:

I'm not getting how to do this codeignitor concept.
How to redirect one page to another page if I click the link?

Main1.php

        <?php 

            class Main1 extends CI_Controller { 

                public function index() { 
                    //Load the URL helper
                    $this->load->helper('url'); 

                    //Redirect the user to some site
                    redirect('http://localhost/CodeIgniter/index.php/Main'); 

                   $this->load->view('test1'); 
                } 
            } 
        ?>

test1.php

    <!DOCTYPE html>
    <html>
        <head>
            <title>view page</title>
        </head>
        <body style="padding-top:50px; padding-left:300px">
                <h1 style="color:red"> Without Extension </h1>
                <a href="">Visit With Extension Example</a>
        </body>
    </html>

Main.php

    <?php 

        class Main extends CI_Controller { 

            public function index() { 
                $this->load->view('test.html'); 
            } 
        } 
    ?>

test.html

    <!DOCTYPE html>
    <html>
        <head>
            <title>view page</title>
        </head>
        <body style="padding-top:50px; padding-left:300px">
                <h1 style="color:red"> With Extension </h1>

        </body>
    </html>

回答1:

You can use redirect(); something like given below.

redirect('controllerName/methodName','refresh'); 

OR if you want to redirect in same controller.

$this->methondName;

Your code should be like this

 <?php 

    class Main1 extends CI_Controller { 

        public function index() { 
            //Load the URL helper
            $this->load->helper('url'); 

            //Redirect the user to some site
            redirect('Main/index','refresh'); 

        } 
    } 
?>


回答2:

in your Main1.php index function , loading view after redirecting. It is not possible.

Main1.php

            class Main1 extends CI_Controller { 

                public function index() { 
                    //Load the URL helper
                    $this->load->helper('url'); 

//display test1 page
                   $this->load->view('test1'); 
                } 
            } 
        ?>

test1.php

  <!DOCTYPE html>
    <html>
        <head>
            <title>view page</title>
        </head>
        <body style="padding-top:50px; padding-left:300px">
                <h1 style="color:red"> Without Extension </h1>
                <a href="http://localhost/CodeIgniter/index.php/Main">Visit With Extension Example</a>
        </body>
    </html>

Main.php

load->view('test.html'); } } ?>

test.html

<!DOCTYPE html>
<html>
    <head>
        <title>view page</title>
    </head>
    <body style="padding-top:50px; padding-left:300px">
            <h1 style="color:red"> With Extension </h1>

    </body>
</html>

It is a small error..keep going with codeigniter



回答3:

you can use either

redirect('controllerName/methodName','refresh'); in your controller or if you want to just load the previous page you can use redirect($_SERVER['HTTP_REFERER']);



回答4:

Suppose you want to redirect from Controller Main1 to Main. Main1 loads the test1.php view, and it should contain a means to redirect from one view to another, like a button or a link. You should have a .php extension instead of .html to utilize php. Consider this as a best practice and you should avoid calling controllers within a controller as a normal practice to conform with MVC in general.

Test1.php

<!DOCTYPE html>
    <html>
        <head>
            <title>view page</title>
        </head>
        <body style="padding-top:50px; padding-left:300px">
                <h1 style="color:red"> With Extension </h1>
                <a href="<?php echo site_url("Main/"); ?>">Visit With Extension Example</a>
        </body>
    </html>

If you call Main then the controller will be processed. No need for you to use redirect inside the controller.

<?php 

class Main extends CI_Controller { 
    public function index() {
       $this->load->view('test'); 
    } 
} 
?>