source code bean

06 Jun, 2010

Two new features I really like in PHP 5.3

Posted by: Peter In: PHP

Namespaces
Finally PHP has support for namespaces, not a day too late! Before the days of object oriented PHP name clashes for functions was usually prevented by adding a prefix to your function names. When object oriented PHP was introduced function name clashes was less likely, but class names still had to be unique, ie you could only have one class named for example “User”. This led to the need of prefixing class names. From PHP 5.3 we no longer have to do this!

Namespaces in PHP introduced two new keywords:

  • namespace – define a namespace
  • use – Use a namespace
  • use … as – Use a namespace but give it an alias name

Late static binding

Lambda functions and closures
Lambda functions, or anonymous functions as they are called in PHP, is really something I have missed in PHP. Lambda functions are very convenient to use when you are dealing with callbacks, for example:

  1.  
  2. <?php
  3. echo preg_replace_callback(‘~-([a-z])~’, function ($match) {
  4.     return strtoupper($match[1]);
  5. }, ‘hello-world’);
  6. // outputs helloWorld
  7. ?>
  8.  


Closures are also a very welcome addition to the language. A closure is not the same thing as an anonymous method, which seem to be a widespread misunderstanding. This is the definition from wikipedia of what a closure is:

“In computer science, a closure is a first-class function with free variables that are bound in the lexical environment. Such a function is said to be “closed over” its free variables. A closure is defined within the scope of its free variables, and the extent of those variables is at least as long as the lifetime of the closure itself.” - Wikipedia

In PHP closures will allow us to define closures like this:

  1.  
  2. <?php
  3. $sayhito= function($name)
  4. {
  5.     printf("Hi %s\r\n", $name);
  6. };
  7.  
  8. $greet(‘Foo’);
  9. $greet(‘Bar’);
  10.  
  11. // This will print
  12. // Hi Foo
  13. // Hi Bar
  14. ?>
  15.  


Pretty sweet!

06 May, 2010

Yahoo Pipes is still very cool

Posted by: Peter In: Uncategorized

I know that Yahoo Pipes is not a new thing, it came out in 2007 so in web years it is almost a 100 years now, but it is still darn cool! If you somehow have managed to miss what it is, this is how wikipedia describes it:

“Yahoo! Pipes is a web application from Yahoo! that provides a graphical user interface for building data mashups that aggregate web feeds, web pages, and other services, creating Web-based apps from various sources, and publishing those apps”

So why I am blogging about this in 2010? Well, it is still really useful. For many years I have been following the CodeSOD section on the blog “The daily WTF”, the rest of the stuff on there is funny as well, but I enjoy the CodeSOD the most. On the blog there was no option of subscribing to only one section, so i deiced to create a Pipe to feed me the CodeSOD only.

The user interface is super easy, just drag items from the list on the left and connect them together:
CodeSOD Pipe

My CodeSOD pipe can be found here if you want to subscribe to it!

Recently I started a new assignment at a customer where we are going to build their new public web site and their intranet, both of them based on EPiServer CMS 6. The person leading the project is very found of ASP.NET MVC, so my first task was to experiment with EPiServer and ASP.NET MVC, to try to get a running prototype up.

I did some research online to see what others had done. I found two great posts by Joel Abrahamsson and one from Fabio Fabrizio, who based his solution on Joels experiments.

Joels first attempt was to create a base class from which all Controllers inherited. However, this made the controller do more than it should and made it very hard to test. His second attempt was to create a custom model binder and make every action receive currentPage as a parameter. This approach makes it much easier to create unit tests for the code, since it is now possible to moc the currentPage parameter.

Fabio took a different approach and decided to implement a custom MVC handler and a controller factory. This approach fits very well with MVC and felt like the right way to go, so I decided to create a prototype based on Fabios code. Fabios code was based on EPiServer CMS 5 and ASP.NET MVC 1.0, but I wanted to use EPiServer 6 and ASP.NET MVC 2.0, so I started porting the solution.

