如何通过一个函数显示不同的页面,以及如何在菜单中声明链接



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>  

页脚视图.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等页面?

-谢谢。

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

控制器

<?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> 

下面给出了基本的url模式

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

在index函数中,我们将页面名称作为参数进行传递

查看联系人页面

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

此处

控制器:主

方法:索引

论点:联系

还可以在config/autoload.php.中自动加载url帮助程序

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

您需要执行以下操作:

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'); ?>

和页脚视图.php

</body>
</html>

和控制器

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class main extends CI_Controller {
 function index(){
     $this->load->view('homeview');
  } 
}
?>

相关内容

最新更新