无法从一页重定向到另一页

时间:2018-06-20 10:52:28

标签: php codeigniter

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>

我完全不是这个Codeignitor概念的新手,我知道这确实是一个愚蠢的问题,但是请大家尝试帮助我。实际上,当我单击链接时,我尝试使用Codeignitor函数将页面重定向到另一页面,反之亦然,但即时通讯无法执行此操作,请任何人都可以致电给我即时通讯出现问题的地方,很抱歉,我知道它的确是愚蠢的问题,但没有得到解决方法。

3 个答案:

答案 0 :(得分:0)

只需在目标路径中使用redirect(控制器/方法[如果 index ,控制器名称就足够])

redirect('main');

在这些情况下,您可以使用redirect

  1. 控制器重定向(自动调用index函数)
  2. 控制器方法重定向(主/测试)
  3. 路由重定向(main-test,其中main-test指向特定方法)

仅供参考: 一旦达到redirect函数,下面的任何代码都将不会执行

答案 1 :(得分:0)

如果要重定向任何页面的外部链接,可以使用header('Location: http://www.example.com/');

答案 2 :(得分:0)

如果您希望有一个链接将您从一页移动到另一页,则redirect()功能不是答案。 “重定向”实际上与在浏览器中键入新URL相同-仅通过编程即可完成。

<a> HTML标记用于在用户界面中创建指向另一个URL的链接。

这是可以满足您需要的修订文件。 请注意,redirect()呼叫已被删除。

/controllers/Main.php

<?php

class Main extends CI_Controller
{
    public function index()
    {
        //Load the URL helper
        $this->load->helper('url');
        //load the view
        $this->load->view('test.html');
    }
}

请注意,没有PHP结束标记,即。 ?>位于文件末尾。如PHP documentation

中所述
  

如果文件是纯PHP代码,则最好在文件末尾省略PHP关闭标记。这样可以防止意外的空格或在PHP结束标记之后添加新行,这可能会导致不良影响,因为当程序员不打算在脚本中的该点发送任何输出时,PHP将开始输出缓冲。

/views/test.html 是上方控制器加载的视图文件。

<!DOCTYPE html>
<html>
        <head>
                <title>view page</title>
        </head>
        <body style="padding-top:50px; padding-left:300px">
                <h1 style="color:red">HTML file view</h1>
                <a href='<?= base_url('main1');?>'>View using php extension</a>
        </body>
</html>

这是用于加载php视图文件的另一对文件。

/controllers/Main1.php

<?php
class Main1 extends CI_Controller
{
    public function index()
    {
        $this->load->helper('url');
        $this->load->view('test1');
    }
}

/views/test1.php

<!DOCTYPE html>
<html>
    <head>
        <title>view page</title>
    </head>
    <body style="padding-top:50px; padding-left:300px">
        <h1 style="color:red">PHP view file</h1>
        <a href='<?= base_url('main');?>'>View using html Extension</a>
    </body>
</html>

FWIW,使用.html文件作为视图没有优势,但是有一个劣势。问题在于有时您需要在视图中执行一些PHP,这意味着它必须使用.php作为扩展名。不久之后,您将尝试混合使用.html和.php视图文件,并且在您不知道该文件之前,您会感到困惑和愤怒,因为事情没有按预期的方式进行。让生活变得更轻松,并使用.php来查看文件-即使文件中没有任何PHP代码。

相关问题