Porting the solution to EPiServer 6 and ASP.NET MVC 2.0 required some changes to both code and configuration, which not all of them was trivial. At the moment I have a prototype up running, and it works remarkably well! I will continue my experiments and post my results.

Recently I got a request from a customer to perform some search engine optimizing for an old EPiServer site we are maintaining. One of the optimizations was to fix the paging on their product page. The products are fetched from an external data source and are not stored as pages in EPiServer, thus normal EPiServer paging controls can not be used. Image a normal paging control like this:

Prev 1, 2 , 3, …, 99 Next

A fairly normal approach to this would have been to use a query parameter for handling the paging. Like this:

  1. <a href="/products/?page=4">4</a>

By using this approach each page would have a unique entry point, using query strings is however not optimal for SEO, but even worse is using asp:LinkButtons. Whoever created the site had decided to use asp:LinkButtons. LinkButtons are very convenient to work with in ASP.NET, but the HTML code they generate is not very SEO friendly. This is the HTML that was generated:

  1.  
  2. <div id="ctl00_MainContent_pnlPaging">
  3.  
  4.   <a id="ctl00_MainContent_rptPages_ctl00_lbPage"
  5.        href="javascript:__doPostBack(‘ctl00$MainContent$rptPages$ctl00$lbPage’,”)">1</a>
  6.  
  7.   <a id="ctl00_MainContent_rptPages_ctl01_lbPage"
  8.        href="javascript:__doPostBack(‘ctl00$MainContent$rptPages$ctl01$lbPage’,”)">2</a>
  9.  
  10.   <a id="ctl00_MainContent_rptPages_ctl02_lbPage"
  11.        href="javascript:__doPostBack(‘ctl00$MainContent$rptPages$ctl02$lbPage’,”)">3</a>
  12.  
  13.   <a id="ctl00_MainContent_lbNext"
  14.        href="javascript:__doPostBack(‘ctl00$MainContent$lbNext’,”)">Next</a>
  15. </div>
  16.  

We can see that:

  • LinkButtons generates a javascript that is run when the link is clicked, instead of using a normal link
  • Paging is handled using a postback, the pages will no longer have unique entry points.

This is disastrous from a SEO perspective.

So lets fix it. First step is to replace the LinkButtons with normal links (asp:Hyperlink) – and then we are going to do some URL rewrite magic to create SEO-friendly URLs. The code behind was changed to assign links like /products/?page=4.

Now, to get URLs like /products/page/4, we can create a custom url rewrite module in EPiServer. First we create a new class, Rewrite, that inherits from EPiServer.Web.FriendlyUrlRewriteProvider. In this class we override three methods (don’t ask we why they have such confusing names, someone at EPiServer must have been under the influsene of something when nameing them):

  • ConvertToInternalInternal - Used to convert from /product/page/4/ to an internal EPiServer page with the page id as a query parameter
  • ConvertToExternalInternal - Used to convert from an internal EPiServer URL (/PageType.aspx?lotsofqueryparameters=values) to an external (/products/page/4/)
  • ConvertToInternal - Needed to work around URL rewrite caching behaviour in EPiServer, will get into more detail on this later.

