tags:

views:

72

answers:

5

I have php scripts that do things like register, login, upload. I would like to keep the HTML in a seperate file so that say I make a desktop client I can just use the php files to do the login register etc.

Basically to have say a front end, the HTML or desktop client and then only one backend, the php.

Is that possible?

EDIT : Would something like ASP.net be more suited to this?

A: 

You can go for Model-View-Controller design pattern. See more info about it here:

http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

Here is a good tutorial about that at phpro.org:

http://www.phpro.org/tutorials/Model-View-Controller-MVC.html

Having said that, going with MVC is rather advanced topic, you got to be good at OOP. The other thing could be to use Smarty template engine but yet you will have to learn how to use it.

Also, not to forget about top 10 MVC PHP frameworks you can use.

I would personally suggest you to go with CodeIgniter, it is easier and faster than other counterparts. Or you may want to go for Kohana, said to be extended version of CodeIgniter.

Sarfraz
+1: good explanation but smarty would be an easier alternative as you have mentioned.
A: 

Yes, this is possible with design paradigms such as MVC (Model-View-Controller). I would suggest looking into a framework such as CodeIgniter, Kohana, CakePHP, or Symfony. I've heard CodeIgniter is the easiest to learn for beginners.

Lotus Notes
+3  A: 

PHP by itself is a template and it has been long time discussed about how useless things like Smarty are http://stackoverflow.com/questions/630714/smarty-the-best-choice It is a pure matter of organization of your code rather than a "templating engine" MVC is good direction.

If you are planing to do a desktop client or something like this I think your direction is also towards the creation of an API - http://www.gen-x-design.com/archives/create-a-rest-api-with-php/

Ivo Sabev
+5  A: 

Don't just jump on a Framework bandwagon. The creator of PHP himself advocates a no-framework approach to dealing with this separation issue. See: http://toys.lerdorf.com/archives/38-The-no-framework-PHP-MVC-framework.html

The problem with general frameworks is they're unnecessary and slow. It's fine if you're specializing in a single domain such as a CMS and using drupal or something, but be wary of general frameworks. Be especially worried about the uselessness of Smarty (see: http://codeangel.org/articles/simple-php-template-engine.html)

Before you go off on me, keep in mind that I'm not anti-Kohana, ZF, etc. They still have their uses. But since people tend to just tell people to jump straight to using a framework, I thought I'd offer a different perspective.

rkulla
A: 

why not? just divide your every application into 2 parts - data preparation and data displaying.

Say, your data preparation part (also called "business logic") may look like

<?
include 'config.php';
$data=db_query("SELECT * FROM data",3);
display($data,"users_list.tpl");
?>

Note the display() function. It's the function responsible for the data output. It can render both HTML and JSON, depends on the destination. It code may look like

<?
if ($destination == 'desktop') echo json_encode($data);
else include $template;
?>

and users_list.tpl may look like

<a href="?id=0">Add item</a>
<? foreach ($data as $row): ?>
<li><a href="?id=<?=$row['id']?>"><?=$row['name']?></a>
<? endforeach ?>

Very simple. Just avoid echo $row['name']; things in your business logic code.

Col. Shrapnel