• 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

    • I totally disagree. Very little good comes out of governments all around the world manipulating everything they can and usually the people are not the benefactors. What you say about being restricted and expensive sounds almost like the arguments against firearms and why banning them will protect people as if making something illegal somehow will prevent the criminals from having and using them. AI being far less mainstream could simply mean the average person will not benefit, but "big brother" and the corporations will benefit, which is almost for sure NOT a good thing.
    • I do apologize to the author Mr. Sen for my rude comment, questioning his knowledge of the subject. It is I whom lacked knowledge of the subject. Sorry!
    • Hello All Have a MSI Pro B650 VC Wifi Rev 1.0 motherboard Ryzen 7 7700X Radeon 7800XT OC 16GB 32GB Teamgroup DDR 5 5600mhz Samsung 990 Pro 1TB Boot NVMe Samsung 990 Pro 2TB Game NVMe Lian Li Lancool Black ARGB 216 Case Seasonic Focus GX 750 Watt Power supply   Wondering today what is best spot to plug in the following items on system for performance and not bottle neck anything if i can help it Creative Pebble Pro USB C or A Speakers, ((Powered by External USB C to C PD Adapter)  Logitech G513 USB Gaming Keyboard Logitech G502X Wired Gaming Mouse Cyberpower UPS USB Cable for UPS Power Management/System shutdown External drives connected occasionally are as follows---WD My Book 8TB (primary backup drive)   Seagate 8TB in External USB 3.0 Enclosure,  Seagate Portable 1TB USB 3.0 drive,   WD My Passport (Blue) 2TB, and WD My Passport (Red) 2TB,    WD Elements 500GB USB 2.0 External (Oldest one, Christmas 2003)       **Do have a 7 Port Powered  USB Hub as well, but when i use that--that leaves only the USB Flash spot for something to directly connect to system if needed.    Rear USB C 2x2 unused right now as moved the Creative speakers off it to USB A port next to it, with a USB C to A Cable, as figured speakers didn't near audio from USB C port and tie up the high speed port**   Front Ports trying to limit use of, so i don't have Front I/O port go bad again, already had it replaced once by Lian Li support all the way from Taiwan over night ((Do get extra nervous at times on things,  so i might just be extra nervous for nothing lol))
    • "connect with audiences" is the most obvious corporate speak you can think of. I only bought Need for Speed from EA because it was the only racing game with cops in existence and I dig that. Now that they killed off NFS franchise, I have nothing to spend money on. EA is officially dead for me, just like Ubisoft which I've been boycotting for some 20 years now...
  • 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
      494
    2. 2
      +Edouard
      203
    3. 3
      PsYcHoKiLLa
      127
    4. 4
      Steven P.
      82
    5. 5
      ATLien_0
      79
  • Tell a friend

    Love Neowin? Tell a friend!