This is the class:

  1. namespace Utils
  2. {
  3. public class Rewrite : FriendlyUrlRewriteProvider {
  4.   // The regexp to match a paged url
  5.   string _regexpPaging = @"(.+)/page/([0-9]+)/$";
  6.  
  7.   protected override bool ConvertToInternalInternal(
  8.     UrlBuilder url, ref object internalObject){}
  9.  
  10.   protected override bool ConvertToExternalInternal(
  11.     UrlBuilder url, object internalObject, Encoding toEncoding){}
  12.  
  13.   public override bool ConvertToInternal(
  14.     UrlBuilder url, out object internalObject) {}
  15. }


The first method we implement is the ConvertToInternalInternal which will convert the URL to an internal EPiServer URL.

  1. protected override bool ConvertToInternalInternal(UrlBuilder url, ref object internalObject)
  2. {
  3.   if (url == null)
  4.   {
  5.     return false;
  6.   }
  7.   // Regexp to match if the URL ends with /page/{Id}/
  8.   Match match = Regex.Match(url.Path, _regexpPaging);
  9.  
  10.   // If we have a match, remove the /page/{Id}/ from the end of the URL
  11.   // and add a querystring to the internal URL that is dqcPagingId
  12.   // (should have a unique name to not clash with some other querystring)
  13.   if (match.Length > 0)
  14.   {
  15.     url.Path = match.Groups[1].Value + "/";
  16.     url.QueryCollection["dqcPagingId"] = match.Groups[2].Value;
  17.     base.ConvertToInternalInternal(url, ref internalObject);
  18.     return true;
  19.   }
  20.  
  21.   // Now when the /page/{Id}/ is removed from the URL, and the querystring is added, we can let EPiServer do its normal URL-rewriting.
  22.   return base.ConvertToInternalInternal(url, ref internalObject);
  23. }


While not nessesary, we should override the ConvertToExternalInternal as well. This will let EPiServer automatically convert internal urls containing the dqcPagingId querystring to a external url ending with /page/{Id}/.

  1. protected override bool ConvertToExternalInternal(UrlBuilder url, object internalObject, Encoding toEncoding)
  2. {
  3.    // First let EPiServer convert the internal URL to an external. This will give us a URL like:
  4.    // /Products/?dqcPagingId=5 (if it is a paged page)
  5.    bool isRewritten = base.ConvertToExternalInternal(url, internalObject, toEncoding);
  6.  
  7.     // Check if the URLs query string contains dqcPagingId
  8.     // If it does we add /page/{Id} to the URL and removes the query string
  9.     if (url.Query.Contains("dqcPagingId"))
  10.     {
  11.       url.Path = string.Concat(url.Path, "page/", url.QueryCollection["dqcPagingId"], "/");
  12.       url.QueryCollection.Remove("dqcPagingId");
  13.     }
  14.  
  15.     return isRewritten;
  16. }


Now one can think we would be done, but there is one more method we need to implement. EPiServer uses a cache to cache URL rewrites from external URLs to internal URLs, but it will only cache the querystrings used by EPiServer, not the dqcPagingId we added. This gives a rather unexpected result. The first time the page is loaded everything loads fine, from code behind we can access the dqcPagingId and show the requested page. If you wait 10 senconds (default cache time) or more and reloads the page, it works fine. But if you reload the page sooner, the dqcPagingId will not be set when the page loads. This is because the querystring dqcPagingId is not cached by EPiServer. This took me quite some time to figure out.

The solution to the problem is to override ConvertToInternal to bypass the default caching:

  1. public override bool ConvertToInternal(UrlBuilder url, out object internalObject)
  2. {
  3.  
  4.   // If the URL end on /page/{id}/, bypass cache by calling ConvertToInternalInternal
  5.   // A more optimal solution would be to perform some kind of caching here
  6.   if (Regex.IsMatch(url.Path, _regexpPaging))
  7.   {
  8.     internalObject = null;
  9.     ConvertToInternalInternal(url, ref internalObject);
  10.     return true;
  11.   }
  12.  
  13.   // Else, it is ok to use the cached result
  14.   return base.ConvertToInternal(url, out internalObject);
  15. }
  16.  


The last step is to add our Rewrite module to the web.config:

  1.  
  2.   <providers>
  3.     <add
  4.        name="MyUrlRewriter"
  5.        enableSimpleAddress="true"
  6.        friendlyUrlCacheAbsoluteExpiration="0:0:10"
  7.        type="Utils.Rewrite, Utils"
  8.        description="URL rewriter for paging" />
  9.   </providers>
  10. </urlRewrite>
  11.  


Now we should be all done! Enjoy!

08 Mar, 2010

Trying out CouchDB for the first time

Posted by: Peter In: NoSQL

There are several good libraries that will abstract accessing CouchDB. However, in order to understand what goes on in the libraries, I think it is important to first understand what is going on on a lower lever, so that is what I will show you.

Step 1, install CouchDB. CouchDB for OSX can be downloaded here, the bundle contains bouth the Erlang runtime and CouchDB – no compiling or installing needed, just download the .dmg, mount it and run the application! If you are on Linux it is quite likely that CouchDB is in the repository of your distribution. In Ubuntu CouchDB is found in “Universe” and can easily be installed using apt. I haven’t tried CouchDB on Windows so I can’t give you any guidance here, if you try it out, please leave a comment.

Now when CouchDB is installed lets explore it using use one of my favorite tools, good old cURL. Port 5984 is the default port for CouchDB.

  1.  
  2. $ curl -X GET http://localhost:5984/
  3. {"couchdb":"Welcome","version":"0.10.0"}
  4.  

CouchDB is up running! Lets create a new database:

  1.  
  2. $ curl -X PUT http://localhost:5984/testdb
  3. {"ok":true}
  4.  

CouchDB’s responses are also in JSON form. Lets inspect the database we just created:

  1.  
  2. $ curl -X GET http://localhost:5984/testdb
  3. {
  4.   "db_name":"testdb",
  5.   "doc_count":0,
  6.   "doc_del_count":0,
  7.   "update_seq":0,
  8.   "purge_seq":0,
  9.   "compact_running":false,
  10.   "disk_size":79,
  11.   "instance_start_time":"1266963717052501",
  12.   "disk_format_version":4
  13. }
  14.  

CouchDB returns some statistics of the database, we can see that it contains 0 documents. Lets store an empty document in the database:

  1.  
  2. $ curl -X POST http://localhost:5984/testdb/ -H "Content-Type: application/json" -d {}
  3. {
  4.   "ok":true,
  5.   "id":"ef40feff87010a6ef3a45a16df5af977",
  6.   "rev":"1-967a00dff5e02add41819138abb3284d"
  7. }
  8.  

CouchDB returns the unique id for the document and the version number (did i mention that all documents are version controlled?:)). Next step is to fetch all documents:

  1.  
  2. $ curl -X GET http://localhost:5984/testdb/_all_docs
  3. {
  4.   "total_rows":1,
  5.   "offset":0,
  6.   "rows":[{
  7.     "id":"ef40feff87010a6ef3a45a16df5af977",
  8.     "key":"ef40feff87010a6ef3a45a16df5af977",
  9.     "value":{"rev":"1-967a00dff5e02add41819138abb3284d"}
  10.   }]
  11. }
  12.  

Yes! it is there! Now you should also be able to see your database “testdb” and the document you created in the CouchDB GUI.

01 Mar, 2010

CouchDB a NoSQL database

Posted by: Peter In: NoSQL

Traditionally relational databases has been the primary way of storing, sorting and searching data, and for most purposes they are very good at it. However, in the last few years – with the growth of cloud computing and sites such as Facebook pushing relational databases to its limits, people have started to look for other alternatives. The problems with relational databases is that it has been hard to scale them in a vertical way (scale them over several servers with linear, or close to linear, performance increase). A few databases in a cluster is no problem, but when it comes to several terabytes of data, which should be searched in real time, they are simply not enough. CouchDB and the other NoSQL databases aims to provide a truly horizontally scalable database.

NoSQL is a umbrella term for a wide variety of data stores, which all have in common that they do not store data in a relational way. Some examples of NoSQL databases are CouchDB, MongoDB, Amazon SimpleDB and Google BigTable. This is some of the properties they have in common:

1. No schema
The data is stored in one big hashtable like data structure. No schema is needed.

2. No more joins
Joins are slow in general, when they are spread out over several servers it gets even worse. In CouchDB there is no join, instead data should be duplicated. This might sound odd if you like me was taucht back in collage that normalization is of highest importance, and that you should really really avoid to duplicate data in your database. This is still true for most relational databases, but keep in mind that when relational databases was invented, disk space was expensive and normalization was a great way to save a few bytes. Storage is everything but expensive today, this is why NoSQL empathizes that data should instead be duplicated.

