php includes and relative link to directories

I have a series of includes in a folder called includes in the main directory. And these have to appear on every page, whatever directory the page is in.

<?php include 'includes/file.php'; ?>

or

<?php include '../includes/file.php'; ?>

What’s the neatest way of doing this to keep the link relative? as in the ./ has to change on each Freeway page created depending of what directory the page appears in, rather than one code set on all pages, on the page master for example.

So I would have something like:-

<?php include '(the current directory)/includes/file.php'; ?>

I have a lot of pages with an inline box model, and the master will break after the page content is created, so I’d like to get this with one hit using relative links if possible. Before the full site build.


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

What you really need is an absolute link, rather than a relative one.
The easiest way to make that happen is to figure out what the root-
relative link is to your includes folder, then prepend that to the
request for the file. So if your site root is here:

/home/yourname/htdocs/

Then make your include like this:

require_once('/home/yourname/htdocs/includes/file.php');

If you do that, no matter where in the directory structure the page
you are including from, you will get the file you request. Absolute
links are completely unambiguous.

Walter

On Dec 9, 2008, at 2:07 PM, WebWorker wrote:

What’s the neatest way of doing this to keep the link relative? as
in the ./ has to change on each Freeway page created depending of
what directory the page appears in, rather than one code set on all
pages, on the page master for example.


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

That’s the problem, I’m having build the site on-line in a sub folder, so the includes folder will change. And I’ll have to change all the 120+ Freeway pages with the 5 includes for each page, scattered around various directories. urgh!

I suppose I could join two includes together to create the URL…

one holding the root-relative link part

/home/yourname/htdocs/

The other holding

inlcudes/file.php

Then I just need to change the include containing the root link when the site moves to the main directory.

Unless there is a better way?


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

No, you’ll still have a bootstrap problem there. You still have to
somehow get the root location file into each page without knowing
where that page is. There is a solution, but it can be kind of tricky
to set up. Have a look at auto_prepend() in the PHP manual. You set
this up using an .htaccess file usually, and that way you can have a
separate one for each subdirectory if you like.

##in your .htaccess file

php_value  auto_prepend_file   /root/path/to/your/file.php

And then file.php can then include a bunch of other files relative to
itself, thusly:

//in file.php

$base = dirname(__FILE__);
require_once($base . '/file_one.php');
require_once($base . '/file_two.php');
require_once($base . '/file_three.php');

In order for this to work, you will need to have the right sort of
permission in your Apache configuration that will allow you to modify
the PHP configuration through .htaccess. If this causes your server to
emit a 500-series error, just delete those lines and holler to your
sysadmin. There’s a specific tweak they will need to make at the
Apache config level, then restart the Web service, and then it should
work.

Also note that whatever you put in here will be prepended to every PHP
file in the entire directory (and all its children), so make sure it’s
included using require_once, and also that it doesn’t actually emit
anything, because otherwise you will have a real hard time debugging
the result, or a lot of blank pages, or both.

Walter


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Would this same method work locally in MAMP?

I’m still previewing the site locally before committing to an upload (hence seeking a relative link method)


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

It should. The critical thing you may have to adjust is the AllowOverrides directive in the httpd.conf (set it to All). I’m not sure how or if MAMP exposes that to you, I don’t use it here.

Walter


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

This is not looking at easy as first thought.

What I’m doing is having a site map (list of page links in 5 columns) at the bottom of each page of the site. So each link in the site map needs this a relative link or the solution you’ve offered.

My first thought was to just add each column list of text links directly into 5 Freeway pages and use the “php make insert page” action which works great - a simple solution and maintains all the links easily, but this option does not Validate - I need the site to validate if at all possible.

Then I thought WebYep, but that does not validate either, so its back to looking at hand coding these links, but use 5 text includes to each column.

Before I make a decision which way to go, does anyone have any alternative solutions?


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

I think what I’m saying is. Is there any way of making the “php make insert page” action just output the parts (text and links) I need to make it validate?


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Could you post this on a public server somewhere so we can see why
it’s failing to validate? There may be something simple you could do
here to get around the problem, and keep your workflow in Freeway as
much as possible. This is a very comfortable rung of the ladder, after
all. If you need to have relative links, then you can have them, as
long as you know where you are starting from. If these are
navigational links that you are referring to, then Freeway will
happily build them for you without question or effort. If they are
links to PHP files, then there’s the PHP Use Insert Pages Action to
try. That even uploads the include page to your Resources folder, and
Freeway will guarantee that there will always be only one such include
page in the entire site (as long as the site is driven by a single
Freeway document).

