Hello World

Practical programming... and stuff...

Subscribe to RSS feed

Posts tagged with "PHP"

Extending Zend Framework Route and Router for custom routing

,

Continuing on posts related to making a CMS with the Zend Framework, todays topic is Zend_Controller_Router_Route, Zend_Controller_Router and customizing the routing in Zend Framework.

The default routing scheme is like this: site.com/module/controller/action. While this works just fine for "normal" sites, it doesn't work very well for a CMS. This is because you would need to create copies of controllers if you wanted to have many similar pages or having longer URLs with the page name as a parameter in the end.

I wanted a better URL scheme for my CMS: site.com/module/page/action where module and action are optional. As you can see, there is no variable for the controller, so how do we tell the framework which controller the request needs to be routed to?

Read more...

Using layouts with Zend ViewRenderer helper

,

Note: this post is very outdated. Unless you're into archeology, I suggest using Zend_Layout.

When working with views in the Zend Framework, you normally have a view for each action in your controllers. Each of the views run a view script, which then include header and footer views in them. This may lead to some repetitive code and may cause a problem if you want to modify the way the header and footer are included in your views.

A better approach could be to use a "layout". A layour is a master view, perhaps similar to the master page in ASP.NET - it has the header, footer and all other code except the content. It effectively replaces the header and footer and is used to include the content (the action view script) inside itself instead of the action view script including the header and footer.

Read more...

Building a CMS with the Zend Framework

, ,

I've been working on a Content Management System (CMS) for my personal use and for experimenting. Since I like the Zend Framework so much, using it as a base for the CMS was a natural choice.

While at first it might seem that using ZF adds many restrictions, it actually isn't like that. Thanks to the modularity of the framework, you can easily replace certain functinality with classes of your own to change the way it works.

There are also many other things to consider when making a CMS.

Read more...

ORM in PHP using Propel

,

Todays topic is ORM, PHP and the Propel framework.

ORM (Object-Relational Mapping) is a name for mapping relational data, such as rows from a database, to objects, such as classes in PHP.

It's not the easiest thing to do, considering that you can have complex relations between your tables in a database. For example, you might have a blog post with an user_id column which links the post to the users table.


I recently tried out Propel, which is very promising. Propel is an ORM framework for PHP 5 which simplifies database access a lot. It has a bit similar interface to databases as the Active Record design pattern describes. It also has some advantages compared to other ORM/ActiveRecord implementations such as Zend_Db or ADOdb_Active_Record

Read more...

Globals are evil

, ,

Globals are evil - do not use them. Quoting Martin Fowler,

remember that any global data is always guilty until proven innocent.



What is a global? A global is a variable that is accessible from anywhere in an application. For example, in PHP declaring any variable outside a function/class method will make it global.

But what about when I want to have some data globally available in my application?
One useful solution is to use the singleton pattern to create a storage class or something like that. We'll look at that later in this post.

Read more...

PHP and named pipes

,

Sometimes you might need to communicate with some other process when working with PHP. Say you have a custom server application running on your server and you need to send it some data when a user accesses your website.

There are a few things you could do to achieve this:
  • Create a file and poll it with your server for new data
  • Use a database and poll it from your server for new rows
  • Use a named pipe


As you can see from the list, all other options except named pipes require you to perform polling. With files you'd need to open and reread the file and with the database you'd need to perform queries to check it for new rows. With a named pipe, the application can just open it and wait for data.

Read more...

Debugging PHP

We've all had problems with our PHP scripts. PHP can be a pain to debug at times and especially on servers that haven't been configured with PHP development in mind.

PHP doesn't always show errors, which makes finding the cause of an error problematic if you don't know what to do. PHP's functions for displaying variable info and debug info aren't very good etc.

Let's look at some good ways to make PHP help you find the problems, including how to configure it to display all errors and a free PHP extension called Xdebug that is a tremendous help in debugging.

Read more...

Running both PHP4 and PHP5

As I've been working as a freelance PHP programmer for a while now, I've already sometimes encountered clients without PHP5 support on their servers.

This sometimes causes small problems when I've used PHP5 syntax or such.

So it would be useful to have both PHP4 and PHP5 on your server so you can switch PHP4 when needed.


The solution is to run PHP4 as a CGI-script. Later in this post, I'll tell you how to enable PHP4 as a CGI script for Debian.

Read more...

Design patterns: The Factory

, ,

In my post about the problems in Zend Framework implementation of the MVC pattern I briefly mentioned the factory design pattern and that it could be used to create a view factory.

In this post I'll explain the concept of the factory a bit further and provide an example view factory implementation for you. We also brush the singleton pattern a bit.

Read more...

AJAX in ASP.NET

, ,

Today I did some research and looked up how you do AJAX in ASP.NET 2.0.

Microsoft has released an AJAX extension for ASP.NET 2.0 which makes doing AJAX enabled pages very very easy. Let's compare the way you'd do AJAX in PHP to the way you use in ASP.NET.

Read more...