Link to home
Start Free TrialLog in
Avatar of Curtis Long
Curtis LongFlag for United States of America

asked on

include_once files during ob_start callback php

I am working on a php module based CMS and almost have it finished. The only thing I am missing is a way to actually include the modules in their proper places. I have a template for the pages with %1% %2% and %3% where the modules should be inserted, if there is no module for that area then it should be empty, which it is doing. If there is a module in that area then it should get the proper files from the module_bases table and include them.

Here is a screenshot to show what I am talking about.

User generated image
Now, during execution, where it says %3% it should include the files for the blog module during the ob_start callback.

Basically what I really need is this.

test.php
<?php
echo "Hello, I am a test";
?>

Open in new window


index.php
ob_start("parser");
echo "%1%";

function parser($buffer)
{
   $buffer = str_replace("%1%",contents from test.php IE include(''test.php"),$buffer);
   return $buffer;
}

Open in new window


and the final result should be Hello, I am a test

Is there any way to do this?

Thanks
Avatar of Curtis Long
Curtis Long
Flag of United States of America image

ASKER

I should also mention that I have this in my .htaccess file

RewriteEngine On # turns on the rewrite engine
RewriteRule ^(((([0-9]+)|([a-z]))*/?)*)$ index.php?page=$1 [NC,L]

This way something like localhost/blog/cat/test would take me to the blog page and pull all the blogs with a category of test
function parser($buffer) {
   $content = file_get_contents('test.php');
   return str_replace('%1%',$content,$buffer);
}

Open in new window


HTH
Bah won't work with your current test.php unless you eval it. AKA
function parser($buffer) {
   $content = file_get_contents('test.php');
   return eval(str_replace('%1%',$content,$buffer));
}

Open in new window

I am having a problem with that code.

Here is what I am doing to test it.

snippet
				$content = file_get_contents('test.php');
				$buffer .="<h1>$content</h1>";
				$content = eval($content);
				$buffer .="<h1>$content</h1>";
 				return $buffer;

Open in new window


here is what I am getting for output
<h1>echo "This is a test include";</h1><h1></h1>

Open in new window


this is my test.php
echo "This is a test include";

Open in new window


I also tried this
<?php
echo "This is a test include";
?>

Open in new window

but it was upset about the opening tag and threw an error.
Is there something I am doing wrong?

Also, with this solution will I be able to include multiple files that have classes and functions that all rely on eachother? It is a basic MVC system, so I need to have the view be able to call the controller which calls the model.

Thanks
I don't understand why you aren't calling the module where it belongs, instead of doing a replace later on a buffer. Doesn't seem necessary to do a buffer at all. The way I do my MVC is i can call $this->view('test'); and it will include test.php (from my view folder) right there instead of trying to go thru a buffer and replace %#% with the proper output.

As far as a solution to your question, give me a few minutes to test some things and I'll get back to you.
The reason is I don't really know what goes where until they pull up a page. When a page is created in the admin interface, certain modules are assigned to it. A new instance of the module is then created in the database with the pageID that is equal to the ID field of the pages table that the newly created page has. Then when the page is requested, it grabs all the modules that are registered to it in the database and places them in the proper place on the page using the replace method.

The blog page could have an HTML module in spot 1 for a title or something,  a Menu module in spot 2 for a navigation menu, and a blog in spot 3.
Ok maybe I was wrong, I can only get it to work when test.php doesn't actually contain php. It works perfectly as such when test.php is just html code.

test.php
Hello World!<br />
How are you all <strong>Today</strong>?

Open in new window


index.php
<?php
ob_start('parser');

function parser($buffer) {
   $var = file_get_contents(DOC_ROOT.'test.php');
   return str_replace(chr(045).'1'.chr(045),$var,$buffer);;
}

echo '<h1> This is a test.</h1><p>%1%</p>';

Open in new window


I'm at a loss out how to get it working, with php in the included unless if you want to use http://localhost/test.php so its actually getting the contents of the output?
ASKER CERTIFIED SOLUTION
Avatar of Derokorian
Derokorian
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You are an absolute life saver :) Thank you that works perfectly.
Awesome work. Thank you so much for your help. Now I can get on to actually developing the modules and still get this out on time :)
Glad to help!