Layout/Decoration framework for PHP? - php

A little of my background... I'm fairly new to PHP as I primarily develop in Java. PHP interests me, thus I decided to spend my free time learning it by trying to make one of my relatively static websites more dynamic using PHP.
Right now, I'm have a problem in decorating all the web pages... not really a problem, but it bugs me. I know I can include the header and footer files in every page, but I'm wondering if PHP has some sort of web-based layout/decoration framework?
In Java, I use Sitemesh to decorate my web pages without the need for me to explicitly include the header/footer files. I'm hoping I can do the same in PHP too.
If you have abundance of experience developing web applications in PHP, can you please share some of your knowledge with me here? I'm interested to know the techniques you use to easily decorate your web pages and to change the page layout in the future.
I greatly appreciate your tips and ideas. Thanks much.

PHP doesn't neccessarily have a 'decor' framework, but more generally it has many MVC based frameworks for the language itself. Many of these include 'templating' elements that allow for such headers, footers, partial layout content, etc...
Some of these are
Codeigniter (My personal Choice)
Kohana
Zend
Symphony
Generally, people will recommend what they use, and most of the time you can't tell if they use it because they like it, or like it because they use it. I recommend consulting the specific frameworks documentation to get a feel for whether or not it fits your needs.
EDIT :
After a second read of your question, I also thought you might look into more simple CMS solutions. While you could accomplish what you want to do with any of these frameworks, it might be less work to use a CMS that uses a templating engine, if all you want to do is manage content and the way it looks. The frameworks listed above are a bit more adept and building web applications (for comparison, a CMS is a web application).
Wordpress
ExpressionEngine (By the makers of Codeigniter)
Drupal

I use a main template view for HTML pages. This includes the header and footer, but the middle has a <?php echo $primaryView; ?>.
I can change the $primaryView depending on what page needs to be shown.
The MVC pattern is very useful with web development.

Related

PHP is a framework the best choice for basic CMS websites

Having been dong research on codeigniter,kohana and fuel php, they seem excellent for the large scale project I want to create with various sections of code. The system I want to build is to be a core set of code, which can produce a full ecommerce web application with plenty of modules, or can produce a simply few page site, with a news or gallery or whatever.
I have started thinking that I may be looking at the wrong thing for producing basic sites. From my very brief research, it seems (atleast on codeigniter) I need to use the 404 overwrite to point to my pages controller in order to handle basic content pages (from the base url /).
Should I consider a seperate code set for the basic sites, and use these frameworks only for large web apps, or is this the normal way frameworks would handle serving basic content pages.
I personally have a core framework (MVC/CodeIgniter) which I use for even the simplest static sites - Because someone will always say "Oh and can we have a shop" or a contact us form or ...
The overhead of the frameworks themselves is negligible if they're not doing any work - so why not put them in?
Another bonus is that you now know exactly what environment your modules will be in - you don't need to worry about maintaining one versions for sites with the framework and one for those without (One other benefit of using a framework is that it makes if far easier to maintain code in general - especially in multiple-developer environments.)
Th templating engines provided by most frameworks (or easy extensions to them) are often useful even for static sites (custom controls like a news ticker or custom markup/js validation for certain controls)
In short, unless you have extremely limited server resources (mobile device?) use a framework
Edit:
I'd add a note of caution - PHP is a very flexible, powerful language and when used well can create some truly fantastic sites. When used poorly, however, it is easy to make insecure websites.
From your post, you seem relatively new to PHP/Frameworks. Can I suggest you make sure that the framework you pick enforces good behavior (as much as possible)
CodeIgniter removes unsafe $_POST and $_GET variables, replacing them with safe equivalents. It also provides a parameterised mechanism for querying the database which helps avoid SQL inection attacks. These are important things to consider when comparing frameworks.
I personally found CodeIgniter to be a good balance of maintainability, security, extensibility and functionality.
I would consider using a CMS for the basic sites, something like Drupal, they do a lot out of the box and should satisfy your requirements for a basic site.
As for the eCommerce part, from experience its best use something that is separate from the CMS. Largely because projects or frameworks that are designed for that sort of thing are generally an application.
CMSes do what they do they do best, but that all they really do :)
Also have a look at Magento for your eCommerce needs.
Hope this helps.
I think your question is about what to use for "basic websites".
Should I consider a separate code set for the basic sites, and use
these frameworks only for large web apps, or is this the normal way
frameworks would handle serving basic content pages
If by basic sites you mean websites that have little to no dynamically drawn content, I would suggest a CMS such as Wordpress. You mention that you are using PHP and Wordpress is a very popular option. If you are creating a blog and a very static pages, this will be a great option. It can even be modified to do more 'dynamic' things that can be considered "CMS Territory". Once you get the basics of Wordpress down, and if you still feel that you need to modify it a lot, then you should look in to a framework that will allow you to have total control such as Codeigniter, Cakephp, etc.
Hope this helps.
Ben this is an interesting topic and no matter which direction you eventually go it would be nice to know your choice of combination. Depending on your requirements.
For something with medium range functionality OSCOMMERCE might be worth a check on. Also see Zencart, Virtumart and Megento.

