taketimeout Posted January 2, 2011 Share Posted January 2, 2011 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 Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted January 2, 2011 Share Posted January 2, 2011 we need the pertinent code, the code that causes the error. where is C:\wamp\www\cms\views\cms\index.php line 3? Quote Link to comment Share on other sites More sharing options...
taketimeout Posted January 2, 2011 Author Share Posted January 2, 2011 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 Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted January 2, 2011 Share Posted January 2, 2011 you need to include or require the file before you use the function. the file has not be included or required in this code: <?php getMenuItems($dbh); ?> Quote Link to comment Share on other sites More sharing options...
taketimeout Posted January 2, 2011 Author Share Posted January 2, 2011 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 Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted January 2, 2011 Share Posted January 2, 2011 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. Quote Link to comment Share on other sites More sharing options...
taketimeout Posted January 2, 2011 Author Share Posted January 2, 2011 Ok. Thanks for your replies BlueSkyIS Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted January 2, 2011 Share Posted January 2, 2011 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 Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted January 2, 2011 Share Posted January 2, 2011 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. ?> Quote Link to comment Share on other sites More sharing options...
taketimeout Posted January 2, 2011 Author Share Posted January 2, 2011 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 Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted January 2, 2011 Share Posted January 2, 2011 I executed these files on my server, http://rbnsn.com/phpfreaks/test_inc.php Whether the files are stored in subdirectories or in the same directory won't matter. The content of included/required files is all executed as though it was in-line with the main code. Ken Quote Link to comment Share on other sites More sharing options...
taketimeout Posted January 2, 2011 Author Share Posted January 2, 2011 Right. I'll try this on another server and see if I can get it working there. Thanks for clearing that up for me. I'll post back the results. Chris Quote Link to comment Share on other sites More sharing options...
taketimeout Posted January 2, 2011 Author Share Posted January 2, 2011 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'. Chris Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.