Reputation: 79686
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.
Upvotes: 12
Views: 9287
Reputation: 13
If you are only dealing with the file types mentioned, my best experience is to have folders and files separate everything, and it also keeps it organized with everything Super Easy to Find as your Site Grows & Expands!
OK, so as for folders, you want all you're main HTML, PHP files in the:
/public_html/ (or whatever it's called on you're host. ie. The main area with the cgi-bin inside that is viewable as your main web folder).
eg-:
NEXT is the CSS and Javascript (JS), these will have their own folders inside your /public_html/ folder. and each file ending in .css or .js go in those particular folders obviously...
eg-:
And that is That! As easy as it comes to remember, and once you are advanced enough at coding this is the best way to do things. The more advanced you get, the more file types, and also the more 'neat' you are going to want to make it look = organized, you may want to add an /includes/ folder before the /css/ /js/ /templates/ /images/ & so on it goes!
I hope this works out for you, or one of them!
THIS IS MY 1st QUESTION ANSWERED, SO I'M PROUD OF MYSELF & HOPE THE POSTER LIKES MY SOLUTION, GOOD LUCK!
Upvotes: 1
Reputation:
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
Each Root subfolder is an expression of an application of your project (ie. registration, news, announcements, faq, contacts, forum, ...) and contains:
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.
Upvotes: 7
Reputation: 66191
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)
Upvotes: 1
Reputation: 3435
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")
Upvotes: 1
Reputation: 14319
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.
Upvotes: 1
Reputation: 72975
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.
Upvotes: 3