• 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

    • memories! completely forgot about alcohol 120%!!!! man this list just makes me think of all the exciting times! everything's become so sterilized these days. 
    • Salesforce to acquire Fin, formerly Intercom, for over $3 billion by David Uzondu Image via DepositPhotos.com Salesforce today announced that it has reached an agreement with customer support software company Fin to buy the company for around $3.6 billion. Salesforce expects the transaction to close in the fourth quarter of its fiscal year 2027, and the acquisition will not change its financial guidance previously announced on May 27. Marc Benioff, CEO of Salesforce, in its press release, claimed that acquiring the startup gives Salesforce an immediate AI agent that resolves support tickets across email, WhatsApp, SMS, and Slack: You might know Fin by its former name, Intercom. If you have ever been on a website or inside a mobile app and noticed a little chat bubble widget floating in the bottom right corner of your screen, often featuring a friendly face and a message like, "Hi! How can we help you today?" you were almost certainly looking at Intercom. Intercom became Fin just last month, transforming itself into an "AI-first" platform that handles customer issues with little need for humans to intervene. The new name originates from the company's highly successful AI customer service agent, which it launched way back in 2023. This digital assistant supposedly resolves about 76% of customer service volume end-to-end on its own, so the business rebranded to match its primary software tool. Fin's new owners, Salesforce, went on an acquisition spree over the last few years, and some of them you might recognize, like its 2020 $27.7 Billion acquisition of Slack. The last few months saw the enterprise giant buy several startups, including m3ter, Momentum, Cimulate, and Contentful. Salesforce said that when the Fin deal pulls through, customers will "deploy AI agents across" various service operations, which will complement Agentforce, the platform that enables businesses to deploy customizable autonomous digital workers.
  • Recent Achievements

    • 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
    • One Month Later
      Leroy Jethro Gibbs earned a badge
      One Month Later
    • Conversation Starter
      flexorcist earned a badge
      Conversation Starter
  • Popular Contributors

    1. 1
      +primortal
      504
    2. 2
      +Edouard
      201
    3. 3
      PsYcHoKiLLa
      127
    4. 4
      Steven P.
      82
    5. 5
      ATLien_0
      76
  • Tell a friend

    Love Neowin? Tell a friend!