Page 1 of 1

PHP Application "Design"

Posted: Wed Jun 18, 2008 9:20 am
by movedx
I just wanted some advise on the manner in which I organise my applications...

The sites I generally develop are small and for self employed individuals. At present I write a class for each page and simply implement an instance of that class in the actual page's .php file. I use PDO for database access (directly in the page class) and patTemplate for the templating (directly in the .php page file). I do need some assistance with form generation, though.

I don't fancy using something like Code Igniter or Kohana, simply because I wont learn much from doing so.

Can anyone suggest a form generation system to use and perhaps give me some feedback on how I organise my applications at present?

Cheers.

Re: PHP Application "Design"

Posted: Wed Jun 18, 2008 6:43 pm
by Bruno De Barros
I feel the same way. Most of my websites don't require really big frameworks (My home brewed framework suits my websites just fine), and most of PHP's frameworks just don't suit my website development style.

I am working on a set of PHP Classes that are completely independent from each other, and one of them is a complete form generator that is lightweight and powerful. I have a set of forms made with that class, and then you can just fill them with your values (if you want to have predefined values coming from a database or from the input data. They are then parsed so they don't break the forms, and that's it. Easy to use. :) And they can be cached (so far, I'm using singular files for each form, with the form object serialized, and the form generator class has a function that loads a cached form and returns the form instance). But I'd like to hear of a better, already existent solution, as my form generator still is a work in progress.

Re: PHP Application "Design"

Posted: Wed Jun 18, 2008 6:54 pm
by Luke
:arrow: moved to theory and design :)

Re: PHP Application "Design"

Posted: Wed Jun 18, 2008 7:19 pm
by Christopher
Bruno De Barros wrote: and most of PHP's frameworks just don't suit my website development style.

I am working on a set of PHP Classes that are completely independent from each other, and one of them is a complete form generator ...
Can you give us a rough example of how your forms solution works. The example might also show you web development style -- maybe a simple form that accepts a few field types, validates the submitted values and either redisplays the form with error messages or if no errors then redirects.

Re: PHP Application "Design"

Posted: Wed Jun 18, 2008 8:24 pm
by Bruno De Barros
When I spoke about my development style, I meant the layouts and different features on the frameworks. There are just some things that I don't like in some of them (most of my issues with them are the same as the issue I had with SimpleTest: pure laziness - not wanting to learn a whole new framework when I can make my own :P). I just need to take some time off to learn other people's code, which is something I've always hated doing. CodeIgniter looks good, and extremely well documented. Zend Framework looked like an amazing PHP framework, but the "Under Construction" quick start page turned me off.

The interface to the form generator (and data validator), it is a work in progress, still an idea in my head, but here is a sample of what it could come to look like:

Code: Select all

 
<?php
# Create the form
$form = new Form($name, $method, $action, $attributes); # Create the form.
$form->display_template($template); # This tells the form class how to build the HTML for the form (i.e. how to wrap it, and each of its fields). If you want to use tables, or divs, or labels, etc, the template can do it.
$form->error_template($template); #This tells the form class how to build the HTML for the form (i.e. how to wrap it, and each of its fields) WHEN THE FORM HAS ERRORS (so where to show the form errors, etc). If you want to use tables, or divs, or labels, etc, the template can do it.
$form->username = new TextInput($name, $attributes, $rules); # Rules are the validation rules. This is probably going to be an array, or an instance of a Rules class. Depends. I think it would be better to have $rules->isRequired()->size(1,255)->type('integer');, that kind of stuff, than a raw array. Besides, if it's a class, you can easily see the rules that are available for you to set. It might also have custom rules, for more flexibility, with callback functions. The attributes might (or might not) be another class, just to process them to work properly on HTML automatically and to be able to communicate with the inputs properly. On the other hand, it can just be a PHP array, and the proper HTML quoting done when generating the form's HTML.
$form->password = new PasswordInput($name, $attributes, $rules);
$form->remember_me = new CheckBoxInput($name, $value, $attributes); # Checkboxes always have a predefined value. They also don't have rules because they either are empty (not checked) or exactly the same as $value (checked).
$form->submit = new SubmitInput($name, $value, $attributes); # Submit buttons always have a predefined value. They also don't have rules because they either are empty (not clicked on) or exactly the same as $value (clicked on).
 
if ($form->submitted()) { # The submitted() method assumes the input data source to be $_POST or $_GET, according to the $method variable set when building the form.
    if ($form->valid($_POST)) { # The valid() method doesn't assume that the input data comes from $_POST or $_GET, because that would exclude validating with other data sources, such as data coming from the database.
        # Ok, it's valid.
    } else {
        # This didn't pass the validation rules.
        $form->data($_POST); # Tell the form where the form data is (to fill the form).
        $form->display();
    }
} else {
    $form->display();
}
?>
 
Looks kind of confusing, but I added all the comments to explain everything (to you, and myself). I usually do this in a planning stage, to figure out different (more developer friendly) ways to make the thing work. If you have any better ideas...

Without the comments:

