【问题标题】:How to call function from Controller to include into the View page with Codeigniter?如何使用 Codeigniter 从 Controller 调用函数以包含到 View 页面中?
【发布时间】:2011-10-28 03:45:37
【问题描述】:

我想从 Controller 调用函数以使用 Codeigniter 包含到 View 页面中。通常当我打开任何页面时,我会在 Controller 中调用 $this->load->view() 来打开该页面。现在我想将子页面包含到主页中,但它不能在视图中包含任何功能。我尝试包含这样的功能。

<body><? include(site_url().'/login'); ?></body>

<body><? include('/login'); ?></body>

<body><? include('./login'); ?></body>

我可以使用此链接http://localhost/ci_house/index.php/login 打开页面。但是当我打开主页运行我的代码时,它会显示这些错误。

A PHP Error was encountered

Severity: Warning

Message: include(http://localhost/ci_house/index.php/login) [function.include]: failed to open stream: no suitable wrapper could be found

Filename: views/main.php

Line Number: 8
A PHP Error was encountered

Severity: Warning

Message: include() [function.include]: Failed opening 'http://localhost/ci_house/index.php/login' for inclusion (include_path='.;C:\php5\pear')

Filename: views/main.php

Line Number: 8

我想在一页中显示 2 个视图。

function test1()
{ $data['var_for_login_view'] = 'get table1';
  $this->load->view('main1',$data);
}
function test2()
{ $data['var_for_login_view'] = 'get table2';
  $this->load->view('main2',$data);
}

在views/main.php中:

$this->load->view('test1');
$this->load->view('test2');` 

我想表现得像

<body>
include('main1.php');
include('main2.php');
</body>

但我可以在 Codeigniter 中这样显示。

【问题讨论】:

  • 你想从一个视图中包含另一个视图吗?
  • 是的,示例 function sh_all_post() { $this-&gt;load-&gt;model('post_model', 'post'); $data['qry_post'] = $this-&gt;post-&gt;get_all_post(); $this-&gt;load-&gt;view('sh_all_post_view',$data); } public function main() { $this-&gt;load-&gt;view('main'); } 我想包含这些功能以仅在索引页面中显示。当我打开索引页面时,它必须在索引页面中显示这些功能的页面。

标签: php codeigniter include


【解决方案1】:

我真的不能很好地理解你的问题,但是嘿,你可以在另一个视图中“包含”任何视图而不会出现问题..

在main.php中:

$this->load->view('login');

您甚至不需要将参数传递给它,因为它们已被缓冲,因此可用于您可能插入的任何子视图。但是,请更清楚您的实际需求。

如果您想在 main() 中包含您在 login() 方法中加载的相同视图,当然您不必包含 CI URI,而只需创建需要在控制器方法 login 中传递的变量(),然后调用您想要的任何视图,无论是为此特定方法或任何其他控制器的方法设计的视图。

所以,for.ex.

function login()
{
  $data['var_for_login_view'] = 'a variable';
  $data['var_for_this_view'] = 'another variable';
  $this->load->view('main');
}

在views/main.php中:

echo $var_for_this_view;
$this->load->view('login');
echo $var_for_login_view;

// see? $data was not passed to $this->load->view('login'), but it's still there nonetheless!

【讨论】:

    【解决方案2】:

    我的理解是:您在其中一个控制器中定义了一些函数,并且您希望能够从另一个控制器/视图调用这些函数。

    如果这是正确的,那么我们通常不能这样做。这里有几个选择:

    1. 将这些函数移至库/帮助程序。在您想要的任何地方加载该库/帮助程序,然后您可以调用这些函数。

    2. 如果你绝对需要从控制器调用这些函数,你可以查看HMVC extension

    【讨论】:

      【解决方案3】:

      你不能包含使用站点 url,使用这个 $this->load->view('template', $data);用于codeigniter。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-03-14
        • 2018-12-21
        • 1970-01-01
        • 2011-06-04
        • 1970-01-01
        • 2016-05-15
        • 1970-01-01
        相关资源
        最近更新 更多