views:

93

answers:

4

Im been playing with PHP for a while, whilst not actually doing it for a crust. I have now stumbled upon jquery and have found it really easy to get what I want out of it. Combining all this together I have strung together a webapp but with no formal training I could possibly be going about it all wrong as far as structure goes.

What I have done is create a main page and insert the content of that page within a div. All the ajax for entire site is included at the loading of the main page as 'frontend.js'. The ajax posts and calls go to one backend file 'backend.php' which has all the sql, validation etc.

My file structure is something like

index.php
backend.php
frontend.js

login.php
about.php
t&c.php .........etc

I have CSS folders and JS folders as well but I guess you get the point.

What is the recommended method here? Add the required JS on each page? Have multiple backend scripts for each page also?

Or is this just a case of what ever works works?

Thx

+1  A: 

You should consider PHP frameworks such as codeigniter, zend, cake, kohana etc. they have very good structure that separates presentation and logic.

Mohamed
+1 for codeigniter.
Anwar Chandra
A: 

You can link directly to the google's hosted jquery file: http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js

<head>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;
  <script>
   // your javascript
  </script>
</head>
Anwar Chandra
A: 

Have a look at the MVC pattern and the Front Controller pattern.

From your description, I take you already seem to be using it without knowing. What you are looking for is a separation of concerns and layers that allows code reuse. Looking at the frameworks suggested elsewhere here is also a good starting point to learn how it can be done.

Please also see my answer to a related question.

Gordon
+2  A: 

A word of warning here, Frameworks do equal MVC. Frameworks come with the ability to implement MVC plus a lot of helpers and tools and perhaps one would dare to say unnecessary baggage which is sometimes (often) mistaken for MVC. It is certainly worth looking at these and other language frameworks such as Struts (Java), Rails (Ruby), Django (Python) and Seaside (smalltalk) and there are some smaller scale project around which claim to have implemented pure MVC only madeam being one. Skimming through the documentation and looking at the structures will give you an overall eerl on HowTo

Also it is worth exploring other avenues as although MVC is widely used because of it's effectiveness there are other options such as konstrukt and recess which claim to be REST based with less MVC.

PurplePilot