• 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.
  • Posts

    • Anyway to download these versions without being on the Experimental builds?
    • Nothing is stopping you from continuing with your testing cadence. If updates are released every 2 weeks instead of 4, and you test once every 4 weeks, the exact same amount of patches will still be available for you in those 4 weeks. For example: Before 4th week - patch 1, 2, 3, 4 After 2nd week - patch 1 and 2 4th week - patch 3 and 4 Still the same amount after 4.
    • Everyone else has said it. I'm gonna say it - you don't know what you're talking about. I do. I have two laptops. One work, one personal. I have access to two more laptops - both personal. At home I manually update my personal laptop when I see on Neowin that there is an update - I carry on and only apply the updates when I am ready. My work one only updates when my workplace decides to send it - I carry on and only apply the updates (when they actually arrive, which is usually days after the release) when I switch off the laptop at the end of the day as usual. The two other personal laptops only get updated when I get to it which is rarely - the people who own them carry on using them until I get to it and update them. All of the browsers on all laptops are configured to restore the tabs when launched. Google and Microsoft have changed from 6 weeks to 4, and it looks like it's going to move to 2. None of these changes affect how any of these browsers on the laptops are used. Not one jot. My advice to you is stop panicking whenever you see an update. Just carry on with what you're doing. This even benefits you in a way - from your comment you sound like you don't like the changes or the frivolous new features - great - then carry on as before!
    • AMAZON needs to take total accountability for this.
    • Server Summit had a heap of announcements, ADCS changes are baller.
  • Recent Achievements

    • Week One Done
      Jeroen Wilms earned a badge
      Week One Done
    • Week One Done
      rolfus earned a badge
      Week One Done
    • One Month Later
      Leroy Jethro Gibbs earned a badge
      One Month Later
    • Conversation Starter
      flexorcist earned a badge
      Conversation Starter
    • One Month Later
      AndreaB earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      509
    2. 2
      +Edouard
      198
    3. 3
      PsYcHoKiLLa
      138
    4. 4
      ATLien_0
      90
    5. 5
      Steven P.
      80
  • Tell a friend

    Love Neowin? Tell a friend!