1

I've been using PHP Frameworks for so long that I've actually forgot the "best practices" for create websites without one.

Usually I will use Symfony, or more recently I've been using Laravel. A client wants a very simple website, but with certain parts of it dynamic. Due to the nature of the site using Wordpress, or a Framework, is out of the question.

I'm a sucker for priding myself on my code, but I feel like I'm asking such a basic question that it's killing me to ask. But, what are the best practices for creating websites without a Framework?

I like to live by the K.I.S.S (Keep It Simple Stupid!) method of thinking. So, my idea was to just create the .php pages that are required, do any page processing or database interaction on that page, then have the HTML below the closing PHP tag. I would have any helpers/functions in a functions.php file. This is what I remember doing way before I was using Frameworks, and to me it seems like a very old school way of doing things.

I've not created a site without a Framework for literally 2+ years, so I've lost my way with the basics.

Any advice would be greatly appreciated.

7
  • 7
    One best practice is to not reinvent things that are already done better than we can do them ourselves, especially when it comes to security. Jul 29, 2013 at 19:31
  • @RobertHarvey This is what I love about Frameworks. Security is easier to implement and less hassle most of the times. Jul 29, 2013 at 19:31
  • 1
    What are these frameworks things you speak of? Never had 'em. Never will after nearly 10 years of doing this.
    – Rob
    Jul 29, 2013 at 20:15
  • You can build a cool website or you can build a cool framework, but few people can build both.
    – Reactgular
    Jul 29, 2013 at 20:50
  • 3
    @JamesJeffery Why don't you go for a micro framework? I think they will suit you well. Check something like Silex from the creator of Symfony2.
    – Songo
    Jul 29, 2013 at 22:01

4 Answers 4

8

You have used Symfony. That is perfect for your need. If you used Symfony, than you will know that you don't need to use all it's components. Quite the opposite is the case. You can basically use each of it's components standalone.

That being said I'd recommend you that you stick with what you know. And even if you would do it without a framework, you would end up with recreating all the stuff a framework already does. It has a reason why frameworks use database abstraction and other forms of abstraction. It makes code much more maintainable.

Going back to the very bad style of programming where you put everything in one php file, is not the solution. It has been never the solution and will never be. People believe it might be basic or quick but in the end it is only a messy code that nobody can maintain.

1
2

Frameworks are there to make your life easier. And so, like most things, your approach should be determined by the needs of the client.

TL;DR Use a framework, especially if the project is going to grow, otherwise use the simplest and quickest way forward.

If most of your sites are simple sites, with a little bit of dynamic, then I suppose there's compelling reason to use a framework. However as your project grows, having a good framework is invaluable. I've had the unfortunate privilege of working on some complicated software for an insurance firm that was built without a framework. They figured "lets build our own framework, since this project is so small". Years later it caused headaches.

Personally, I use frameworks for everything. I've moved from php to python and use a 'micro framework' called flask. Its super quick to get the basics up and running, yet adds structure should a project go. In my php days I used cakePhp and it worked really well for the simple projects I did back then.

2

If you REALLY don't want to use an out of the box framework, then design your app using some of the principles you have already learnt from your experience with frameworks.

You could follow the Symfony style, having an Entity represent your objects and acting as a basic abstraction for your DB, then a series of Controller classes which handle the routing and interactions with the view. Then finally, pick a templating language and use that within your view.

As stated, throwing all the code together in a few files is taking a step backwards.

2

While there is nothing inherently wrong with the idea of rolling a custom framework, the reality is that most 'frameworkless' PHP programmers are not really creating interesting content, they are just naive about what comprises a production ready application and repeating the same common security errors over and over again. Consider the following resources just on the security aspect of web applications:

  • The OWASP Top Ten Project (note: that is only the top ten)
  • Security issues in similar frameworks: Symfony, Zend
  • PHP Security Patches
  • Browser updates (ie, chrome, firefox, android, ios, etc...)

You should be aware of all of these things even if you use a known framework, the difference is that without a framework, you are solely responsible for implementing and maintaining these changes alongside the ones for your specific application. With a framework, you have help with most of these issues.

It's hard for me to buy the idea that a lone programmer can solve these problems any better without spending years of time, and while having a specific application to build aside from the web framework. It is not impossible, but usually those that succeed release their work as another framework, so unless that is your intention, it is not advised to down the custom framework path.

Also see this answer for more detail.

Not the answer you're looking for? Browse other questions tagged or ask your own question.