简体   繁体   中英

should i use lots of php/html files or just one?

I'm making a website for a friend and I have a general question about how I should make the files.

I've been making a seperate html file for each page (eg index.html, forgotpassword.html, activate.html etc.) and also a seperate PHP file for handling each form (eg processlogin.php, activate.php, forgotpassword.php etc)

I'm wondering if it is better practice to have one big php (or as few as possible) file to handle all the login/activate/forgot password etc forms. Should I also have one PHP file that echo's all the html files? Is there a way that is overwhelmingly better than another?

The file separation must respond to these criteria :

  1. Will it be easy to maintain and expend
  2. Will I repeat my code often

That said, I suggest you to split at least like that :

  1. General template (header.php, footer.php)
  2. Page for typical content, informative page (including header.php + footer.php)
  3. Page for each complex task example : contact form, login, forgot password, etc

One general practice is to include the login form inside the header.

Is there a way that is overwhelmingly better than another?

There are many ways to handle this kind of "problem".

First, it's not better to have one big php file to handle the forms. Your approach to handle your HTML and PHP seperately is not too wrong altough you need to structure your application in a better way.

I would suggest you a front controller. That means: you have one file (index.php fe) which takes all your requests and depending to the parameters send to it, it will handle the actions by including the appropriate files.

Also work with templates. Split your HTML up into several parts (header, content, footer) so you don't have to include all of your HTML into every file. Instead of doing this you load your header and your footer (fe) and depending on the parameters send to your front controller, you load the appropriate template file for your content.

Also take a look on the MVC approach for an even better seperation of concerns.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM