【问题标题】:How to access the the MVC model created by me in CodeIgniter如何访问我在 CodeIgniter 中创建的 MVC 模型
【发布时间】:2015-03-30 17:18:05
【问题描述】:

我想现在我正在获取页面,但页面显示为空白。我访问的网址是http://localhost/BookLearn/CodeIgniter-2.2.1/index.php/start


我的控制器的文件名为start.php。 My View 的文件名为testview.php

控制器

<?php
  class Start extends CI_Controller
  {
    var $base;
    var $css;
    function Start()
    {
      date_default_timezone_set('UTC');
      parent::CI_Controller();
      $this->base = $this->config->item('base_url');
      $this->css  = $this->config->item('css');
    }

    function hello()
    {
      $data['css'] = $this->css;
      $data['base'] = $this->base;
      $data['mytitle'] = "Using CI to build a Site";
      $data['mytext'] = "Hello now Can we do some Dynamic Processing. <br /> And Ah! U Know, This Is Dynamic!!";
      return $this->load->view('testview', $data);

    }
  }
?>

查看

<html>
  <head>
    <base href= <?php echo "$base"; ?> />
    <link rel="stylesheet" type="text/css" href="<?php echo "$base/$css"; ?>" />

  </head>
  <body>
    <h1> <?php echo $mytitle; ?> 
    test</h1>
    <p class="test"> <?php echo $mytext; ?> </p>
  </body>
</html>

【问题讨论】:

  • 我不确定您要做什么...但是对于 base_url,您为什么要这样称呼它?为什么不使用 URL 帮助程序并使用 base_url()
  • @CodeGodie 请详细说明一下!
  • 你也不需要return视图,删除它,你只需要$this-&gt;load-&gt;view('testview', $data);

标签: php codeigniter model-view-controller


【解决方案1】:

解决方案 1

您只需将date_default_timezone_set('America/Los_Angeles'); 添加到您的Start() 函数即可。

您可以通过替换“America/Los_Angeles”来使用您的时区

解决方案 2

您可能需要将时区放入 php.ini 文件中的配置中。在 php.ini 文件中应该有一个像 blow 一样的块:

[Date] ; Defines the default timezone used by the date functions ; http://php.net/date.timezone date.timezone = America/New_York

如果没有,请在您的 php.ini 中添加此块(将时区替换为您的),确保重新启动 httpd(服务 httpd 重新启动)。

List of Supported Timezones

【讨论】:

    【解决方案2】:

    这就是我会做的:

    Config.php (应用程序/config/config.php)

    $config['base_url'] = 'http://'.$_SERVER['SERVER_NAME']';
    

    控制器:

    class Start extends CI_Controller {
    
        private $css;
    
        function __construct() {
            $this->css = $this->config->item('css');
        }
    
        function hello() {
            $data['css'] = $this->css;
            $data['mytitle'] = "Using CI to build a Site";
            $data['mytext'] = "Hello now Can we do some Dynamic Processing. <br /> And Ah! U Know, This Is Dynamic!!";
            $this->load->view('testview', $data);
        }
    
    }
    

    查看:

    <html>
        <head>
            <base href="<?= base_url(); ?>" />
            <link rel="stylesheet" type="text/css" href="<?= $css; ?>" />
    
        </head>
        <body>
            <h1><?= $mytitle; ?> test</h1>
            <p class="test"><?= $mytext; ?> </p>
        </body>
    </html>
    

    【讨论】:

      【解决方案3】:

      您需要在每个 CI 类中定义一个索引函数

      或按名称调用类函数,例如:ci_root/index.php/class/function

      http://localhost/BookLearn/CodeIgniter-2.2.1/index.php/start/hello

      您需要通过替换parent::CI_Controller();来使用parent::__construct();

      <?php
      class Start extends CI_Controller {
      var $base;
      var $css;
          function Start()
          {
            date_default_timezone_set('UTC');
            parent::__construct();
            $this->base = $this->config->item('base_url');
            $this->css  = $this->config->item('css');
          }
      
          function index(){
              $this->hello();
          }
      
          function hello()
          {
            $data['css'] = $this->css;
            $data['base'] = $this->base;
            $data['mytitle'] = "Using CI to build a Site";
            $data['mytext'] = "Hello now Can we do some Dynamic Processing. <br /> And Ah! U Know, This Is Dynamic!!";
            return $this->load->view('testview', $data);
      
          }
      }
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-08-27
        • 2010-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-14
        • 1970-01-01
        相关资源
        最近更新 更多