Can we create a full dynamic web-site using just CMS like Joomla,Drupal,etc..?or should know some programming like php,jsp beforehand?

Should one need to learn php(or jsp) to create dynamic web-sites or just to use CMS ?.I am new to web-development...
If you just use the CMS without understanding how it works then you are limited by what the CMS will do out of the box or with plugins. You will have little scope for customisation, and not be able to fix things if anything breaks.
It really depends what you want to achieve. If a CMS does everything you need it to do, then you can probably get away with just using it out of the box.
(And now I will resist the urge to start a language war and just say that there are other languages beyond PHP and Java).
No, you can use cms without knowledge about php, css or even html.
Depends on what you want to do in what scope. If you're looking to set up a web site for your band (for example), there's no need to learn PHP. There are plenty of CMSes that can be set up without deep PHP knowledge.
If you want to start a web development career, however, learning a server-side language is pretty mandatory.
CMS Made Simple does not require much technical knowledge. Some is benefecient. But in the old days I used to work with complete lack of php/js.

Cms to plugin to a multiple controller php site

I have a php site setup with multiple controllers for the different sections of the site.
e.g index.php, gallery.php, faq.php etc.
Currently I have a base template which I call from each controller, which in turn includes the relevant content for the page. I also use tinymce to edit this content file.
So it's really a very simple cms.
Edit: To clarify, I have several new features I would like for my cms. e.g Specific section editing, menu editing and a few others.
But I thought instead of continuing development on this and reinventing the wheel, I'd try to find a cms which can plug in to my multi-controller system, instead of forcing me to develop my app in it's framework. I guess I want it to manage just the content, not any other backend stuff of my site.
Hope this is understandable.
I'm not sure if I'm understanding your question correctly. You have a site with a basic CMS using a template system and multiple controllers. You don't want a framework to port your site to, but you instead want a CMS that will "plug in" to your current site? What exactly do you want plugged in to your site? (i.e. What features/functionality are you looking for?)
Edit:
I still think you should consider an MVC framework like CakePHP if your goal is to build a more robust CMS without re-inventing the wheel. Building on a well-designed framework would also make extending your site's functionality in the future much easier (as well as make it easier to maintain a complex web application).
The only other option I can think of is to simply replace your existing CMS with a more robust one, like Drupal/Joomla/Wordpress, which you could then extend with modules. But that would probably give you less flexibility, not more.
Le me see if I have this right. You want a CMS. But it has to be one that will conform to your arbitrary way of doing things that you can drop in and have work. You want a CMS that isn't frameworkish at all. A CMS that will only do the frontend, and not the backend.
You don't want a CMS at all. A CMS, by its very nature, does backend work. That's the point of a CMS. What you want is Dreamweaver-esque magic template handling.
Or maybe you should adopt a real CMS or framework, and work from there. It's not really reasonable to expect this sort of magic psychic power from a CMS.
i also recommend Drupal. its very well organized and is easily adjusted to almost any style of website. you can use it as a pure cms or add plugins w additional features as needed .. the core package is fairly small, very easy to set up and play with, and very well supported .. worth looking at
If you wrote a very basic CMS for your own use it seems to me that you should have no problem adopting an existing out of the box solution like Drupal, Joomla, etc. These are full featured CMSs that are ready to go and with some graphics work and a little hacking on your end can look like anything you want.
Your other option is using an existing framework like others have suggested such as CakePHP, CodeIgniter, etc. These are all frameworks which basically give you all the tools you need in order to build a more robust custom CMS than what you have now.
It seems to me that you know at least a little bit about what you're doing. I would suggest you check out the different frameworks that are available and run with that.
CodeIgniter
CakePHP

PHP best design practices

