Jump to content

How do you organize your PHP scripts?


bilis_money

Recommended Posts

Actually i want my PHP codes to be professionally organize,

Yeah, i can understand OOP, but how do you organize them properly?

 

Let say i have a very very BIG project, now what do you do for your first step

to organize your php script? then the second step,the third step and so on.

 

How do you also organize the files and folders?

 

I want to hear your experiences.

 

 

Thanks in advance.  :D

Link to comment
Share on other sites

this would depend on how you approach things in generally. i tend to send ALL requests (via a rewrite) through a single file - index.php, rather than having stuff like account.php and login.php, etc. this way, i'll group all my site code into a directory called, for example, app, and the rest in respective places of their own. eg - my root directory might look like this:

 

- app/

  - config/

  - framework/

  - templates/

  - uploads/

- css/

  - main.css

- js/

  - scripts.js

  - mootools.js

- img/

  - logo.gif

index.php

favicon.ico

 

whereas only css/js/img directories and index.php and favicon.ico are web accessible. very often i'll move the whole 'app' directory outside the webroot altogether, just for safety. in summary - i find the directory structure the most important for keeping organised, as well as naming conventions of my files. if i have enough files that have much in common, i'll then probably group those into their own directory with a name to suit - eg, classes, models, controllers, funcs, etc

Link to comment
Share on other sites

very interesting organization rebull

 

Could you explain this further or show me example of this sentenced that you said below,

i tend to send ALL requests (via a rewrite) through a single file - index.php, rather than having stuff like account.php and login.php, etc.

 

thanks in advance again.  ;)

Link to comment
Share on other sites

many people organise files like this:

index.php

myaccount.php

news/index.php

news/article.php

 

etc

but i tend to prefer things like this through a single index.php file. i might use mod_rewrite to make sure that EVERYTHING after my domain is rewritten, so a request like mydomain.com/news/article/10.htm wont look for page 10.htm inside the articles folder inside the news folder, but will instead rewrite it as:

 

index.php?request=/news/article/10

 

behind the scenes, and run my application from their by including the necessary classes, etc based on $_GET['request']. there's many reasons i choose to work this way, but the main one is just one point of entry in to my app, and one point of exit. makes things much easier when you want to either make significant changes to the site or you're creating a whole new site from the same codebase.

Link to comment
Share on other sites

index.php?request=/news/article/10

 

a rewrite for something like that would look like this

 

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?request=$1 [L]

 

The RewriteCond %{REQUEST_FILENAME} !-f and RewriteCond %{REQUEST_FILENAME} !-d will not rewrite valid files or directories to index.php.

 

Another alternative to using $_GET would be to do something like Daniel0 describes in his Clean URLs without mod_rewrite tutorial. You would still have to use mod_rewrite to remove the index.php. The same rewrite can be used with the following edit 

RewriteRule ^(.*)$ /index.php/$1 [L]

 

Also a good general way of doing things is say you have a request like redbulls example /news/articles/10. News would be a controller and articles would be a function within that controller. Whereas 10 is the id of the article.

 

For more on mvc style check out the Wiki article on the Model-view-controller architectural pattern.

Link to comment
Share on other sites

index.php?request=/news/article/10

 

I am not a big fan of this method for getting information from files, never have been.  In the places i have seen this used it has been very hard for me to find the file i was am looking for because there is a chain of this file calling this file which calls anopther file which calls another file which calss another file(i think you get the point).  Some people like so this is really just a personal standpoint, and has nothing to do with file directory stucture(or should not).

 

i like to follow a common directory style.  fro instance my web API i am developing has a directory of this:

 

api(the api root directory which is in the root directory of site)

-js

--css

--images

--jquery.js

--cluetip.js

--etc...

-php

--classes

---base

----base.class.php

----database.class.php

----etc...

---custom

----site.class.php

----etc..

---html

----html_element.class.php

----table.class.php

----etc..

--functions

---date_time_functions.php

---etc..

config.php

Link to comment
Share on other sites

Well I have a basic structure: Then again I ain't no expert!!

 

root:

 

layout --> All images to make the template of my site

 

element --> Header / Footer / Login - Registrations scripts that I then include () in my pages

 

info --> About Us / contact / terms / sitemap

 

news -->

 

more -->

 

News and more act us extra directores that I use to simply put html in and then their corresponding css. I usually have the script in the needed area. If I need to use a script in more than one directory I move it to element where I then include it.

 

IN the future I might move into other ways. I don't think my is effiecient... lol!  :P

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.