Practical Web 2.0 Applications with PHP

Quentin Zervaas

Mentioned 3

Provides information on using PHP to develop applications for Web 2.0.

More on Amazon.com

Mentioned in questions and answers.

I'm working through Practical Web 2.0 Appications currently and have hit a bit of a roadblock. I'm trying to get PHP, MySQL, Apache, Smarty and the Zend Framework all working correctly so I can begin to build the application. I have gotten the bootstrap file for Zend working, shown here:

<?php
    require_once('Zend/Loader.php');
    Zend_Loader::registerAutoload();

    // load the application configuration
    $config = new Zend_Config_Ini('../settings.ini', 'development');
    Zend_Registry::set('config', $config);


    // create the application logger
    $logger = new Zend_Log(new Zend_Log_Writer_Stream($config->logging->file));
    Zend_Registry::set('logger', $logger);


    // connect to the database
    $params = array('host'     => $config->database->hostname,
                    'username' => $config->database->username,
                    'password' => $config->database->password,
                    'dbname'   => $config->database->database);

    $db = Zend_Db::factory($config->database->type, $params);
    Zend_Registry::set('db', $db);


    // handle the user request
    $controller = Zend_Controller_Front::getInstance();
    $controller->setControllerDirectory($config->paths->base .
                                        '/include/Controllers');

    // setup the view renderer
    $vr = new Zend_Controller_Action_Helper_ViewRenderer();
    $vr->setView(new Templater());
    $vr->setViewSuffix('tpl');
    Zend_Controller_Action_HelperBroker::addHelper($vr);

    $controller->dispatch();
?>

This calls the IndexController. The error comes with the use of this Templater.php to implement Smarty with Zend:

<?php
    class Templater extends Zend_View_Abstract
    {
        protected $_path;
        protected $_engine;

        public function __construct()
        {
            $config = Zend_Registry::get('config');

            require_once('Smarty/Smarty.class.php');

            $this->_engine = new Smarty();
            $this->_engine->template_dir = $config->paths->templates;
            $this->_engine->compile_dir = sprintf('%s/tmp/templates_c',
                                                  $config->paths->data);

            $this->_engine->plugins_dir = array($config->paths->base .
                                                '/include/Templater/plugins',
                                                'plugins');
        }

        public function getEngine()
        {
            return $this->_engine;
        }

        public function __set($key, $val)
        {
            $this->_engine->assign($key, $val);
        }

        public function __get($key)
        {
            return $this->_engine->get_template_vars($key);
        }

        public function __isset($key)
        {
            return $this->_engine->get_template_vars($key) !== null;
        }

        public function __unset($key)
        {
            $this->_engine->clear_assign($key);
        }

        public function assign($spec, $value = null)
        {
            if (is_array($spec)) {
                $this->_engine->assign($spec);
                return;
            }

            $this->_engine->assign($spec, $value);
        }

        public function clearVars()
        {
            $this->_engine->clear_all_assign();
        }

        public function render($name)
        {
            return $this->_engine->fetch(strtolower($name));
        }

        public function _run()
        { }
    }
?>

The error I am getting when I load the page is this:

Fatal error: Call to a member function fetch() on a non-object in /var/www/phpweb20/include/Templater.php on line 60

I understand it doesn't see $name as an object, but I don't know how to go about fixing this. Isn't the controller supposed to refer to the index.tpl? I haven't been able to discover what the $name variable represents and how to fix this to get the foundation working.

Any help you have is much appreciated!

The problem isn't with the $name variable but rather with the $_engine variable. It's currently empty. You need to verify that the path specification to Smarty.class.php is correct.

You might try this to begin your debugging:

$this->_engine = new Smarty();
print_r($this->_engine);

If it turns out that $_engine is correct at that stage then verify that it is still correctly populated within the render() function.

I would really like to get a list of hands-on approach books, where you mostly learn by examples and not only theory. I'm looking for CSS/PHP/SQL maybe and any other hands-on books.

For example, I wasn't very comfortable reading HTML Dog (although it's a good book) and would really much prefer books like CSS Anthology 101 Tricks, where you learn mostly by examples / questions etc.

I tried to find those kind of books but couldn't really and I think I'll benefit more from them then a pure theory book where you read 300 pages without writing code.

For php, I invite you to read the indispensable PHP and MySQL Web Development book

alt text

For the CSS/HTML, I recommend the Meyer's references (book, articles):

CSS: The Definitive Guide, Third Edition

alt text

Meyer's website

The Head First series by O'Reilly is a beginner series that teach you entirely through applications / examples. EDIT: the Head First series is definitely not like any technical book you have ever read before. First and foremost it is designed to stimulate your mind and get you thinking about what you're learning creatively (e.g. cross-word puzzles, brain teasers) which, combined with the examples/code really help you understand the concepts.

The Cookbook series by O'Reilly is another good (more intermediate) series that teaches you through different examples. The structure of the book CSS Cookbook, for example, is just "recipes" (tutorials) on how to solve certain problems or accomplish specific tasks.

I gained a lot of contemporary web development methodology from Practical Web 2.0 Applications with PHP. It was fun reading through as I was enjoying creating all cool stuff by my own. As this book title Web 2.0 you'll find a good examples on making things usable with HTML/CSS.

Where can I learn more about building social networks with PHP?

Preferably, I would like a book on the subject.

it's not PHP specific, but Building Social Web Applications from O'Reilly publications is a good start.

And while it's not completely social-network oriented, Practical Web 2.0 Applications in PHP is also pretty good.