views:

374

answers:

5

Now I know that you can use OOP and MVC to organize, but that is just for PHP.

Let's say, I add a new window that pops up when a user clicks on a link and it displays a form with JS validations and it is CSS styled.

Here we got 4 codes: JS, CSS, PHP and HTML (with some PHP snippets).

How would you organize all these codes? Because when I got 50 windows codes are spreaded everywhere and for me to change behaviour of delete a window, I have to play detective. I crunch everytime I have to add a new window with JS, CSS and so on.

I have thought about the structure. wouldn't it be better if you got a separate "module" for each one of the window. E.g. a folder for each one of the window. In that map you place one CSS-, one JS-, one PHP-, and one HTML-file? Then you got a very nice structure that aren't messy and you dont mix all windows with each other in one big JS and CSS file.

What do you think? And I would appreciate suggestions of how to organize these 4 kind of codes.

+2  A: 

I generally have my PHP pages in one folder, (maybe 10 files if it's a medium size site) then a subfolder in there called media, in which I put a css folder, a js folder an img folder, a swf folder etc.

I have 2 css files, one's a reset, the other has the style for the whole site written in chunks. I use a class on the body tag to target different page layouts.

The js folder has jquery, a file that's run on every page, then specific files for individual pages.

This keeps things pretty straight forward really.

Rich Bradshaw
+1  A: 

If you are looking for examples on how to design an application, having a look at the many frameworks is a good start. Even just their file structure will give you an idea. Typically, they organize their code into modules where both the PHP code and HTML templates also reside. None in particular are better to look at, but try: CakePHP, Symfony, CodeIgniter, or Agavi.

They will not do a great job on suggesting how to organize JavaScript and CSS files, however. When I make an application, I typically only have a handful of CSS files. I am surprised that you seem to need one per page but if you do: embed them. The advantage of external style sheets is completely lost if there is nothing reusable about their styling. JavaScript files, again, if they are not reusable you should simply embed them. Less HTTP requests per page load makes everyone happy.

When you find yourself scouring for a particular file that stubbornly will not surface itself, grep is an invaluable tool. Here is a random article that illustrates its usage.

erisco
in development i find it very easy if i look at a popup window, and know, all files (css, php, html, js) are in THIS map. then when i wanna reuse anything i just make a copy of that folder and its done. better than to play detective everytime i want to add a new popup window or reuse same code-collection. for me it seems to be a matter of oop vs procedural. all relative codes for an object (eg popup window) in one place. not spreaded throughout lot of files in different locations. so when i want to delete a popup window for example, i just delete that folder, and its done:)
weng
of course, in production, i just copy everything to one js file, and one css file. then it wont matter:)
weng
+1  A: 

If you can/need I'd create top level folders for JS, CSS, PHP, etc that would contain code that could be used across different windows. There's no need to have 50 copies of the same CSS file if they are all the same or even mostly the same code.

Then create a folder for each "window" that has seperate css, js, etc folders. Here you could put the files that are specific to that particular window. This way if you're only changing 1 css rule or js function, that is used by every window you could change it in 1 place.

If you ever need to change the rule for just 1 window, put the rule in the "local css" folder for that window and it will override your default. (That is if your HTML Links it after the "global css")

joatis
yes that was indeed a good structure. the main problem with what i wanna accomplish is that you have to embed all files (css, js, php) for every popup window:) would be great if i could make an automator php script that embeds everything in every folders...
weng
+1  A: 

If you expect the same user to open a few of these different popup windows during a single visit, for their sake you need to consolidate your files so they are cached and the end users doesn't need to load all the CSS/JS again on each popup.

The folder approach is fine for the images and the JS. Unless every popup is drastically different, I would suggest a single CSS file for your own sanity. So your folder structure might look like this:

css/
* layout.css
popups/
* add_new
   - add_new.js
   - logo.png
   - add_new.php
* delete
   - delete.js
   - other_logo.png
   - delete.php

Now, before you deploy you can decide if it makes sense to compile your JS into a single file, or if the separate files would be best. (For instance, if the user opens 30 of the 50 windows every visit, use a single file)

Doug Neiner
+2  A: 

I like Django's way to organize folders. Let's try to imagine and adapt it to your php project:

Root folder is the Project folder, let's say the name of the website. It contains

  • common shared settings and values (ie. database access values, paths etc.), maybe helper functions (not Object Oriented), call it settings.php and/or utils.php or whatever
  • a media folder that also has its own structure:
    • css folder, for general css (ie. reset.css and common.css, for defining a general layout)
    • image folder, for common shared images
    • js folder, for common shared javascript code
  • a template folder containing static common pages not belonging to particular categories of the website

Each Root subfolder is an expression of an application of your project (ie. registration, news, announcements, faq, contacts, forum, ...) and contains:

  • a model folder in which you put your Models in PHP (MVC pattern)
  • a controller folder where you implement the Controllers in MVC pattern
  • a view folder in which you put the Views of your MVC pattern (i.e. quite static php pages responsible just to present the results passed by the Views)
  • a media folder structured exactly in the same way you structured the one for the root folder. In this folder you put elements only belonging to the particular part of the website you're developing.

For connecting the components, you could directly call / include them based on their paths, or you could implement a php file inside the root directory and each subfolder that is responsible for mapping urls and redirect the requests. Call it index.php or urls.php or connector.php, whatever.

It may seem redundant but it is not, and provides a high quality separation of concerns.

bodom_lx
so these components, are they just different pages or could one page have different components? is there any good brief tutorial explaining just the folder hiearchy and not so much in programming details so i can get hang of the component structure?
weng
the "components" are just folders, so you're free to either use subfolders for representing a real component (or sub-activity) of your web application or use a subfolder for each "page" of your website. You are writing a website from scratch without using frameworks,so you're free to do what you most feel confortable with. As far as I know, every Django project structure explanation is highly binded to python code. Moreover, I changed some directory names that are Django related to fit your needs.
bodom_lx