Page 1 of 1

Front-end and back-end shared or separate libraries?

Posted: Sat Aug 14, 2010 7:37 pm
by letseatfood
Is it good practice to separate the front-end PHP library from the back-end?

It seems, at the moment, better to share a library between the front-end and back-end.

I am curious to hear opinions about this.

Thanks!

Re: Front-end and back-end shared or separate libraries?

Posted: Sat Aug 14, 2010 7:40 pm
by John Cartwright
All my applications on a given server share the same library. However, in the context of one application, yes it is a good idea to share your library between different "modules". It will certainly make things a lot easier when upgrading given libraries, but it does come at the cost of potentially breaking functionality elsewhere (which is why it is important to unit test!).

Re: Front-end and back-end shared or separate libraries?

Posted: Sat Aug 14, 2010 9:49 pm
by letseatfood
@John - Thanks for your response. I can see how "potentially breaking functionality" could be an issue, but with proper testing, prevented.

Two related questions I have about sharing my current library with two or more "modules" are:
  1. Where to place the library directory within the server?
  2. How to refer to the files within the library? For example, absolute or relative URLs?
For example, I currently have the following structure:

Code: Select all


[root]
  |
  |- [admin]
  |    |
  |    |- [lib]    <−− Library I would like to share
  |    |- index.php
  |
  |- index.php

Do you think I should move the [lib] directory, which I would like to share with other modules within the [root] directory into the [root]?

I appreciate any advice you might give.

Thanks!

Re: Front-end and back-end shared or separate libraries?

Posted: Sun Aug 15, 2010 8:03 am
by PHPHorizons
Hello letseatfood,
Where to place the library directory within the server?
There are really two main places for the library to go. One: above the web root folder. Two: inside the web root folder (or one of it's subfolders). If you can put the library above the publicly accessible web folders, that is best. Otherwise, it doesn't matter too much where the library is.
How to refer to the files within the library? For example, absolute or relative URLs?
I prefer using absolute *paths*. I do not use URLs at all for including files. URLs can be used, but if the files are on the same server, use a file path instead. It's far more secure and efficient. Either way, use an absolute path/url. I define a constant that points to my library folder. Then, anytime I need a library, it goes like this:

Code: Select all

PATH_LIBRARY . 'some_particular_library' . DIRECTORY_SEPARATOR . 'some_file.php'
Cheers

Re: Front-end and back-end shared or separate libraries?

Posted: Wed Aug 18, 2010 10:35 am
by letseatfood
@PHPHorizons - Thanks a lot! Is `DIRECTORY_SEPARATOR` a constant that holds a forward or backward slash?

Re: Front-end and back-end shared or separate libraries?

Posted: Wed Aug 18, 2010 11:21 am
by PHPHorizons
You're welcome letseatfood,
letseatfood wrote:Is `DIRECTORY_SEPARATOR` a constant that holds a forward or backward slash?
Absolutely. It's handy when a script might be used on Windows or Linux, since this value is tied to the operating systems own directory separator.

Cheers

Re: Front-end and back-end shared or separate libraries?

Posted: Wed Aug 18, 2010 11:46 am
by letseatfood
@PHPHorizons - Thanks! I had never thought about that, what a useful tip.