Archive for the ‘Zend’ tag
Zend Framework Bootstrapping Modules
I am working on a small API for bluesignal and i wanted a modular architecture. I have done this before using the Zend Framework. But this time i wanted a bit more control while loading the modules. And adding a Bootstrap class would seem like a good option. The only example i could find involved loading all bootstraps on every request. Which doesn’t seem like a good idea. So after reading through the Manual and some blog posts. I decided to give it s shot my self.
The structure i want looks like this.
The application.ini file has the following contents:
includePaths.library = APPLICATION_PATH “/../library”
bootstrap.path = APPLICATION_PATH “/Bootstrap.php”
bootstrap.class = “Bootstrap”
resources.frontController.moduleDirectory = APPLICATION_PATH “/modules”
resources.modules[] = “default”
resources.modules[] = “admin”
includePaths
This sets the applications local library location. Any shared code for this application goes here.
bootstrap.path & class
Define the location and type of the Bootstrap class.
resources
Define the modules location and create a list of modules.
The main Bootstrap class
application/Bootstrap.php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
Load the config parameters for this application and set some debugging settings if needed.
protected function _initConfiguration()
{
$app = $this->getApplication();
$config = $app->getOptions();
if (APPLICATION_ENV == 'development') {
error_reporting(E_ALL & E_STRICT);
if (isset($config['phpsettings'])) {
foreach ($config['phpsettings'] as $setting => $value) {
ini_set($setting, $value);
}
}
}
}
We need autoloading here because we are using a class from the application library. Right now this causes a problem. A notice is thrown
Warning: include_once(FrontController.php) [function.include-once]: failed to open stream: No such file or directory in Zend/Loader.php on line 147
The application responds fine. And this problem seems to be a recurring issue (ZF-7224, ZF-7550) for the framework. Until now i have not find a graceful fix for this. besides a small patch reversion.
protected function _initAutoload()
{
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->setFallbackAutoloader(true);
return $autoloader;
}
Setup the controller to register the Bluess_Modules_Loader plug-in. And set the prefixDefaultModule parameter so we can prefix the default module controllers as well. Just for the sake of consistency. The Bluess_ namespace is part of my API. And can be changed at will.
protected function _initController()
{
$this->bootstrap('FrontController');
$controller = $this->getResource('FrontController');
$modules = $controller->getControllerDirectory();
$controller->setParam('prefixDefaultModule', true);
$controller->registerPlugin(
new Bluess_Modules_Loader($modules)
);
return $controller;
}
Now the last method. which is a bit weird. And i am probably missing a key factor here. But if this method resource is not declared only the default module functions. When declared empty all modules function as they should. This would indicate that this method could be used to load the modules. But i haven’t found a way to achieve this yet. Except for loading all modules in a row. Which makes no sense for this purpose. So we leave it empty.
protected function _initModules()
{
// Call to prevent ZF from loading all modules
}
The most important part here is the controller plug-in. This will be the place where module bootstraps are called from.
application/../library/Bluess/Modules/Loader.php
class Bluess_Modules_Loader extends Zend_Controller_Plugin_Abstract
{
protected $_modules;
Setup the plug-in by passing the applications module list.
public function __construct(array $modulesList)
{
$this->_modules = $modulesList;
}
The dispatchLoopStartup method will be called on every request and will do the magic. Based on the current module name we create a new Zend_Application with the current modules config file module.ini. And we bootstrap it.
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
{
$module = $request->getModuleName();
if (!isset($this->_modules[$module])) {
throw new Exception("Module does not exist!");
}
$bootstrapPath = $this->_modules[$module];
$bootstrapFile = dirname($bootstrapPath) . '/Bootstrap.php';
$class = ucfirst($module) . '_Bootstrap';
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/modules/' . $module . '/configs/module.ini'
);
if (Zend_Loader::loadFile('Bootstrap.php', dirname($bootstrapPath))
&& class_exists($class)) {
$bootstrap = new $class($application);
$bootstrap->bootstrap();
}
}
}
Now setup the default module. Once this is done it’s a nice example for further modules. Make sure the module has it’s own layout set.
application/modules/default/configs/module.ini
default.resources.layout.layout = “default”
default.resources.layout.layoutPath = APPLICATION_PATH “/modules/default/layout”
Setup the modules bootstrap and use it to set the modules model location.
application/modules/default/Bootstrap.php
class Default_Bootstrap extends Zend_Application_Module_Bootstrap
{
protected $_moduleName = 'default';
protected function _initConfiguration()
{
$options = $this->getApplication()->getOptions();
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/modules/' . $this->_moduleName . '/models'),
get_include_path(),
)));
return $options;
}
}
That’s all. Now make sure your layout is set correctly and the controllers are prefixed
application/modules/default/layout/default.phtml
echo $this->layout()->content;
application/modules/default/controllers/IndexController.php
class Default_IndexController extends Zend_Controller_Action
{
It took me a while to get this working like i had it in my mind. But it’s going the right way. If your interested in a working copy. You can download one here.
Zend Server CE MyPHPAdmin mysqli not loaded
The last time i installed Zend Server i did it from source. Now it was time for the Ubuntu package manager to do it for me. I’ll spare you the install process. Since it’s documented well by the Zend team.
After the install i walked through all pages of the ZS GUI. And all seemed to work great. So i checked the repository if there were anymore packages i could add to the ZS setup. And found the myphpadmin package. Just what i needed. And as i remember this was missing from the previous version. So i installed the package. And the GUI now showed a link to the phpmyadmin application. But when triggering the page i was confronted with an error message.
So mysqli was not loaded. Well that’s possible of course. But the repository showed it was installed. And so did the phpinfo() page.
So after looking around the config files for a while. It hit me. The ZS GUI is running on lighttpd and not apache. And just as i expected. Apache loaded the mysqli extension. But lighttpd didn’t.
A work around for this problem would be to leave of the :10081 port number in the GUI URL. So it points to 127.0.0.1/myphpadmin/. Or add the extensions to the lighthttpd setup. But adding a single line to the php-fcgi.ini file.
$ vi /usr/local/zend/gui/lighttpd/etc/php-fcgi.ini
$ extension=mysqli.so
This makes it possible to load phpmyadmin from the GUI. But the mysqli missing error is still displayed. So it’s probably better to load it over Apache. I tried to find a bug report link or something for the ZS project but couldn’t find any……
Zend Server Community Edition Beta!
This week Zend released a Beta version of the new Zend Server and Zend Zerver CE (Community Edition). So last night i couldn’t sleep and decided to take a look at the CE version of the two.
Zend Server CE
The package is a nice stack of applications / components that build up a solid PHP development environment. The package consists of the following main parts. And of course some 3rd party libraries to support the PHP extensions.
PHP v5.2.8
Apache v2.2.10
Zend Data Cache v4.0.32
Zend Debugger v5.2.17
Zend Guard Loader v3.3.5
Zend Java Bridge v3.1.21
Zend Optimizer+ v4.0.27
Zend Framework v1.7.4
Like i said it’s a great stack of applications and components that gives you a solid PHP development environment up and running in seconds. On top of that it provides the nice looking web GUI in which you can easily configure most of the suite. It’s a breeze to activate or deactivate PHP extensions and Zend components. And almost all PHP ini settings can be configured here as well. There’s some logging functionality that’s always handy.
Although it would have been nice if there would be some way to configure the web server from the interface as well. And maybe some integration with PEAR? It would have been nice if the web interface had some configuration options for Apache and PEAR as well. But besides that. I like the package and think Zend did a great job on this one. The application is pretty self explanatory so i will not spend more words on it. Besides Zend did a great job at documenting it all.
http://www.zend.com/en/products/server/getting-started/?zsp=gettingstarted
http://files.zend.com/help/Zend-Server/zend-server.htm
Below i posted some screen shots of Zend Server CE in action:
After logging in we are greeted with a nice dashboard screen. That provides shortcuts to the rest of the application.
The Server info page shows everything running under the hood of Zend Server CE.
Configuring and activating or deactivating PHP extension sis a breeze.
The same counts for the Zend components which can be configured and activated or deactivated at will.
Configuring PHP ini settings has never been this easy
SVN over SSH in Zend Studio for Eclipse
This should be pretty damn straight forward. However i ran into some problems while setting this up. At my daily work spot we use SVN over HTTP so we can do NTLM authentication. This sucks in many ways. But most of all it’s slow. And i mean really slow. Last week i tried to retrieve the log information for a project in the repository. This took my IDE 30 minutes. At first i thought my IDE was just being lame on me. But it turns out it’s SVN over HTTP that’s the culprit.
So i started to do some testing over the standard svn:// protocol. And retrieving the logs on the same repository took about 2 seconds. Amazing. That’s enough reason to switch to the svn:// protocol. This however was not possible. Because our network policy doesn’t allow for multiple log ins. All is centrally stored on some M$ domain controllers. That’s why SVN over HTTP was used in the first place. End of story i thought.
But i couldn’t let go of this. The situation becomes less workable every day. So i decided to look around for other options. When i notice some log ins were merged to the *nix boxes. i immediately thought about using SSH in combination with SVN. Logging in on the *nix box was no problem. So let’s try that with SVN.
svn+ssh://location/of/repo
This gives an error about a mismatched handshake. And i just couldn’t get this to work until i found a post made by Martin Woodward. He explains how to setup an environment variable on my local windows machine. To the tortoise PLink executable.
Set the environment variable (by right-clicking on My Computer, Properties, Advanced, Environment Variables, New):-
Variable name: SVN_SSH
Variable value: C:\\Program Files\\TortoiseSVN\\bin\\TortoisePlink.exe
This seemed to work i thought. i was presented with a login box. But no matter how i tried i couldn’t login. It just kept giving me the login box. So i opened a SSH session to the server and tried to do the same from there.
svn list svn+ssh://location/of/repo
After hitting enter i typed my password and got the following message.
bash: line 1: svnserve: command not found
svn: Connection closed unexpectedly
Now we’re getting somewhere. So svnserve could not be found. Probably because it’s not in the $PATH variable. So let’s add it there.
vi .bashrc
PATH=$PATH:/usr/local/bin
So let’s try to get a listing of the repo.
svn list svn+ssh://location/of/repo
This gave me the following message:
svn: No repository found in ’svn+ssh://location/of/repo’
Seems that when you use SVN over SSH you need to give the full location to the repository. Not the web server path but the complete file system path. After changing the webserver path for the file system path everything worked out. From here on adding the repository to eclipse is easy.
I always open the repository view. From there i right click > New > repository Location. The URL to add will look something like
svn+ssh://file/system/location/of/repository
So now we can enjoy the speed of the svn:// protocol. And the security of SSH. So let’s do some coding
Zend_Filter_Inflector and the CamelCaseToUnderscore filter
I was thinking about how to add theme support to an application that makes use of the Zend_Layout component. When i came across the Zend_Filter_Inflector class. This looks interesting. From teh ZF manual:
Zend_Filter_Inflector is a general purpose tool for rules-based inflection of strings to a given target.
This little class makes it easy to convert string to for instance paths. Anyway. i was trying to configure this class in my Bootstrap class. And run into some problems. I was testing the example in the manual. which makes use of the Zend_Filter CalemCaseToUnderscore. But when i reloaded my page instead of the usual output i was treated by an error.
$inflector = new Zend_Filter_Inflector(':script.:suffix');
$inflector->setRules(array(
':script' => array('CamelCaseToUnderscore'),
'suffix' => 'html'
));
Zend_Loader_PluginLoader_Exception: Plugin by name CamelCaseToUnderscore was not found in the registry. in Zend\Loader\PluginLoader.php on line 370
When i looked at the structure of the Zend folder. I noticed the CamelCaseToUnderscore filter was not available in the root of the Filter directory. Instead it’s placed inside the Filter/Word directory. So i prefixed the class name with Word_. And this works fine. The code now looks like this.
$inflector = new Zend_Filter_Inflector(':script.:suffix');
$inflector->setRules(array(
':script' => array('Word_CamelCaseToUnderscore'),
'suffix' => 'html'
));
it’s a minor issue but i reported it anyway.
Zend Framework is confused
I decided to give the Zend Framework a try. The first attempt to create a simple SOAP based login.
In the first few hours i had absolutely now problems building my small application. But for some strange reason after a few hours ZF started to throw error’s. Mainly for files it was trying to include. These files were in the correct location. Nothing changed. The include path was still set correctly. I found a few bug reports with similar problems. Although there were some solutions. I was not satisfied with them. And had the feeling the problems were caused by something else.
So after i read threw all code. I notice a few lines i copied from Cal Evans book. These lines mainly set the include_path for the current application. And it looked something like this:
$lib_paths = array(); $lib_paths[] = "/path/to/application"; $lib_paths[] = "/path/to/library"; $inc_path = implode(PATH_SEPARATOR, $lib_paths); set_include_path($inc_path);
I noticed the application directory was added first. So this is also the first part where PHP starts looking for it’s include files. So i decided to switch them. So the code looks like this now:
$lib_paths = array(); $lib_paths[] = "/path/to/library"; $lib_paths[] = "/path/to/application"; $inc_path = implode(PATH_SEPARATOR, $lib_paths); set_include_path($inc_path);
After this change the problem seems to have been resolved. I still would like to figure out why ZF has problems with finding it’s own include files. Maybe it’s a combination of the wrong include order and the .htaccess file not working well together.
Dutch PHP Conference 2008 :) +20%
I wanted to finish this post last weekend, But i was feelings so bad (flew). I haven’t really been near my computer untill now. So here goes. Last Saturday me, Bart and Robbert headed over to the second edition of the Dutch PHP Conference. After getting the name tags. We headed over to the main room. Where i scored a nice PHPWoman t’ shirt for my wife
Not much later the first keynotes started.
The first keynote was by Zeev. Which i had high hopes for. It’s a opportunity to hear one of the guys that made PHP to what it is. Give a talk. It was mainly about the history of PHP. Because nobody in the audience seemed to know about it. Or they just wanted to hear it from the master himself. I don’t know. I enjoyed the talk.
After Zeev Marco Tabini hit the stage. Which was pretty funny. This guy has a way of talking. And his slides connect to that perfectly. It was amusing and interesting. Although i had some problems getting comfortable in the main room chairs. the name of the talk was “PHP and the Taste of Mayo”. And it was mainly about keeping it simple. And choosing the right tools for the job.
After the keynotes it was time for lunch. Because i was feeling a bit sick i wasn’t really hungry. But the food was good. All kinds of sandwiches and the tasty saucijzenbroodjes. Just good variety. Outside we sniffed some fresh air.. And tried to figure out to which talk we wanted to go. Since we don’t do anything with unit testing at the moment. We decided to go to Sebastian Bergmann’s talk about PHPUnit 3.3.
Sebastian quickly skimmed through the basics of PHPUnit. How to create a test class and the use of mock objects. After that he showed some new features in PHPUnit 3.3. One of wich is based on behavior based development. It was a story based testing. With the use of method chaining phrases can be build to execute the execute scenario’s. I have to say it’s a strange way. And the code looks kinda weird. I had the feeling Sebastian was also not to happy about it. I guess he tries to keep his users happy. He dropped some words about a new project. I think it was PHP Depend. But i kinda forgot. Couldn’t find any info about it on the net. So we keep that for another time. I had the feeling Sebastian was rushing a bit. He didn’t have enough time. But it was good talk. His slides can be found here.
After the first talk we took something to drink and walked around a bit. Waiting for the next talk to start. We decided to go to Mayflower and Sektioneins security talk. Which was something different then i expected. It was not as technical as i had hoped. But it touched some nice topics. I had hoped Stefan Esser would be there also. But Johann-Peter Hartmann did a great job. The talk was mainly was about Risk assessment. How to identify risk points in your application flow. And how to anticipate to those points. The talk also showed some malicious web trends. Some nice graphs that hacking web applications is not for fun anymore.
When the second talk was over we went outside. i was feeling pretty bad. Seems every time when there’s a conference i come down with the flew. Was thinking about going home. But decided i didn’t want to miss the last two talks. So we headed back to the main room. After getting a nice cold coke. We picked some nice seats and waited for Stefan Priebsch to begin his talk about PHP 5.3 and PHP 6. This was one of the best talks i think. it was pretty interesting to hear about some new features. Although i knew about most. There was a bit of e discussion about namespaces. Some guy in the audience was screaming for attention. Although he made some points. I had the feeling i was sitting near a real life troll
Anyway Stefan’s talk was good. A bit of irony about the up coming PHP 6 with Unicode support. And some new features in PHP5.3 like, Garbage Collection, SPL, Late static binding which was very interesting. With some nice examples. I ended with a small plug for his new book. Which you can buy over at PHP|Architect. The slides for his talk can be found here.
The final talk was given by Terry chay (the PHP terrorist). I looked forward to this talk. I heard and read a lot about his way of presenting in front of a crowd. And i can tell you. This guy rocks. The talk was of course not without some Ruby on Rails bashing. I had to say this was one hell of an inspiring talk. The talk was titled The Internet is an Ogre: Finding Art in the internet Architecture. His talk was about what he called the four s’s (Stability, Scalability, Speed, and Security). How to build an application the right way. From the bottom up. I didn’t find any slides to the talk yet. They will probably pop up somewhere.
After the last talk ended. All speakers got on stage and received some goodies and a big applause from the audience. The conference was over. And because i wasn’t feeling to well. I went home immediately. Overall i think it was a great day. I learned some new thing. Witch for me is the main thing about conferences. On our way out we picked up a goody bag. Which had a very nice t’ shirt. And some promotion materials. Thanks to all the people who made this happen. And hope till next year
By now some pictures are apearring on flickr.
Hide .project files from SVN in Zend Studio for Eclipse
I’ve been using Zend Studio for Eclipse (ZendNeon) ever since it came out. I just love this IDE. The fact that i have all tools at my finger tips without needing to switch between applications is just great. Specially when using SVN. But one thing was bothering me. Every time i started a new project. I had to add the svn:ignore for the .project file. There is really no reason to store this in SVN.
Untill yesterday i just added the svn:ignore tag to every project i start. But my eye fell on this post in the yahoo group for ZendNeon. It’s an old post. But yesterday some guy posted a better way of settings this globally.
So to exclude the .profile file from being committed to SVN do the following:
Window > Preferences > Team > Ignored Resources and click the Add Pattern button.















