• 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

    • Ninja Theory was great until Microsoft acquired them. They let them spend a decade on two games, with a third on the way, that explore mental health and were OK interactive experiences. Sony appears to be able to choose better IPs to develop, particularly single player. I'm probably going to buy a PS5 just for Wolverine and God of War Laufey (which reminds me of Heavenly Sword when Ninja Theory was with Sony making games).
    • Good riddance to a terrible studio. They've done nothing but over promise features and fall flat with the releases they've put out.
    • hey buddy, i don't appreciate the attitude. Can't wait to see the results.
    • Marshall Major V Bluetooth headphones are now up to 47% off on Amazon by Ivan Jenic The Marshall Major V in Midnight Blue is currently $89.99 on Amazon, down from $169.99. That's 47% off and $80 saved on a pair of wireless on-ear headphones from one of the most recognizable names in audio. The Major V is Marshall's take on a long-lasting everyday headphone. The headphones deliver 100+ hours of wireless playtime, which puts them in a completely different category from most Bluetooth headphones that hover around 30-40 hours. You’re charging this thing once a week at most, and with wireless charging supported, you don’t have to worry about additional cables. Marshall promises its signature sound profile, with strong bass, smooth mids, and clear highs. There’s a customizable M-button, which you can set to quickly access Spotify Tap, your EQ settings, or a voice assistant. The design is foldable and lightweight at 186 grams, so it’s easy to pack for travel. And finally, the faux leather finish gives the Major V a sleek, premium look. At $89.99, the Major V Midnight Blue is a genuinely strong buy for anyone who wants a reliable daily headphone without paying premium prices. It’s also worth mentioning that the Cream and Brown variants are also discounted to $89.99, though from a lower original price of $99.99. Marshall Major V Midnight Blue - $89.99 | 47% off on Amazon This Amazon deal is US-specific and not available in other regions unless specified. This is a first-party seller link (at the time of article publishing); ensure that you also purchase from a first-party seller link only. If you don't like it or want to look at more options, check out the previous deals that we have covered, OR you can also visit Amazon US deals page. Get Prime (SNAP), Prime Video, Audible Plus or Kindle / Music Unlimited. Free for 30 days. As an Amazon Associate, we earn from qualifying purchases.
  • Recent Achievements

    • Reacting Well
      Almohandis earned a badge
      Reacting Well
    • First Post
      Cosminus earned a badge
      First Post
    • One Year In
      ThatGuyOnline earned a badge
      One Year In
    • Week One Done
      Jeroen Wilms earned a badge
      Week One Done
    • Week One Done
      rolfus earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      482
    2. 2
      +Edouard
      185
    3. 3
      PsYcHoKiLLa
      122
    4. 4
      Steven P.
      84
    5. 5
      neufuse
      73
  • Tell a friend

    Love Neowin? Tell a friend!