开发者

Loading CI controller in PHP on the same server

开发者 https://www.devze.com 2022-12-25 12:33 出处:网络
How can include CodeIgniter content in a regular PHP page on the same server but not part of the CI app?

How can include CodeIgniter content in a regular PHP page on the same server but not part of the CI app?

For example I'm am trying to load a header from CI into Wordpress. Whats the best way to include a CI controller (eg; 开发者_开发技巧index.php/mycontroller/header/) on the same server?


From http://codeigniter.com/forums/viewthread/88635/

This is overkill, as file_get_contents($url) or similar, would be better. However, it may work for your situation:

$CI_INDEX = '/path/to/your/codeigniter/index.php';
$path = '/controller/method';
$_SERVER['PATH_INFO'] = $path;
$_SERVER['REQUEST_URI'] = $path;

chdir(dirname($CI_INDEX));
ob_start();
require($CI_INDEX);
$output = ob_get_contents();
ob_end_clean();
die($output);


Use PHP's built-in file_get_contents(). Just make sure to use the full HTTP path, not a relative path. Example:

<?php
  file_get_contents('http://your.server.com/codeigniter-path/controller/');

That should do the trick.


Have you considered using an iframe in your Wordpress page?

<iframe src="http://my-site-url/index.php/mycontroller/header"></iframe>

It might be the simplest solution.


When you are trying to wrap content with HTML from an external source, this can easily be achieved by placing HTML comments (or other recognizable tags) in the target site, then using PHP to split/explode the content.

I have used this method to create several micro-sites for MSN Money which has in. Then I would simply use:

list($header_html) = explode('<!-- Header -->', file_get_contents($url));

It was slightly more complex than that, involving caching and all sorts of other madness, but at its base that is the method to use if WordPress will allow it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号