Ok, have a bunch of questions that I have been thinking about the past few days. Currently I have a site that is just a bunch of PHP files with MySQL statements mixed in with PHP, HTML and CSS, basically a huge mess. I have been tasked with cleaning up the site and have made for myself, the following requirements:
The site needs to be efficient and well laid out (the source code), I would like to be able to write as little code as possible.
There has to be good separation between structure, presentation and logic.
For whatever reason, I can't use a framework and need to keep the code maintainable and "simple" as there will be future developers working with it.
There needs to be an admin section for at least a few pages.
Saying that, this is what I know about the site as it is now:
Consists of 10-12 pages, a few are completely static, most are dynamically driven via a database and there is a huge form for users to fill out (20-30 fields) that need to be validated and checked.
The hierarchy of the site is basically 5-6 main pages and then sub-pages within those.
So, knowing those things I wanted to know if anyone had any tips/suggestions as to how to go about doing this with the least amount of headaches.
Would an OO approach be best in this situation?
Since there are many static pages and the dynamic pages just need the content filled in would it be best to use some kind of basic template?
EDIT: Thanks for the answers, when I said no frameworks I basically meant anything that would require new syntax other than PHP, as whoever gets hired to work on this site after me will probably only know PHP.
Here's an article about how to organize your PHP project, from Rasmus Lerdorf, the architect who created the language:
http://toys.lerdorf.com/archives/38-The-no-framework-PHP-MVC-framework.html
Despite the popularity of OO frameworks for PHP, Rasmus advocates a less OO approach. He knows more than anyone about PHP intended usage, and how to take advantage of its architecture for high-performance websites.
edit: In response to the comment by #theman, I'll concede the article isn't a fine work of writing, but I think the content is important. Using PHP as it was intended to be used is better than struggling against its weaknesses to make it fit an OO mold.
I highly recommend the Smarty templating engine for all PHP projects. It gives you an easy way to separate the logic from the presentation.
Have a look at this SO question and the answer. It's a pretty good, simple MVC design with some tips on how it can be improved. If you are concerned about maintenance, then at the very least you need to seperate presentation from logic (you need a view and controller). Smarty forces that, but it is a type of framework and you'll have additional syntax to learn.
Before you jump on Rasmus' "no framework php mvc framework" bandwagon, read some of the critical comments. Any web application structure is a framework, and Rasmus' approach isn't the best I've seen.

Simple web page layout and templating in PHP

In the past I've written sites in ASP.NET just to get nice reusable templating via master pages (layouts) and user controls (partials). I'm talking about sites that have no more complicated code in them than simple variable substitution in templates - I'm just using it to keep my HTML organized. It's great to be able to define the basic layout of your page in one file, and then just fill in the holes for each individual page, re-using some common bits as user controls.
However, I'd rather not continue using Windows hosting, so I'd like to write static pages in PHP, which runs everywhere. In the past I've written myself a poor-man's user control for PHP that just unpacks an array and includes a template, but that's clunky and still requires me to put PHP includes all over my pages. I feel like Smarty or one of the other big templating languages for PHP is overkill for just organizing some "static" pages.
I've considered just doing the pages in Ruby on Rails because I really like HAML/SASS and the layout/partial model, but having a whole rails app for static content seems silly. But I haven't found anything that seems just right in PHP. Can any PHP gurus recommend anything? I want something more sophisticated than "include" but I don't really need much. That said, something that could give me layouts/partials AND HAML/SASS would be heaven. I've looked at phammable but it doesn't look like it solves the layout/partial problem.
Again, I'm not looking for something as complex as Smarty, and I don't want a full CMS. I just want something that will let me properly modularize the HTML of my site. We're talking one step beyond static HTML here.
Zend_View supports layouts, partials and placeholders. Also checkout a new templating language called Dwoo which is similar to Smarty but also takes some ideas from Django templating. And finally, Calypso which is a Django template language clone for PHP
If your looking for something simple I would recommended PHP Template Inheritance. Then you can continue to code in straight php.
I just want something that will let me
properly modularize the HTML of my
site.
Look at http://code.google.com/p/hoduli/
With that simple tool you'll be able to write like this:
<h:define name="header">
some header contents
</h:define>
...
<h:header />
<h:include file="middle_part.php" />
<h:footer>
some ...
</h:footer>
I would suggest to you to look at Drupal and its Block templating model. I think its just what you need.
Stacey is about what I was looking for in terms of a simple PHP templating library for static sites.
On the Ruby side, there's StaticMatic, which doesn't even require Ruby support on the server since it generates the whole static site. Very nice (especially since it uses HAML) but you can't have any dynamic elements at all.
From what i understand, you don't want a full fledged cms and no overkill templating engine like Smarty. Php is a templating system and if you want to properly modularize it, that's up to you.
But i understand, those php tags between your html doesn't look pretty. (Although using shorttags minimizes this...)
You can always use PEAR Templating system, it's available by default on most php installations.
I would also recommend the CodeIgniter MCV framework because it's really lightweight. You choose which libraries get autoloaded.
About six months ago, I started using the Zend Framework. I got used to working with layouts and partials, and never wanted to go back creating sites without it. But for small sites, ZF is just too bulky. It's overkill to use ZF for a site that could be done in HTML.
Check out EuropaPHP. It's a super lightweight, PHP based layout framework. It's brand new as a framework, but so far, I like it a lot! It allows me to use layouts and doesn't require a huge library and strict rules to achieve the same results.
Maybe you should take a look at some content management systems. Drupal is famous in PHP.
Drupal really is a simple and extensible CMS. There is a learning curve, but it's strengths are that is starts small out of the box and can grow with your needs and the community is pretty tight about the quality of the code that's written (though I personally find that some of the modules get a little two function happy).
They have a simple stripped down version of the Zen theme, that lets you create the HTML you want to use. Time spent learning this CMS is time well spent, especially if PHP is already your native language.

Categories