PHPBuilder - PHP Framework Review: DooPHP Page 2



RSS Twitter
Articles Tools

PHP Framework Review: DooPHP - Page 2

by: W. Jason Gilmore
|
March 29, 2011

Creating a Layout in the DooPHP Framework

Logically your site is going to integrate a global layout and theme. While DooPHP supports the ability to wrap a template around the various views, admittedly I'm really not a fan of the chosen approach because it's really no better than co-opting PHP's native require_once statement for template construction. Nonetheless it's possible, done using a custom include syntax, as demonstrated here:
<!-- include '../site/header' -->

<h2>About Us</h2>

{{message}}

<!-- include '../site/footer' -->

Using Models in the DooPHP Framework

So far we've covered creating controllers, actions, and views, but haven't yet discussed the important matter of interacting with a database via models. DooPHP offers an incredibly easy and intuitive approach to this typically complex feature, and in this section I'll show you how to use it by way of creating a model used for managing website bookmarks. Begin by creating a MySQL database named bookmarks and within it a table also named bookmarks (incidentally the database and table naming choices for this example is completely coincidental; you can name your database and tables anything you please). The bookmarks schema looks like this:
create table bookmarks (
 id integer unsigned not null auto_increment primary key,
 url varchar(255) not null,
 title varchar(255) not null,
 description mediumtext not null
);
Next create a model file named Bookmark.php and place it within app/protected/model/. Like controllers, DooPHP models are also classes. The Bookmark.php class should look like this:
<!--p
class Bookmark {

  public $_table = 'bookmarks';

  public $_primarykey = 'id';

  public $id;
  public $url;
  public $title;
  public $description;

  public $_fields = array('id', 'title', 'url', 'description');

}
</p-->
Next, open up the db.conf.php file which resides in app/protected/config and add the following line, replacing the uppercase placeholders with the appropriate values:
$dbconfig['dev'] = array('localhost', 'DBNAME', 'USERNAME', 'PSWD', 'mysql', true);
Finally, uncomment the following two lines in your app/index.php file:
include './protected/config/db.conf.php';
...
Doo::db()->setDb($dbconfig, $config['APP_MODE']);
With DooPHP's model management capabilities configured, you can begin adding records to the bookmarks table from within your controller actions like this:
Doo::loadModel('Bookmark');

$bookmark = new Bookmark;
$bookmark->url = 'www.wjgilmore.com';
$bookmark->title = 'WJ Gilmore, LLC';
$bookmark->description = "Books, tutorials, videos, Web training, and more!";

$result = $this->db()->insert($bookmark);

Conclusion

Frequent readers of my writings here and on Developer.com are likely well-aware of my advocacy for the Zend Framework. More generally though I'm an advocate of framework-driven development in general, urging even beginning PHP users to explore the various available solutions and adopt the framework which best resonates with them as soon as possible.
In fact, it's entirely practical to adopt multiple frameworks, selectively adopting them according to the particular project requirements. For instance, if you were charged with building a large enterprise application, then the Zend Framework or Symfony might be the best candidate.
This short DooPHP tutorial barely scratched the surface in terms of what's possible with this powerful framework, yet nonetheless managed to guide you through the creation of a simple yet dynamic, database-integrated website. Be sure to check out the DooPHP website for more examples, demos, and documentation!

About the Author

Jason Gilmore is founder of the publishing, training, and consulting firm WJGilmore.com. He is the author of several popular books, including "Easy PHP Websites with the Zend Framework", "Easy PayPal with PHP", and "Beginning PHP and MySQL, Fourth Edition". Follow him on Twitter at @wjgilmore.

« Previous Page
1
|
2

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
W. Jason Gilmore

Comment:



Comment:

(Maximum characters: 1200). You have characters left.