3. Eventual consistency
When you update the database there is no longer a quarantine that all subsequent queries will get the updated value immediately, it might take some time depending on the system load. For some systems such as banking systems, this kind of behavior would be a big no no. But for most large web sites, this is no problem, the data is going to be cached in one way or another anyway.


CouchDB

CouchDB is a document database, accessible via a RESTful JSON API. Everything stored in the database is a “document” and is stored in a flat address space.There are no schemas, the documents are stored and retrieved as JSON objects.

To address this problem of adding structure back to semi-structured data, CouchDB has something called “views”. A view is as close as we gets to a SQL query. The views are expressed in Javascript and consist of map and filter functions. They are built dynamically and will not affect the underlying data.

CouchDB is written in Erlang and runs on all systems that the Erlang runtime supports (Linux, Windows, OSX and other unix systems). I have tested CouchDB on Linux and OSX. This is a screenshot of the CouchDBX GUI running on OSX:

The JSON representation of the same document:

In my next post I will show you some hands on action with CouchDB.

Tags:

14 Feb, 2010

Articles coming up on CouchDB and ExtJS

Posted by: Peter In: PHP

It has been a bit too long since my last post so I wanted to give you an short update on what I have been up to lately. Last year I built a web service using Amazon SimpleDB, which got me interested in document based databases. Since then I have been reading and following the progress of different open source document based databases, such as CouchDB and MongoDB.

During the time, I have been thinking about what other kind of projects a document based database would be suitable for. Because my lack of imagination (or just because I was too eager to get started:)), i decided to build a simple Web CMS system using PHP and CoachDB. Pages in a CMS system are perfect for storing as documents in a document database, so it seemed like a good choice.

Building the CMS backend actually turned out to be what took the shortest time. I wanted to create a nice looking Javascript based GUI for the editors and administrators, and decided to build it using the javascript library ExtJS. I had never worked with ExtJS before (and I am more of a backend guy than a javascript frontend guy), so it took me some time to get my mind around it, but the result is sweet – ExtJS is really powerful.

The CMS is far from complete, but so far it has given me quite a few ideas for new blog posts, so stay tuned for the upcoming posts on CoachDB and ExtJS!

In this post I am going to show how easy it is to create a JSON-RPC web service using the built in support in Zend Framework.

First we need to create the php-file that will handle the incoming RPC calls. It is not advised to put this inside the MVC structure of a Zend web application, since that will lead to unessesary complexity and overhead. The Zend people recommend that we create the JSON-RPC under /public/api/vX/, so lets create the file /public/api/v1/jsonrpc.php (if you haven’t setup your Zend MVC structure, read my blog post Getting started with the zend framework to get started).

We will have to do the regular bootstrapping to get our application up and running:

  1.  
  2. // Define path to application directory
  3. defined(‘APPLICATION_PATH’)
  4.     || define(‘APPLICATION_PATH’, realpath(dirname(__FILE__) . ‘/../../../application’));
  5.  
  6. // Define application environment
  7. defined(‘APPLICATION_ENV’)
  8.     || define(‘APPLICATION_ENV’, (getenv(‘APPLICATION_ENV’) ? getenv(‘APPLICATION_ENV’) : ‘production’));
  9.  
  10. // Ensure library/ is on include_path
  11. set_include_path(implode(PATH_SEPARATOR, array(
  12.     realpath(‘../../../library’),
  13. )));
  14.  
  15. /** Zend_Application */
  16. require_once ‘Zend/Application.php’;
  17.  
  18. // Create application, bootstrap, and run
  19. $application = new Zend_Application(
  20.     APPLICATION_ENV,
  21.     APPLICATION_PATH . ‘/configs/application.ini’
  22. );
  23.  
  24. $application->bootstrap();
  25.  

