Avatar of Curtis Long
Curtis Long
Flag 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.

Template layout for CMS
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
PHPApache Web ServerMySQL Server

Avatar of undefined
Last Comment
Derokorian

8/22/2022 - Mon
Curtis Long

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
Derokorian

function parser($buffer) {
   $content = file_get_contents('test.php');
   return str_replace('%1%',$content,$buffer);
}

Open in new window


HTH
Derokorian

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

Your help has saved me hundreds of hours of internet surfing.
fblack61
Curtis Long

ASKER
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
Derokorian

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.
Curtis Long

ASKER
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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Derokorian

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
Derokorian

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Curtis Long

ASKER
You are an absolute life saver :) Thank you that works perfectly.
Curtis Long

ASKER
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 :)
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Derokorian

Glad to help!