For the last couple of months I’ve been incorporating portions of applications I’m working on into my Galahad Framework Extension project. Right now it’s not at a point where I’d feel comfortable promoting it (you can check out the project on GitHub if you want), but there are portions that are pretty solid that might be useful to others right now. Two such portions are Galahad_Validate_Uri and Galahad_Filter_PrependHttp which are both very useful for processing forms with URL fields.
[Updated with follow-up video]
About a month ago I posted some ideas about PHP modeling in the Zend Framework and requested feedback. After a month of on-and-off discussions through this website and #zftalk I decided to sit down and implement things a little more.
Continue reading “More PHP Modeling (w/ video demo)” »
This just came up on #zftalk, and it appears that the information out there is either incomplete or incorrect, so I thought I’d just put out a simple solution. Here’s a simple way to calculate the difference between two Zend_Date objects (in days):
$jan1 = new Zend_Date('1.12.2009', Zend_Date::DATES);
echo "\nJanuary first: ", $jan1->toString();
$christmas = new Zend_Date('25.12.2009', Zend_Date::DATES);
echo "\nChristmas is on: ", $christmas->toString();
$diff = $christmas->sub($jan1);
echo "\nNumber of days: ", $diff / 60 / 60 / 24;
I’ve been thinking a lot about Modeling in a MVC application, particularly in the Zend Framework. Obviously each application is different, and any Model is going to be fairly unique to your application. That’s why ZF doesn’t provide a base Model class. That said, there are some design patterns that a lot of people are using nowadays, and applications could use some base functionality to facilitate those patterns.
Zend Framework’s project lead, Matthew Weier O’Phinney, has a lot of great thoughts about Modeling that I’ve been trying to stick to. In implementing those ideas, I’ve started thinking out some base classes to build my Models on top of. Obviously these classes won’t work for everyone. But they should work for a lot of “typical” web applications.
Someone was recently asking on ZFTalk about how to use a different layout for each module in your application. Since this is a problem I’ve dealt with in the past and planned on adding to the Galahad FE, I thought I’d quickly write up a tutorial on how to do it:
Put the following class in a library/Galahad/Controller/Plugin/Modularlayout.php file (you’ll probably have to create all those directories and the file).
<?php
/**
* This file is part of the Galahad Framework Extension.
*
* The Galahad Framework Extension is free software: you can redistribute
* it and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* The Galahad Framework Extension is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* @category Galahad
* @package Galahad
* @copyright Copyright (c) 2009 Chris Morrell <http://cmorrell.com>
* @license GPL <http://www.gnu.org/licenses/>
* @version 0.2
*/
/**
* Use separate layout per module
*
* @category Galahad
* @package Galahad
* @copyright Copyright (c) 2009 Chris Morrell <http://cmorrell.com>
* @license GPL <http://www.gnu.org/licenses/>
*/
class Galahad_Controller_Plugin_Modularlayout extends Zend_Controller_Plugin_Abstract
{
public function routeShutdown(Zend_Controller_Request_Abstract $request)
{
Zend_Layout::getMvcInstance()->setLayout($request->getModuleName());
}
}
Update your Bootstrap.php file’s autoloader initialization method (if you don’t have one, add one):
protected function _initAutoloaders()
{
$this->getApplication()->setAutoloaderNamespaces(array('Galahad_'));
return $this;
}
Please note: You might need to have other namespaces in there, like My_ or App_ or Default_.
Update your Bootstrap.php file’s plugin initialization method (if you don’t have one, add one):
protected function _initPlugins()
{
$this->bootstrap('autoloaders');
$this->bootstrap('frontController');
$plugin = new Galahad_Controller_Plugin_Modularlayout();
$this->frontController->registerPlugin($plugin);
}
Just make sure you have a layout file in your layouts directory for each module (modulename.phtml).
Recent Comments