如何通过一个功能显示不同的页面以及如何在菜单中声明链接?

时间:2013-02-21 11:05:00

标签: codeigniter menu

headerview.php

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title><?php echo $title; ?></title>
    </head>
    <body>
        <div id="topmenu">
            <ul>
                <li><a href="What Should I write Here ?">Home</a></li>
                <li><a href="What Should I write Here ?">About Us</a></li>
                <li><a href="What Should I write Here ?">Contact Us</a></li>
            </ul>    
        </div>  

footerview.php

</body>
</html>

控制器/ main.php

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class main extends CI_Controller {
 function index(){
     $this->load->view('headerview');
     $this->load->view('homeview');
     $this->load->view('footerview');
  } 

}
?>

如何通过一个函数显示view / about_us_view.php,view / contact.php等页面?

-Thanks。

2 个答案:

答案 0 :(得分:1)

我假设所有您的视图页都在根视图文件夹

控制器

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Main extends CI_Controller {

 function index($page = 'homeview')
{

    if ( ! file_exists('application/views/'.$page.'.php')){

        show_404();
    }
    else{

    $this->load->view('headerview');
    $this->load->view( $page);
    $this->load->view('footerview');
        }

}

}

标题

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title><?php echo $title; ?></title>
    </head>
    <body>
        <div id="topmenu">
            <ul>
                <li><a href="<?php echo base_url('index.php/main');?>">HOME</a></li>
                <li><a href="<?php echo base_url('index.php/main/index/about_us_view');?>">About Us</a></li>
                <li><?php echo base_url('index.php/main/index/contact');?>">Contact Us</a></li>
            </ul>    
        </div> 

基本网址格式如下所示

http://example.com/[controller-class]/[controller-method]/[arguments]

在索引函数中,我们将页面名称作为参数传递

查看联系页面

<?php echo base_url('index.php/main/index/contact');?>

这里

控制器:主

方法:索引

论点:联系

还在config / autoload.php中自动加载url助手。

$autoload['helper'] = array('url');

答案 1 :(得分:-1)

您需要执行以下操作:

headerview.php

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title><?php echo $title; ?></title>
    </head>
    <body>

homeview.php

<?php $this->load->view('headerview'); ?>
<div id="topmenu">
            <ul>
                <li><a href="What Should I write Here ?">Home</a></li>
                <li><a href="What Should I write Here ?">About Us</a></li>
                <li><a href="What Should I write Here ?">Contact Us</a></li>
            </ul>    
        </div>  
<?php $this->load->view('footerview'); ?>

和footerview.php

</body>
</html>

和控制器

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class main extends CI_Controller {
 function index(){
     $this->load->view('homeview');
  } 
}
?>