Finally, you might want to try my TemplateHelper Action. In “partial”
mode, it will snip a single DIV (or group of DIVs) out of the page and
save it as a separate file alongside its parent page. It includes
fixes for the most common validation problems – ID clashes – as well
as a simplistic technique for removing excess positioning CSS.

Walter

On Dec 10, 2008, at 8:21 AM, WebWorker wrote:

I think what I’m saying is. Is there any way of making the “php make
insert page” action just output the parts (text and links) I need to
make it validate?


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

If I follow the “PHP Use Insert Pages Action” route the best option on balance in maintaining link for the future, as an example the following code does not validate at Validation Results - W3C Markup Validator

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>home page</title>
<meta name="viewport" content="width = 1050, minimum-scale = 0.25, maximum-scale = 1.60"/>
<meta name="generator" content="Freeway 5 Pro 5.3.1"/>
<link rel="stylesheet" type="text/css" href="css/sheet1.css"/>
<style type="text/css">
<!-- 
body { margin:0px; background-color:#fff; height:100% }
html { height:100% }
-->
</style>
<!--[if lt IE 7]>
<link rel="stylesheet" type="text/css" href="css/ie6.css"/>
<![endif]-->
</head>
<body>
<div id="PageDiv" style="position:relative; min-height:100%">
	<div id="item1" class="f-x2" style="position:absolute; left:0px; top:0px; width:262px; z-index:1; overflow:visible">
		<p class="f-lp">
	<p><a href="index.php">link 1</a></p>
	<p><a href="index.php">link 2</a></p>
	<p class="f-lp"><a href="index.php">link 3</a></p>
</p>
	</div>
</div>
</body>
</html>

this being the insert

<p><a href="index.php">link 1</a></p>
	<p><a href="index.php">link 2</a></p>
	<p class="f-lp"><a href="index.php">link 3</a></p>

dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Aha. you are inserting a paragraph inside another paragraph. P tags
are block-level tags, and they cannot be nested. To work around this,
use the Crowbar Action where you are currently using the Markup Item
to insert your included code. Freeway wraps non-text items in P tags
with reckless abandon, and CrowBar works around that.

Walter

On Dec 10, 2008, at 10:45 AM, WebWorker wrote:

> [snip]

> 		<p class="f-lp">
> 	<p><a href="index.php">link 1</a></p>
> 	<p><a href="index.php">link 2</a></p>
> 	<p class="f-lp"><a href="index.php">link 3</a></p>
> </p>

[snip]

dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

OK. I put

<?php   require_once ("inc.php");  ?>

Into the Crowbar action? Which is applied to the div the HTML was surrounding? It does not work?


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Sorry the “HTML Insert” was surrounding.


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Take a look at the instructions on the ActionsForge page for Crowbar.
There’s lots of different ways to use it. I suspect that you were
using an inline Markup Item in your layout previously, what I
recommend you do is insert an instance of CrowBar where that MI was
previously, then paste whatever code the MI contained into the Code
dialog of the CrowBar.

Walter

On Dec 10, 2008, at 11:26 AM, WebWorker wrote:

Sorry the “HTML Insert” was surrounding.


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Walter, Is there no other way of doing this other than your suggestion of using the .htaccess method - decided to use text include to make the links

Its a mind bending conundrum, to create relative links in and out of directories. As you say you need to know the relative place of each page from anywhere else on the site.

Its OK to get the includes appear into each page with two Freeway templates that are 1 (include.php)

<?php require_once ("includes/col1.php"); ?>

or 2 level pages

<?php require_once ("../includes/col1.php"); ?>

Where the fun starts is creating the footer links in the include files, in an out of the pages that are either one or two levels deep. (Sort of fix one break another situation) Working on something like this - thinking I would have an file include showing the root path.

<?php
ob_start(); // start buffer
include_once ("dir.php");
$dir = ob_get_contents(); // assign buffer contents to variable
ob_end_clean(); // end buffer and remove buffer contents
?>
<?php
$folder_path ="$dir"."folder/";
?>
<strong><a href="<?php echo "$folder_path"; ?>">Folder Name</a></strong><br />
<a href="<?php echo "$folder_path"; ?>index.php">Page Name</a><br />

I can only make it work if all the pages are in the same directory. Unless I have more than one set of includes, which defeats the objective of having these all in one place to edit.

Think I’m going round in circles with this. Could you suggest anything?


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

This is a hard thing to get your head around, I agree. What you’re going to need to do is get your navigation to be “smart”. What I mean by that is you’re going to need to add some PHP logic to the navigation footer that “reads” the URL of the current page and determines where it is in the hierarchy, and then calculates the correct number of hops to any other folder. Imagine the following:

You work out a way to get the same footer into every page. I still maintain that the correct way to do this is with the htaccess trick.

Now, in that footer, rather than a static HTML representation of the links, you have a function which can create a prefix for the links, and then an array of links to apply the prefix to.

Something like this?

//in your auto-prepend file:
define('SITE_ROOT',dirname(__FILE__);
//in your page
$here = dirname(__FILE__);
require_once(SITE_ROOT . '/smart_footer.php');
//in smart_footer.php
$base = count(preg_split('~/~',SITE_ROOT,-1,PREG_SPLIT_NO_EMPTY));
//$base is now a number representing the distance of the site root from the server's filesystem root
$offset = count(preg_split('~/~',$here,-1,PREG_SPLIT_NO_EMPTY));
//$offset is now the same thing for the current page
$shim = str_repeat('../',$base - $offset);

$options = array('folder/subfolder/page.php'=>'Something','page.php'=>'Something Else','folder/'=>'Folder');
$nav = '<p class="footer">';
foreach($options as $p=>$o){
    $nav .= '<a href="' . $shim . $p . '">' . $o . '</a> ';
}
$nav .= '</p>';

Now really this should get wrapped up in a function, rather than all this procedural code lying around loose, but that’s the basic premise. You get the main auto-include file to declare where it is, then the page declares where it is, and between those two vectors you figure out how far to navigate UP before navigating back DOWN.

I have not tested any of this, it may not work at all, but it would be easy enough to try it out and see if it’s going to work.

Otherwise, you can do the same thing except with the URI, which is easily read and understood from the individual page level using the superglobal $_SERVER['REQUEST_URI'].

Walter


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Thanks Walter, I’ll need time to take and test what you’ve done here.

In the mean time (as time is pressing) what I’ve done is to use text (php) files to include the text in the 5 footer columns. An include in HTML markup in each or the 5 columns in each Freeway master page. And as the site is either 1 or 2 levels deep, create two master pages with absolute links. These masters can be used to create new pages form as and when in the future.

<?php include 'includes/col1.php'; ?>
<?php include 'includes/col2.php'; ?>
etc

and

<?php include '../includes/col2.php'; ?>
<?php include '../includes/col3.php'; ?>
etc

So all Freeway pages can find the includes folder for each folder as pages are created at a later date.

Then create a directories.php file in the includes folder

<?php
$absolute_url = "http://localhost:8888/local_site_folder/";
$main_dir = "";
$folder1_dir = "folder1/";
$folder2_dir = "folder2/";
$folder3_dir = "folder3/";
etc...
?>

Here I can control the absolute URL and folder names to work locally.

Then each column with links can go like this (e.g. col1.php)

<?php require_once ("directories.php"); ?>
<a href="<? echo "$absolute_url"."$folder1_dir";?>page1.php">link name</a><br />
<a href="<? echo "$absolute_url"."$folder_dir";?>page2.php">link name</a><br />
Copyright © <? echo date("Y"); ?>
etc...

It all keeps it reasonably controllable in one includes folder. I just change the $absolute_url on the server when I FTP up the includes folder separately. So I can work locally and remote. Luckily Freeway does not overwrite changed files in the site folder on the server, and leaves them be. So this should work. Any changes in the future can be made to these text files to control the footer site wide.

This is because of Freeways inability to cope with inline box model content on master pages. Removing all this Freeway content to these text files stops having to copy and paste changes to 5 columns in 100+ Freeway pages at a later date.


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options