• 0

[PHP] Classes and config outside public_html


Question

I'm just experimenting with building a site, and want to follow a patten I have used elsewhere...

Say my folder is called "WebSite1". Within that I have public_html which will contain all client-facing code and assets...

But outside it I want a folder called "Classes" that will contain my business objects. Also I'd like to have a config.whatever that contains things such as database connections.

I know this can be leveraged through Zend for example, but am looking at my own way to do this. Can someone tell me how?

Ta

12 answers to this question

Recommended Posts

  • 0

Where's the problem exactly? just tell apache that public_html is the root folder for your web application. That's all there is to it. Inside the php files in the public_html do include statements that you require from the Classes folder for example.

  • 0

I don't think you have fully understood.

I am looking to create a situation sort of like where I have a "Application Root" and a "Web Root". Public_html being the web root, and the folder that is above that being the app root.

I do not want to be declaring my includes and so on on each HTML page.

  • 0

It may be better to do something like that with mod_rewrite. Here is a snippet of old code which does a similar thing (I apologise for the poor quality of it in advance - it was written in about 5 minutes to accommodate something someone wanted hacked into their existing script and I wasn't being paid :p):

Note: classes belonging to the system begin mm_, are stored in inc/class_lib, and cannot be publicly called using the URL. Methods beginning _ can also not be called publicly, and there's some other things which help this such as is_public. mm_error and mm_system are the main system/error classes here:

// Class Loader
function __autoload($class_name) {

	// We give mm_ classes priority here:
	if(file_exists($_GLOBALS['root_path'] . "inc/class_lib/$class_name.class.php")) {
		require_once($_GLOBALS['root_path'] . "inc/class_lib/$class_name.class.php");
	} else {
		require_once($_GLOBALS['root_path'] . "application/controller/$class_name.php");
	}
}

// Create & Init System - **you can probably ignore this bit**
$GLOBALS['system'] = new mm_system();
$GLOBALS['system']->init();

// Load Required Class and Function
$class = $_GET['class'];
$func = $_GET['func'];
if(empty($class)) $class = $GLOBALS['default_class'];
if(empty($func)) $func = 'index';

if(substr($class, 0, 3) !== 'mm_' && file_exists($_GLOBALS['root_path'] . "application/controller/$class.php") && eval("return $class::is_public;")) {
	$page = new $class();
	if(method_exists($page, $func) && substr($func, 0, 1) !== '_') {
		$page->$func();
	} else {
		$page->unknown_method();
	}
} else {
	$page = new mm_error(102);
}

This is called by the url index.php?class=ClassName&func=FunctionName, which can be simply modified with mod_rewrite to be something like http://url/classname/function/variables

  • 0
Sorru for lack of updates. I went with Anthony's autoloader idea. However am looking at Zend or CodeIgnitor to use as a framework to work within with such things.

I would recommend CodeIgniter, although I wish that it would end support for PHP4 and bring on some new PHP5 features.

You could also use a pseudo-singleton controller to autoload all of the classes and achieve what you want in a fairly clean and efficient manner.

  • 0

If you're using Apache as your web server, you can put this in your application root's .htaccess file and it'll forward all outside requests to that folder.

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteRule ^$ public/ [L]

RewriteRule ^(.*)$ public/$1 [L]

</IfModule>

Replace 'public' (without the quotes) with the folder of your choice.

The first line (the first rule): send all requests for the root directory (without any files or directories, for example: http://example.com/) to the public directory (http://example.com/public/). Also, if there's a direct request for http://example.com/public/, it'll look for a folder called 'public' (without the quotes) inside the public directory (effectively http://example.com/public/public/). And the [L] option indicates that if the rule is successful, that rule will be the last one checked.

The second line: send all requests for anything to find anything inside the public directory (a request for http://example.com/index.php will be directed to http://example.com/public/index.php). And, like the above rule, stop checking all following rules.

  • 0
I would recommend CodeIgniter, although I wish that it would end support for PHP4 and bring on some new PHP5 features.

You could also use a pseudo-singleton controller to autoload all of the classes and achieve what you want in a fairly clean and efficient manner.

Kohana, originally a fork of Codeigniter, it shares less and less in common with it. It is easily my favourite framework and is written to a standard that I admire.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.