The next step is to create the class that will be exposed through the service. I will create a very simple class that will simply perform an addition of two ints. It is very important to describe the input parameters using the @param directive in the comment. This information is used by the Json Server when creating the SMD (Service Mapping Description).

  1.  
  2. /**
  3.  * Simple – sample class to expose via JSON-RPC
  4.  */
  5. class Simple
  6. {
  7.     /**
  8.      * Return sum of two variables
  9.      *
  10.      * @param  int $x
  11.      * @param  int $y
  12.      * @return array
  13.      */
  14.     public function add($x, $y)
  15.     {
  16.         return array(‘result’ => $x + $y);
  17.     }
  18. }
  19.  

The last step to get the JSON-RPC server running:

  1.  
  2. // Instantiate server, etc.
  3. $server = new Zend_Json_Server();
  4. $server->setClass(‘Simple’);
  5.  
  6.  
  7. if (‘GET’ == $_SERVER[‘REQUEST_METHOD’]) {
  8.     // Indicate the URL endpoint, and the JSON-RPC version used:
  9.     $server->setTarget(‘/api/v1/jsonrpc.php’)
  10.            ->setEnvelope(Zend_Json_Server_Smd::ENV_JSONRPC_2);
  11.  
  12.     // Grab the SMD
  13.     $smd = $server->getServiceMap();
  14.  
  15.     // Return the SMD to the client
  16.     header(‘Content-Type: application/json’);
  17.     echo $smd;
  18.     return;
  19. }
  20.  
  21. $server->handle();
  22.  

Now your JSON-RPC Server should be up running. Browsing http://{your web server}/api/v1/jsonrpc.php should result in the following SMD:

  1.  
  2. {
  3.   "transport":"POST",
  4.   "envelope":"JSON-RPC-2.0",
  5.   "contentType":"application\/json",
  6.   "SMDVersion":"2.0",
  7.   "target":"\/api\/v1\/jsonrpc.php",
  8.   "services": {
  9.       "add":{
  10.           "envelope":"JSON-RPC-2.0",
  11.           "transport":"POST",
  12.           "parameters":[
  13.               {"type":"integer","name":"x","optional":false},
  14.               {"type":"integer","name":"y","optional":false}],
  15.           "returns":"array"
  16.        }
  17.     },
  18.    "methods":{
  19.        "add":{
  20.            "envelope":"JSON-RPC-2.0",
  21.            "transport":"POST",
  22.            "parameters":[
  23.                {"type":"integer","name":"x","optional":false},
  24.                {"type":"integer","name":"y","optional":false}],
  25.            "returns":"array"}
  26.        }
  27. }
  28.  

jQuery does not support calling JSON-RPC services out of the box, but fourtenly there is plenty of plugins for jquery that fixes this. One is the JSON-RPC client found here. Download the client and put the javascript files into your /js folder. Then create a new file test.html and add the following html:

  1.  
  2. <html>
  3. <head>
  4.         <script LANGUAGE="javascript" SRC="js/jquery-1.3.min.js"></script>
  5.         <script LANGUAGE="javascript" SRC="js/json2.js"></script>
  6.         <script LANGUAGE="javascript" SRC="js/jquery.zend.jsonrpc.js"></script>
  7.         <script>
  8.                 $(document).ready(function(){
  9.                         test = jQuery.Zend.jsonrpc({url: ‘/api/v1/jsonrpc.php’});
  10.                         alert(test.add(1,1)['result']);
  11.                 });
  12.         </script>
  13. </head>
  14. <body>
  15. </body>
  16. </html>
  17.  

We are all done! Browsing test.html should result in an alert box containg the result.
result

Congratulations! You have created a JSON-RPC service!



Read more about the Zend Framework:

Today I upgraded a project we started working on last year from Zend Framework 1.7 to Zend Framework 1.9. I excepted to run into several API incompatibilities, but the only problem I got was the autoloader.

In Zend 1.7, and earlier versions, the autoloader was registered like this:

  1.  
  2. require_once "Zend/Loader.php";
  3. Zend_Loader::registerAutoload();
  4.  

In Zend Framwork 1.9 this has changed slightly, you now have to register the namespaces you want to autoload:

  1.  
  2. require_once ‘Zend/Loader/Autoloader.php’;
  3. $loader = Zend_Loader_Autoloader::getInstance();
  4. $loader->registerNamespace(‘Dqc_’);
  5.  

This was the only change we needed to do to upgrade from 1.7 to 1.9, quite impressive!

13 Dec, 2009

Getting started with the Zend Framework

Posted by: Peter In: PHP| Web| Zend Framework

It used to be a bit tricky to get started with the Zend Framework. The Zend Framework is very flexible and allows you to set it up in almost any way that fits your needs. This means for example that the directory structure and location of files is up to you, however – there is a recommended layout. When I first started using Zend I had to figure out this by looking at examples and reading the (at that time) rather poor documentation available. In Zend Framework 1.9 a tool, zf.sh, was introduced. It simplifies creating a new site a lot. In this blogpost I will guide you though the process of setting up a Zend development environment in OS X. The only part that is OS X specific is MAMP. Zend Framework runs just fine under Windows and Linux as well.

Step 1 – Getting a *AMP setup (LAMP, MAMP, WAMP)
The AMP (Apache, Mysql, PHP) stack is available for almost all modern operating systems. I am writing this on my Macbook, so in this tutorial I will use MAMP. When I develop PHP in Windows I usually use WAMPServer and in Linux you can install the LAMP-stack using the packaging system in most distributions.

Setting up MAMP is pretty straightforward, download the MAMP .dmg-file and drag the MAMP folder to your Applications folder.
mamp

Start the application and press the “Open start page” button to make sure everything works. On the start page you will find information about your site, phpInfo, phpMyAdmin and SqLiteManager. We will get back to configuring the http root directory later.

Step 2 – Download and install Zend Framework
Go to the framwork download page and download the minimal distribution. In this tutorial I am using 1.9.6, but the instructions will probably apply to all 1.9 versions.

Extract the downloaded file and move the folder to a shared location, for example /usr/local/. Next step is to create an alias for the zf.sh tool. Edit your ~/.bash_profile and add the following line (change the path to where you moved the extracted files):

zf=/usr/local/ZendFramework1.9/bin/zf.sh

This will allow you execute the zf tool without using the full path. Try it out by executing zf show version, it should return the version number of the file you downloaded.


$ zf show version
Zend Framework Version: 1.9.6

Step 3 – Create your project
Go to the folder where you want to create your new project, in my case ~/Development/. Run zf create project zf-tutorial. This will setup the default directory structure and create the necessary files.

Zend Framework directory structure

The application/ folder is where the source code for your website lives. It contains separate folder for models, views and controllers. The public/ folder is the folder that is going to be your document root.

Now you need to copy the Zend library (in my case /usr/local/ZendFramework1.9/library/Zend) or create a symlink for it so your site can find the Zend files. I prefer using a symlink:


$ cd ~/Development/zf-tutorial/library
$ ln -s /usr/local/ZendFramework1.9/library/Zend/ Zend

Step 4 – Run the project
The last step before we will have running site up is to configure the step that we skipped in Step 1, configuring the Apache document root.
sitesettings

Open up MAMP and click the preferences button, under the apache tab you will find the document root. Click select and navigate to the “public” folder in your zend project. Apply the settings and restart the server.

Now open your browser and direct it to http://localhost and you should see the default welcomescreen:
zenddefault

Now open your application/controllers/IndexController.php and start hacking your code!



If you want to learn more about the Zend Framework i have some posts on some more advanced topics:

Categories

Adwords

Twitter Updates


    • Petan: I got in bootstrap this (insted of $frontController->getRouter()): $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini', 'rout
    • oanh tong ngoc: :) It's usefull but could U give everyone's an example with a project source code. Thanks
    • Peter: Hi Sohaib, It seems like the rewrite module isn't loaded by IIS. Have you uploaded the UrlRewriter dlls and made the changes to web.config on the ser

    About

    Welcome to source code bean! You will find information on tips and tricks on programming languages, server side stuff, and anything that causes troubles to web development.