Code: Select all

 
<?php
$form = new Form($name, $method, $action, $attributes);
$form->display_template($template);
$form->error_template($template);
$form->username = new TextInput($name, $attributes, $rules);
$form->password = new PasswordInput($name, $attributes, $rules);
$form->remember_me = new CheckBoxInput($name, $value, $attributes);
$form->submit = new SubmitInput($name, $value, $attributes);
if ($form->submitted()) {
    if ($form->valid($_POST)) {
        # Ok, it's valid.
    } else {
        $form->data($_POST);
        $form->display();
    }
} else {
    $form->display();
}
?>
 

Re: PHP Application "Design"

Posted: Wed Jun 18, 2008 9:26 pm
by Christopher
No, not confusing at all. In fact it is almost exactly the same structure and naming that use in Skeleton. I think this is pretty standard. I am always interested to see what others are doing as a reality check for what we are doing. Your system looks very good. Do you support multiple template types or just your standard one? And is $template a filename, template string or object?

Re: PHP Application "Design"

Posted: Fri Jun 20, 2008 3:08 am
by movedx
Hello,

Here is an example "shell" of a page class, the style in which I design my sites:

Code: Select all

<?php
 
class About_Page
{
 
private $db;
private $tmpl;
 
public function retrieveBio() {}
public function retrieveAboutCar() {}
 
}
 
class About_PageMgr extends About_Page {
 
public function setBio($val) {}
public function setAboutCar($val) {}
 
}
 
?>
 
I then, obviously, create an instance of this class in about.php and start setting template engine variables using the methods in the class from within about.php.

About_PageMgr is implemented in the administration panel for modifying the page. About.php would have readonly access, effectively.

Suggestions? Thoughts? Ideas?

Re: PHP Application "Design"

Posted: Tue Jun 24, 2008 4:40 pm
by Bruno De Barros
Probably $template could be another object, that can receive input from files, strings, and whatnot. I know it's a hell lot of objects, all around, but it is much more easily extensible and more flexible (look at the template example, you are not limited to a text string or a filename. You can use anything you want (if the template object supports it). For example, you can create a subclass of the SubmitInput, that has some different things, and then, when adding a new field to the form, just use new MyOwnSubmitInput.

I wonder what your Skeleton Framework is like. I keep reading posts talking about it, but I dunno where to download snapshots or just learn more about it.

Re: PHP Application "Design"

Posted: Tue Jun 24, 2008 5:08 pm
by Christopher
Bruno De Barros wrote:I wonder what your Skeleton Framework is like. I keep reading posts talking about it, but I dunno where to download snapshots or just learn more about it.
Skeleton Framework is here: http://code.google.com/p/skeleton/

There is a download that includes a bunch of very rough examples. The code is being used on real projects, but it is still under development and has a long way to go. I guess it might be described as lighter weight than most popular frameworks because it is a little more loosely coupled -- like a lighter weight ZF. We are still struggling with all the design issues of frameworks. And we want to support multiple implementation styles by building the framework using a layered approach. That just makes the job harder.

If you have questions feel free to ask me...

Re: PHP Application "Design"

Posted: Wed Jun 25, 2008 1:46 pm
by Bruno De Barros
Looks good. I'll have a better look at it when I get back home (i'm on holidays right now).

Re: PHP Application "Design"

Posted: Wed Jun 25, 2008 3:49 pm
by Christopher
Bruno De Barros wrote:Looks good. I'll have a better look at it when I get back home (i'm on holidays right now).
I will be quite honest:

- there is no documentation yet,
- the examples are for the teams internal use testing use cases,
- there is no nice website,
- the design is still in flux as we look to improve the design in many areas.

That's the bad news. :)

The good news is:

- the code is stable enough to be used on production sites (much of the base code is 3-5 years old)
- the changes tend to be adding a new way of doing something, not changing the existing interfaces
- the goal is to allow variations in implementation styles, and our layered approach makes that possible little overhead
- you can implement a much more lightweight, minimal app than with most frameworks, if you want to.
- we know we aren't brilliant or have a unique or best design, so we are very open to input from those willing to slog through the details.

PS - if you are knowledgeable/interested in Domain Model and Data Source patterns we need your help

Re: PHP Application "Design"

Posted: Wed Jun 25, 2008 3:57 pm
by alex.barylski
arborint: You should have a link to the Skeleton library/framework in your sig. I download it, view it, borrow ideas, etc...but then delete it. It would be nice to just find a post by you, and re-download it, as opposed to having to google it or search the forums. I know I'm just lazy but I think that would help in building it's user base.

Re: PHP Application "Design"

Posted: Tue Sep 16, 2008 6:52 pm
by thinsoldier
arborint wrote:
- there is no documentation yet,
- the examples are for the teams internal use testing use cases,
- there is no nice website,
- the design is still in flux as we look to improve the design in many areas.
Would you consider taking a variation of one of the real projects you've built with it and using that as a decent example?
I'm not making any promises but if I could understand it better I probably would have used it on my last 3 side projects for friends/family and allowed their code to be used as working examples.

They weren't big, fancy or complicated projects but they were complete sites and I know for some people (especially myself) looking at a complete website with graphics and content and the code to make it all happen is a lot more helpful than trying to understand a random folder-full of rough, bare-bones, tests and experiments.

Re: PHP Application "Design"

Posted: Tue Sep 16, 2008 10:18 pm
by Christopher
You tricked me and replied to a different thread! ;) Matthijs and I are working on a Skeleton Framework version of the standard blog example right now (and writing/organizing the docs). I will let you know when we have something for you to look at.