creating a request filter – allowing for /username

 Development, Zend Framework  Comments Off on creating a request filter – allowing for /username
Aug 302011
 

Often in modern websites, particularly social websites, the urls are in the format of http://somesite.com/username. This presents something of a problem for web apps that follow the MVC pattern and direct all requests through a single front controller that then dispatches the request to the correct controller based on the url or on a route set up in the app. Recently I have begun work on a website, groups.ie and I wanted to implement a url pattern of http://groups.ie/the-group-name . So how so we achieve this when we cannot properly map this using a route as the group name could be almost anything?
I achieve this by using a simple Zend plugin. Here is the code:

/**
* Description of GroupFilter
* allows for urls to follow the format of
* sitename/group-name
*
* @author craigbrookes
*/
class Application_Plugin_GroupFilter extends Zend_Controller_Plugin_Abstract {

public function preDispatch(Zend_Controller_Request_Abstract $request) {
$controller = $request->getControllerName();
$groupMapper = new Application_Model_GroupsMapper();
$group = $groupMapper->findRowByFieldsAndValues(array("url"=>$controller));

if(!$group->isEmpty()){

$request->setControllerName("group");
}
}

}

So in this code, I extend the Zend_Controller_Plugin_Abstract which provides several hook functions (functions which the parent or a controller will call on all plugins at certain times during execution)

The hook functions provided are:

  • routeStartup(): prior to routing the request
  • routeShutdown(): after routing the request
  • dispatchLoopStartup(): prior to entering the dispatch loop
  • preDispatch(): prior to dispatching an individual action
  • postDispatch(): after dispatching an individual action
  • dispatchLoopShutdown(): after completing the dispatch loop

In the above code I choose only to implement the preDispatch() which is past the instance of
Zend_Controller_Request_Abstract. From this request object I get the current controller which my be set to the name of a group. I then use my Mapper class and check if there is a group that responds to the controller name. Finally, if there is a group object found, then I set the controller to the Groups controller which can then take over. Other wise the request continues along its merry way. Got any other solutions or can see potential problems with my solution, please leave a comment and let me know.

For more information on plugins, I found this article to be quite useful.

 

nullI believe we should be doing everything we can to encourage an environment where small startup businesses have every possible chance to get off the ground and go from an idea to an actual business. I think it is vital to any real, lasting economic recovery. These types of businesses will in the future create a more healthy, robust and diversified economy.

There is one area in particular that I see as great way to cultivate a healthy startup environment in Ireland, and that is the area of office space.

It has struck me several times, while travelling around Ireland and around my own home city of Waterford, that there seems to be a whole lot of empty office space. This empty office space, is not newly empty, most, if not all of it, has been largely empty now for the best part of two years or more.

Waterford is not alone in this phenomena, wherever I have travelled in Ireland, these sad, empty shells are present on the landscape of any business park. It is as if they are destined to remain grim memorials of our recent past or perhaps simply as the scars of a well known tiger that turned on its keeper. You don’t have to stray far from the city center to find rows and rows of to let signs lamely hanging in the windows of these buildings; no doubt buildings which have been long forgotten by the appointed letting agent. These offices are not being let, and I would suggest wont be let for the foreseeable future. They are being wasted.

Now add to this mix the high number of unemployed people eager to get to work who, no longer able to find employment, are looking to set up something of their own, perhaps in a field they took interest in only as a hobby etc during the boom years, but now see potential for a business idea. Every effort should be being made to support these people that are working hard to try and start again; the entrepreneurial types who are building home grown, Irish businesses from the ground up.

Well these fledgling businesses need a place to work from. It is all well and good, working from home, but nothing really substitutes for a space in which you can bring people together for a single purpose, with no distractions and the tools with which to achieve your goals.
Also with a buildings full of these energetic, passionate and hard working people the effect will spread and infuse others, who are maybe a little unsure or disheartened, to take the first steps and begin putting together concepts and talking to people about their business ideas.

We do hear rhetoric from the politicians claiming they wish to help small businesses get started or continue to exist, and, while some things are being done to help get small businesses up and running, surely this expanse of empty office space offers a great opportunity to our enterprise boards and the government as a whole?

So here is my proposal: a young business, a startup, of under two years would have the opportunity to apply to the enterprise board for subsidized or even completely free office space. They would apply using a simple, but thought out business plan. The businesses that were successful, would be all offered office space in the same area, and perhaps with other business who had ideas in the same area? It’s that simple.

Landlords would begin renting now empty buildings. Businesses and jobs would begin to be created and the economy as a whole would benefit. Tell me I’m wrong, please!

Now I am aware that you can get grants etc from the enterprise board, but from speaking with people who have received these grants recently, this is a long and difficult process. What I am thinking of is something much more simple. It would be the case that if you could prove you were working hard on something, from home for example, and that your proposed business wasn’t simply a hobby, but actually had merit, then you would get space to work in. It may be shared space, or simply two desks in a room with other budding businesses. As long as there was access to broadband and general office equipment, I am sure it would be fine for most simple startup businesses. It seems amazing to me that this isn’t already being done and I would be delighted if someone told me it was already being done.
And after all, sure, we probably own half the building through NAMA etc anyway.

Abstract Zend Data Mapper Class for Zend Framework

 Development, Zend Framework  Comments Off on Abstract Zend Data Mapper Class for Zend Framework
Aug 232011
 

I have started a new project with a fellow programmer, Peter Fortune @pfortune. The project is a group organising tool, aimed at the Irish market and will be hosted under the domain http://groups.ie.

So when discussing this project, we decided to use Zend Framework as we both are very comfortable with PHP and I have used Zend Framework in the past. After working on the design of the database schema, it came to laying the ground work for the project. Building the models fell under my todo list and so I set to work.

So I spent a little time coming up with what I think is a reasonable Abstract class for the Applications data mapping classes to extend. I know quite a few developers prefer to use doctrine with Zend Framework, and when the project progresses, this may be something we will consider using also, but for the time being, I went with the inbuilt tools Zend_Db_Table_Abstract etc. So here is the class I ended up with. Please give me your thoughts and criticisms and point out any gaping errors.


setDbTable($tableName);
$this->_model = $model;

}

public function setDbTable($dbTable)
{
if (is_string($dbTable)) {
$dbTable = new $dbTable();
}
if (!$dbTable instanceof Zend_Db_Table_Abstract) {
throw new Exception('you must pass a class that extends Zend_Db_Table_Abstract');
}
$this->_dbTable = $dbTable;
return $this;
}

/**
*
* @return Zend_Db_Table_Abstract
* return concrete child
*/
public function getDbTable()
{
if (null === $this->_dbTable) {
throw new Exception("no dbtable set");
}

return $this->_dbTable;
}

/**
*
* @param Application_Model_RowAbstract $row
* @return array
* returns an array of concrete children extending Application_Model_RowAbstract
*/
public function findAllByExample(Application_Model_RowAbstract $row)
{
//returns all public properties and their values in assoc array
$props = get_object_vars($row);
$sql = $this->getDbTable()->select();
foreach($props as $property=>$value){
if(!NULL == $value)
$sql->where(''.$property.' = ?',$value);
}

$rows = $sql->query()->fetchAll();
$ret = array();
foreach($rows as $row){
$ret[]= new $this->_model($row);
}
return $ret;

}

/**
*
* @param Application_Model_RowAbstract $row
* @return Application_Model_RowAbstract
* returns a concrete child
*/
public function findRowByExample(Application_Model_RowAbstract $row){
$props = get_object_vars($row);
$sql = $this->getDbTable()->select();
foreach($props as $property=>$value){
if(! NULL == $value)
$sql->where(''.$property.' = ?',$value);
}

$sql->limit(1);
$rows = $sql->query()->fetchObject($this->_model);
return $rows;
}

/**
*
* @param Application_Model_RowAbstract $row
*
* chooses to save or update based on whether the primary key is set or not
*/
public function saveUpdate(Application_Model_RowAbstract $row)
{
//get pri key from get_primary() public method added
//to concrete implimentation of Zend_Db_Table_Abstract
$prikey = $this->getDbTable()->get_primary();
$prikey = $prikey[1];
$data = get_object_vars($row);
if(isset($row->$prikey)){
//update
$updateData = array();
foreach($data as $property=>$value){
if(!NULL == $value)
$updateData[$property]=$value;
}
print_r($updateData);
//$this->getDbTable()->update($updateData, ''.$prikey.'='.$row->$prikey.'');
}else{
//insert
$this->getDbTable()->insert($data);
}

}

/**
*
* @param mixed $value
* @return Application_Model_RowAbstract
* the value is the value of the primary key set for the row
*/
public function findWherePriKeyEquals($value){

$row = $this->getDbTable()->find($value);
$ret = $row->current();
return new $this->_model($ret->toArray());

}

}

so that’s it. As you can see you have most functionality there. You can create a dummy object and find all that match its values in the database. You can update and save an objects values
and you can find based on simple rules. All of this means that the concrete sub classes have a lot less work to do. Hope this is helpful.

 

The success of a community is vital to the lasting success of any individual within that community. This is something I hold as a core value and I sincerely hope that you do too.

As a product of your community and peers, successful communities will create more successful individuals; and in turn, an individual that holds his own interests and those of his community as being equal, or as close to equal as possible, will create more successful communities.

When I speak of community, I am referring to your locality, the place where you live. Your community is made up of the people in your favourite coffee shop, the teachers educating your children your neighbours and so on. Each member of your community will have an impact, whether directly or indirectly, on your outlook and help you form opinions on society and on how you, as an individual, fit in and should interact with it. They will inform your ideas on how society should be governed, what is acceptable and how it can be improved. So far so obvious, right?

Well the above leads me to a major issue I believe we are facing, and that is the problems caused by the ever expanding scale of our communities brought about by our ability to communicate across vast distances. This problem of scale is affecting every aspect of our lives. In business, the internet is giving companies easy and cheap access to markets that they would have never considered accessible previously. This in turn means that these companies are growing at an unprecedented scale, and their customer bases are immense* this of course erodes the importance of an individual customer. News agencies and charities can bring every tragic disaster to our living rooms, causing us to become increasingly numb to disaster, as an act of self preservation. Your time is important and it is impossible to care about, in a realistic way which motivates us to action, all that is presented to us on a daily basis. But this also must have a trickle down effect on the way in which we perceive those within our own communities which require charity, effect how we run our small to medium businesses and so on.

With such large scale in our everyday lives, it becomes difficult to feel truly connected. We give money to charity but do not see any of the impact which that action had, we help a customer but again see no impact of having solved their problem. Seeing the positive impacts of our actions is essential to continued motivation to perform further actions. To this end, I think it is essential that we find ways to scale down, make things more personal and the effects of good action more visible.

So if we accept, and perhaps you don’t, that we should try to find ways in our lives to try and scale down and make the impact of our own actions more visible, then how can this be achieved?
Well I intend to go into that in my follow up post coming soon.

* read groupon, amazon etc

© 2012 Craig Brookes Suffusion theme by Sayontan Sinha