1

For a while I've had a website, that grew and grew for years into several hundred individual php files. About 2 years ago I made a mobile version of the same website. I decided to do this by copying only the main files, and adjusting the header and the footer for mobile friendly. The rest worked as expected; I'd say 50% compatibility at this point. Some of the sub-directories I decided that instead of copying them I could do a link using ln -s. This has solved the remaining 45%.

Recently I ran into a troubling situation. One of the files inside of this sym-linked subdirectories needs to be drastically different for the mobile version and for the full website. For normal files I would just make a hard copy, and modify the contents, but for a file within a sym-linked directory this is impossible.

Has anyone run into this? How can I solve this?

Here is the file layout & example:

/var/www/domain.com/
/var/www/mobile.domain.com/

/var/www/mobile.domain.com/images -> /var/www/domain.com/images
/var/www/mobile.domain.com/tools -> /var/www/domain.com/tools

If I need to modify a file /tools/help/wizard/configure.php then I can't have a unique file JUST for that file

One approach that I thought of, but don't like is modifying the mobile header file to check something like:

if (file_exists("/var/www/mobile.domain.com/_overwrite_/" . $requested_file)) {
    // ... include it instead of running the actual file
}

This seems like an ugly approach. Similar can be done with Apache directives (I don't know them, but I'm guessing it's ModRewrite)

5
  • I guess there isn't a better approach. Mod-rewrite is used to redirect/modify HTTP requests, sent by user(browser), not by php. Apache is a "bridge" between user and php Commented Jan 16, 2012 at 22:05
  • can I ask what are you switching here, is it the presentation logic of your page or are there business logic changes to support the different presentation as well for the mobile platform? Commented Jan 16, 2012 at 22:06
  • 1
    Do you use an autoloader or simple include statements? Commented Jan 16, 2012 at 22:06
  • @Scuzzy: the amount of content, for one, is just overwhelming for mobile. Secondly the heavy use of javascript on that page is "unfair". I could remove all that for both websites, but then full PC website would look plain. Commented Jan 16, 2012 at 22:08
  • @ManosDilaverakis: each php file is individual URL. each file begins with require_once 'header.php'; and ends with require_once 'footer.php'; Commented Jan 16, 2012 at 22:09

3 Answers 3

1

You are posing the question as a filesystem level issue. To solve it at the filesystem level, you could simply make symbolic links to every entry within a directory except that entry you want to override.

If you want a special /tools/help/wizard/configure.php for mobile.domain.com, then instead of a directory level soft link

/var/www/mobile.domain.com/tools -> /var/www/domain.com/tools

You could could make soft links to every entry in tools except for help:

/var/www/mobile.domain.com/tools/file1 -> /var/www/domain.com/tools/file1
/var/www/mobile.domain.com/tools/file2 -> /var/www/domain.com/tools/file2
/var/www/mobile.domain.com/tools/file3 -> /var/www/domain.com/tools/file3

make a physical directory tools/help, and make soft links to every entry in there except for wizard; and make a physical directory wizard, and make soft links to every entry in there except for configure.php:

/var/www/mobile.domain.com/tools/help/fileH1 -> /var/www/domain.com/tools/help/fileH1
/var/www/mobile.domain.com/tools/help/fileH2 -> /var/www/domain.com/tools/help/fileH2
/var/www/mobile.domain.com/tools/help/fileH3 -> /var/www/domain.com/tools/help/fileH3
/var/www/mobile.domain.com/tools/help/wizard/fileW1 -> /var/www/domain.com/tools/help/wizard/fileW1
/var/www/mobile.domain.com/tools/help/wizard/fileW2 -> /var/www/domain.com/tools/help/wizard/fileW2
/var/www/mobile.domain.com/tools/help/wizard/fileW3 -> /var/www/domain.com/tools/help/wizard/fileW3
/var/www/mobile.domain.com/tools/help/wizard/configure.php`

Now, the includes in mobile.domain.com will pick up customised physical files where they exist, and via the softlinks will pick up the files from the main site where customisations don't exist.

It will not be tedious to create directories with all those soft links, just write a simple shell script to list the contents of the source directory and make a soft link in the target directory for each one.

It will be tedious to update the soft links in /var/www/mobile.domain.com every time you add a file to /var/www.domain.com. This is a consequence of architecting your original code for a single site, and then attempting to extend it to multiple sites later. Re-architecting your code could improve how it handles addition and removal of files, but that requires an investment of effort. I take it from your question you are trying to avoid re-architecting your code.

I'll note that there is a way to handle this problem at the PHP object hierarchy level. If your PHP files for domain.com define objects and classes, then you could have your PHP files for mobile.domain.com define subclasses of domain.com, with only the modifications you need. In that way, mobile.domain.com need only contain those PHP files to define the differing modules.

Sign up to request clarification or add additional context in comments.

1 Comment

+1. Not my favorite solution, but at least it's one I haven't considered yet.
0

Try setting a constant in the mobile header :

define(IS_MOBILE_VIEW, true);

Then in the file you are loading that has to be different for the mobile version just do a simple check at the beginning and load the appropiate other file :

if(defined(IS_MOBILE_VIEW) && IS_MOBILE_VIEW) {
    require_once '/var/www/mobile.domain.com/other_file.php';
} else {
    //rest of file, you can also use a return; in the if clause 
    //or something similar if you don't want to enclose the whole logic in
    //an else clause
}

Comments

0

I don't see any way that is better than what you described. In fact if all your include statements are in one place (header.php?) then it looks like a decent solution, since you'll be able to hide this logic from the rest of the app. I see no downside to it.

If you want to take this further you could also abstract this into a function and replace your include calls with it, making it possible to overwrite any file you want later on without having to touch the code.

In the future you might also want to look into PHP's autoload functionality.

1 Comment

Your answer is confusing. Abstract what into a function? Also who said anything about classes? header.php checks if the user is logged in, and displays beginning of HTML. That's all

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.