TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Includes are driving me nuts!!! (http://www.talkphp.com/general/2605-includes-driving-me-nuts.html)

Aaron 04-09-2008 12:35 AM

Includes are driving me nuts!!!
 
Im going to go insane figuring out a way to make these include files work. I finally get one directory to work, and then another one goes and doesn't work.

How do you guys work on your local machine?! Paths need to be relative to be able to transfer from one machine to another, and relative paths SUCK!

Aside from include problems, where I can rest in peace by just adding a "../" for every directory I go down (it would be great if I didn't have to), I need to deal with these smarty problems.


smarty_libs.php:
PHP Code:

<?php
require('Smarty.class.php');

$smarty = new Smarty;

$smarty->template_dir "templates";
$smarty->config_dir "config";
$smarty->cache_dir "cache";
$smarty->compile_dir "compile";
?>

index.php:
PHP Code:

<?php require_once('smarty/smartlibs.php'); 
$smarty->assign("location","here");
$smarty->display("index.tpl");
?>

File hierarchy:
------------------
index.php
/include
/smarty
/templates
/cache
/config
/compile
smarty.class.php
smarty_libs.php
-------------------

Can somebody stop the headaches? I go down a directory and everything breaks. I can't do absolute paths from the root directory either.

delayedinsanity 04-09-2008 02:49 AM

I wish I could remember who it was who suggest this, so I could give them credit, but have you tried using $_SERVER['DOCUMENT_ROOT']? I've started to employ this between my test machine and my remote server and it works quite well. Say if you're local is a windows box, and the document root is C:\my documents\htdocs, and your remote is linux, and it's /www/public_html, using document root, you never have to worry about relative paths, you just

include($_SERVER['DOCUMENT_ROOT'] . "/path/to/include.php");

It's just like using

include("C:\my documents\htdocs\path\to\include.php"); or
include("/www/public_html/path/to/include.php");

except that since you're calling the _SERVER array it will automagically fill in the root based on whatever machine you may transfer the script to.
-m

Tanax 04-09-2008 10:30 AM

This is the problem:

PHP Code:

$smarty->template_dir "templates";
$smarty->config_dir "config";
$smarty->cache_dir "cache";
$smarty->compile_dir "compile"

You need to do like:
PHP Code:

$smarty->template_dir "c:/path/to/server/www/projectfolder/templates";
$smarty->config_dir "c:/path/to/server/www/projectfolder/config";
$smarty->cache_dir "c:/path/to/server/somewhereoutside_www/cache";
$smarty->compile_dir "c:/path/to/server/somewhereoutside_www/compile"

The compile and cache isn't supposed to be in the www directory.
And also, you need to change php.ini and include that dir..

If you need more detailed description, you could add me to MSN(PM me for my msn).

Aaron 04-09-2008 10:30 PM

I figured out a nicer way from delayedinsanity's.

PHP Code:

$install_dir "Projects/";
set_include_path($_SERVER['DOCUMENT_ROOT'] . $install_dir);
//@include("anything") 

Can some of you crazy "I've been doing PHP since age 3" people on this forum give some suggestions?

Edit: DONT USE THIS! :( It screws up any frameworks you might be using.

xenon 04-10-2008 06:23 PM

Here's a suggestion:

in index.php (or what ever your bootstrap file is):
Code:

define( 'ROOT_DIR', dirname(__FILE__));
then, just use that constant:

Code:

require_once ROOT_DIR . '/abcd/lib.php';
If you don't use a bootstrap file, then you will have problems, and then you'd better do something like:

Code:

define('ROOT_DIR', $_SERVER['DOCUMENT_ROOT'] . 'your_app_dir');
Go figure which way works better for you and stick to that.

Aaron 04-11-2008 01:17 AM

Whats a bootstrap file?

xenon 04-11-2008 05:03 AM

Quote:

Originally Posted by Aaron (Post 13375)
Whats a bootstrap file?

The main file of your application that performs basic initialization and handles requests from users. If you were used to write centralized, extensible applications, you would use a bootstrap to ease your work. A bootstrap example: various frameworks index.php file (Cake, CI, Zend, etc.).

Bootstrapping (computing) - Wikipedia, the free encyclopedia


All times are GMT. The time now is 06:28 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0