天天看點

CI架構(4)-頁面跳轉

1,設定CI架構的初始控制器

2,建立兩個頁面

3,實作頁面跳轉

1,設定CI架構的初始控制器

當運作CI項目時,我們所看到的第一個頁面是由controller轉發的視圖,而這個controller我們是在[application]->[config]->[routes.php]進行設定

$route['default_controller'] = 'welcome';

我目前設定的第一個頁面是由welcome轉發的視圖。

2,建立兩個頁面

(1)首先在[application]->[controllers]目錄下建立Page2.PHP

class Page2 extends CI_Controller {

    public function index()
    {
        $this->load->view('page2');
    }
}
           

(2)首先在[application]->[view]目錄下建立Page2.PHP

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>page2</title>
</head>
<body>

   <h1 >page2</h1>

</body>
</html>
           

3,實作頁面跳轉

(1)在[application]->[controllers]->[Welcome.php]

添加代碼$this->load->helper(‘url’);

class Welcome extends CI_Controller {

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

        $this->load->helper('url');
    }
    public function index()
    {
        $this->load->view('welcome');
    }
}
           

(2)在[application]->[views]->[welcome.php]頁面添加js代碼

<html lang="en">
<head>
    <meta charset="utf-8">
    <title>第一個頁面</title>
    <script type="text/javascript">
        function turnto(){
            var url = "<?php echo site_url('Page2')?>";

            window.location.href=url;
        }
    </script>

</head>
<body>
   <h1 onclick="turnto()">hello student</h1>
</body>
</html>
           

繼續閱讀