简体   繁体   中英

Include codeigniter page into non-codeigniter php page

I am working on a CMS that instead of generates pages automatically, it generates code and gives it to the user to add to their php page.

I am in the process of moving the application to CI but struggling with how to set up this process.

By making 2 codeigniter index pages (one for the main application and the other for connecting which changes the initial controller and index path) this almost works, what I would desire would be something like this:

<?php include("cms/connector.php"); ?>
<html>
..
<body>
<h1>Static</h1>
<?php echo($cms['data']); ?>
</body>

It pulls the file correctly and runs the view that is called, but the variables from the view are not saved.

Also I need the ability to target the cms page they are trying to get, however:

<?php include("cms/connector.php/cms/2"); ?>

No longer works (I guess because there is no extension?) If I could get this part to work I wouldn't need to worry about the variables since I could just include the view directly on the page

Any ideas would be great

The application could be hosted on various setups, so I would like it if I could bypass the need to include the direct url (http) since it might not always be turned on by default. Also I don't want to pull with ajax since I want the content to be picked up well by search engines

You can use PHP's cURL module . Below is taken from the examples in the documentation.

<?php
// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/cms/connector.php/cms/2");
curl_setopt($ch, CURLOPT_HEADER, 0);

// grab URL and pass it to the browser
curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);
?>

Note that this will pull the ENTIRE document, including doctype, <head> , etc. This is not what you want if you're including this in another page. You probably want to modify connector.php to output only the body of the document..

Sounds like you are designing an api.

(And I'm not sure why you are using CI for half of it and custom scripts for the rest. CI can accomodate your needs.)

The following suggestion might be obvious or outside of your expected answers, but on the off-chance it helps, I wanted to put it on the table.

If I understand your question and comments correctly, you have users who have sites on your server. You also have an application that gives them code that they paste into their site that somehow works with information on your server.

  1. You need a controller that receives requests for information from the pasted code
  2. You need a way to authenticate the request
  3. The user provides some kind of variable information
  4. You need to return information or html upon successful validation, based on variables provided by the user.

Codeigniter allows you to work with segment based url's or with query strings. It prefers and works out of the box with segments, but can be configured to work with query strings .

You may construct a url that contains

  1. the name of a controller and function to process the request (instead of the second index page you mentioned
  2. a token for authenticating the request. This would ideally be used to compare to information stored in the db when the token is created and issued to the user. Check out this article: http://www.infoq.com/news/2010/01/rest-api-authentication-schemes and this one too: http://oauth.net/
  3. information provided by the user

An example:

"example.com/cms/(index.php)/connector/function-name/auth-token/user-info"

Once your controller processes the request, return the information to the user.

Codeigniter routes the application depending on the environment state of the URI

What you need to do is set the environment and include the index view file like so:

$_SERVER["REQUEST_URI"] = "cms/2";

//Set GET action,method params etc

require_once "path/to/index.php";

When you load CI Index file it reads the SERVER Variable and others which you may have to find and execute the controller and method, i would also advise that you modify the library/view file as it may exit upon output causing your script to exit.

Also you may wis hto look into ob_start() etc to catch the buffer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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