Jump to content

Require a file and execute functions within that file


taketimeout

Recommended Posts

Hello.

 

The title is a little confusing, but:

 

What I am trying to do is build a simple CMS where all requests are routed through the root index.php file.

 

This file will be where all constants are defined and importantly where the database connection is estabilished.

 

There will be a folder where template files will reside and, based on the URI, a template file will be included into the root index.php file.

 

Here's some code:

 

<?php

//Sets the database connection and defines some constants
require ( "includes/config.php" );

//Where I will place the core functions of the CMS, one being the navigation builder
require ( "includes/functions.php" );

//Home page template file
require ( "views/cms/index.php" );

?>

 

So as you can see, I want to be able to call the navigation builder function, called "getMenuItems();", from the home page template.

 

However, I get the following error:

 

Fatal error: Call to undefined function getMenuItems() in C:\wamp\www\cms\views\cms\index.php on line 3

 

How can I access functions that have been defined in the "includes/functions.php" file, from the required template file "views/cms/index.php"?

 

If you need anything else explaining then please ask. If this has already been posted, I apologise in advance! I have searched Google and the forums but nothing turned up.

 

Chris

Link to comment
Share on other sites

Thanks for the reply.

 

Here is the code from "views/cms/index.php":

 

<?php 
		   
getMenuItems($dbh);
		   
?>

 

And the code that builds the menu:

 

<?php
function getMenuItems($dbh)
{
	$sql = "SELECT * FROM posts WHERE menu_item = 1";

	echo '<ul>';
	foreach ( $dbh->query($sql) as $item ) 
	{
		echo '<li>';
		echo $item['slug'];
		echo '</li>';
	}

	echo '</ul>';

}
?>

 

When I run "getMenuItems();" from the root index.php the menu is printed out.

 

Chris

Link to comment
Share on other sites

I know that, but is there no way to use the functions that were included in the root index.php file?

 

<?php
//index.php

//These functions are declared
require ( "includes/functions.php" );

//Can this file not use the previously declared functions?
require ( "views/cms/index.php" );

?>

 

That's what I want to do, if I can't do that then I will just include the functions in the "views/cms/index.php" file as well.

 

Chris

Link to comment
Share on other sites

files included in one .php file are not automatically included in other .php files, unless the file that include()'s other files is ALSO include'd in the other files.

 

if the files are included in /index.php, no, the functions will not automatically be included/required in /views/cms/index.php.

Link to comment
Share on other sites

What you're doing should work, if you've posted the correct code. Here is a quick example showing what I think you're trying to do:

File include_one.php

<?php
function one() {
   return ('This is comes from function "one"');
}
?>

File include_two.php

<?php
function two() {
   return ('This is comes from function "two"');
}
?>

File include_three.php

<?php
echo one() . "\n";
echo two() . "\n";
?>

File test_inc.php

<?php
require 'include_one.php';
require 'include_two.php';
require 'include_three.php';
?>

Results of running test_inc.php

This is comes from function "one"
This is comes from function "two"

 

Ken

 

Link to comment
Share on other sites

yes, i agree. i might have misunderstood the question. so, for example if this is /index.php,

 

<?php
//Sets the database connection and defines some constants
require ( "includes/config.php" );

//Where I will place the core functions of the CMS, one being the navigation builder
require ( "includes/functions.php" );

//Home page template file
require ( "views/cms/index.php" );

// IF YOU EXECUTE CODE FROM /views/cms/index.php here, it will have access to all functions in includes/functions.php.

?>

Link to comment
Share on other sites

Well it should work... I have tried what you said, 'kenrbnsn', but that is pretty much what I am doing at the moment. Do you think it could be that I have the includes and functions files in a different directory? I will try moving them into the root with the index.

 

yes, i agree. i might have misunderstood the question. so, for example if this is /index.php,

 

<?php
//Sets the database connection and defines some constants
require ( "includes/config.php" );

//Where I will place the core functions of the CMS, one being the navigation builder
require ( "includes/functions.php" );

//Home page template file
require ( "views/cms/index.php" );

// IF YOU EXECUTE CODE FROM /views/cms/index.php here, it will have access to all functions in includes/functions.php.

?>

 

<?php // IF YOU EXECUTE CODE FROM /views/cms/index.php here, it will have access to all functions in includes/functions.php.
?>

 

I have tried that, and it works as it should, but accessing those functions from the "/views/cms/index.php" file doesn't.

 

'kenrbnsn', have you tried what you suggested on a test server?

 

Chris

Link to comment
Share on other sites

Ok, that seems to work now, I have re-installed wamp and restored everything...

 

One thing that might have been causing it is this:

 

in the root index.php file, where I was including the index.php template file I had a constant called CONTENT_PATH that had the value of:

 

"http://localhost/cms/views/cms"

 

When I re-installed, I was getting the following warnings:

 

Warning: require() [function.require]: http:// wrapper is disabled in the server configuration by allow_url_include=0 in C:\wamp\www\cms\index.php on line 7

Warning: require(http://localhost/cms/views/cms/index.php) [function.require]: failed to open stream: no suitable wrapper could be found in C:\wamp\www\cms\index.php on line 7

Fatal error: require() [function.require]: Failed opening required 'http://localhost/cms/views/cms/index.php' (include_path='.;C:\php5\pear') in C:\wamp\www\cms\index.php on line 7

 

Taking out the constant seems to have fixed it.  :-[

 

I know... I didn't include THAT bit in my code.

 

Thanks for all your help though 'BlueSkyIS' and 'kenrbnsn'.  :thumb-up:

 

Chris

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.