简体   繁体   English

如何从Controller调用函数以使用Codeigniter包含到“视图”页面中?

[英]How to call function from Controller to include into the View page with Codeigniter?

I want to call function from Controller to include into the View page with Codeigniter. 我想从Controller调用函数,以使用Codeigniter将其包括到“视图”页面中。 Usually when I open any page, I call $this->load->view() in Controller for open that page. 通常,当我打开任何页面时,我都会在Controller中调用$ this-> load-> view()来打开该页面。 Now I want to include sub page into main page, but it can't include any function in View. 现在,我想将子页面包含在主页中,但是它不能在View中包含任何功能。 I try to include function like this. 我试图包括这样的功能。

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

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

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

I can open page with this link http://localhost/ci_house/index.php/login . 我可以使用此链接打开页面http://localhost/ci_house/index.php/login but when I open main page for run my code it show these error. 但是,当我打开主页运行代码时,会显示这些错误。

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

I want to show 2 view in 1 page . 我想在1页中显示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);
}

In views/main.php: 在views / main.php中:

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

I want to show like 我想表现像

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

but I can show like this in Codeigniter. 但我可以在Codeigniter中这样显示。

I really can't understand your question well, but hey, you can "include" any view within another view without problems.. 我确实无法很好地理解您的问题,但是,您可以在没有问题的情况下将任何视图“包含”到另一个视图中。

In main.php: 在main.php中:

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

You don't even need to pass it the paramwters, as they are buffered so available to any child view you might insert. 您甚至不需要将参数传递给它,因为它们已被缓冲,因此可以插入任何子视图。 But please, be more clear on what you actually need. 但是,请明确您的实际需求。

If you want to include in main() the same views you load in login() method, of course you don't have to include a CI URI, but just create the variables you need to pass inside the controller's method login(), and then call whatever view you want, be it a view which is designed for this specific method or for any other controller's method. 如果要在main()中包含与login()方法中加载的视图相同的视图,则当然不必包含CI URI,而只需创建需要在控制器的方法login()内部传递的变量,然后调用所需的任何视图,无论是为该特定方法或其他任何控制器方法设计的视图。

So, for.ex. 因此,for.ex。

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

In views/main.php: 在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!

What I understand is this: You have some functions defined in one of your controllers and you want to be able to call these functions from another controller/view. 我的理解是:您在一个控制器中定义了一些功能,并且希望能够从另一个控制器/视图调用这些功能。

If this is correct, then we can't normally do this. 如果这是正确的,那么我们通常不能这样做。 Here are a couple of alternatives: 以下是一些替代方案:

  1. Move these functions to a library/helper. 将这些功能移至库/帮助器。 Load that library/helper wherever you want and then you can call these functions. 随时随地加载该库/帮助程序,然后可以调用这些函数。

  2. If you absolutely need to call these functions from the controller, you can look into HMVC extension . 如果您绝对需要从控制器调用这些功能,则可以查看HMVC extension

You can't include using site url, use this $this->load->view('template', $data); 您不能包括使用网站网址,请使用此$ this-> load-> view('template',$ data); for codeigniter. 用于代码点火器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM