0

I'm conceptually designing a website / application and trying to come up with the correct separation of concerns.

The basic technologies I'm settling on are: MySQL, PHP (Laravel), HTML/JS (jQuery). It's been a few years since I've written PHP, and since then I've gotten very used to MVC and MVVM approaches.

I would like to stay away from using PHP as a templating engine. My reason for this is that I want to keep presentation solely to JS and HTML code, with none of it mixed in with server-side code. To achieve this, my thinking is to stick with using PHP to create services, then call those services from JS / AJAX on the pages.

As far as I can tell, this approach would allow me to keep PHP to handling data and the client will handle all of the logic and presentation.

My main concern is that I am having a difficult time finding any resources outlining similar approaches used in the real world. Almost every single instance I can find of PHP being used has it implemented as a templating engine, dropping HTML and data onto pages when they're being sent from the server.

Are there any obvious flaws in my approach that I've missed?

I'm entirely open to changing my thinking on this one - no code has been written yet so the architecture is mine to play with :)

Thanks!

2 Answers 2

1

The reason you are probably not finding those examples is because that would mean implementing a complete templating system in PHP, which is far beyond the scope of a lot of tutorials.

A good templating engine does not mingle PHP into the view, but there will always be binding logic in your views. There are plenty of templating frameworks out there and there are even frameworks that go as far as providing MVC approaches so you might have more luck looking into that.

On my latest project I've used Symfony so that's the first one that comes to mind. The standard template engine for Symfony is Twig, which is also available as a standalone templating framework. (I'm not affiliated with Symfony or Twig in any way, it's just the last framework I've used for PHP-based MVC development)

4
  • Hmm, fair point about the tutorials. I think to focus the scope a bit - I'm thinking about avoiding a PHP templating engine altogether, not just looking for a framework to do the job. EDIT: Symfony looks pretty cool though!
    – aaron-bond
    Jul 30, 2015 at 9:42
  • You have to mix in server-side code at some point. Even if you render everything using AJAX calls and javascript, you are at that point again mixing in server-side code when you call an AJAX method... If you are used to MVC, what did you use for presentation in your other projects that was so radically different from templating frameworks in PHP?
    – JDT
    Jul 30, 2015 at 9:45
  • I've used PHP / templating before. At the minute I'm developing on Silverlight which calls services to populate client side data. All the data and presentation is handled by XAML/C# and ViewModels. Not comparing apples to apples I know, but trying to mimic the approach. If it helps any, I'd figure this project to only have a handful of page TYPES, but lots of different data to populate them.
    – aaron-bond
    Jul 30, 2015 at 9:49
  • 1
    Sounds like you want to use php to implement a REST like interface. Quite common. Here is a Symfony bundle that can get you started: symfony.com/doc/current/bundles/FOSRestBundle/index.html. I actually think the bundle is over engineered but it's not bad. On the client side, you might want to take a look at ReactJS instead of jquery. It's what all the cool kids are using. And for good reasons.
    – Cerad
    Jul 30, 2015 at 17:19
1

Your approach is far different from that of most PHP applications does, obviously a template engine binds some logic in views, most programmers prefer writing this logic in PHP itself. Limiting PHP to create services/ API s to only manipulate Data is a good idea, indeed. You may consider http://backbonejs.org/. I believe you could use it to write all presentation logic, that further connects with the services you are gonna develop in PHP. You would enjoy going this way. It helps you clearly identifying Business logic with Presentation logic and makes the software more readable and maintainable for future up-gradations.

1
  • Thanks for the information. I'll take a look at Backbone and ReactJS (as suggested above) - at least what I'm trying isn't totally pointless :)
    – aaron-bond
    Jul 31, 2015 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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