09
September
2008
The quest of routing in Zend Framework
I am a ZF beginner and I am trying to find out a way to handle routing.
Let say that I am building a website with different sections:
http://example.com/news
http://example.com/articles
http://example.com/gallery
http://example.com/forum
I am wondering how should I set up routing? Take the forum as an example.
Let say I use this principle:
http://example.com/module/controller/action?var1=value&var2=value
And apply the principle to the forums function addpost:
"http://example.com/forum/addpost" should be routed to Forum module, viewthread Action, indexAction() method
The indexAction method displays a form where you can write your post. The forms action is pointing to http://example.com/forum/addpost/add
which is represented by the addAction() method in the same controller.
By choosing module/controller/action route pattern, each controller manages a complete "forum function" like:
- Adding a post (writing, preview, adding post)
- Editing a post (writing, preview, update post)
- Reading a post (display)
This looks like a good solution for me? But is it working?
And how can you handle a form which has several submit buttons in the above context? I must point the forms action to a *Action method in the Controller and inside this method I must handle which button the user has pressed and do a second "dispatch" on my own? I would like to have organized code, with one method per "subfunction". Has ZF built in functions for this?
/Erik
07
September
2008
Staplande steg med Zend Framework
Har i dagarna fått lite smak på att koda lite. Har köpt en bok, PHP|architect’s guide to Programming with Zend Framework. Mycket intressant läsning som gör mig grymt nyfiken. Dock verkar tröskeln vara hög för att kunna använda Framworket eftersom i alla fall jag vill veta hur det funkar i detalj.
Håller just nu på förstå hur routing sker. Zend Framework bygger på principen att alla request hamnar i en enda php-fil, mha mod rewrite om man kör apache. Zend Framework använder en sk FrontController för att expedera (dispatch) alla anrop till individuella sk Action Controllers.
I denna process ingår routing och det är den som jag just nu studerar. Vill luska ut hur Zend Framwork routar följande exempel:
http://examplecom/forum/viewboard/board/ot
Detta enligt principen http://example.com/controller/action/var/value
Ovanstående är standard routing-inställningar i Zend Framework och skulle i så fall routa anropet till classen Forum och metoden viewboardAction.
Dock inser jag att klassen Forum skulle bli gigantiskt stor eftersom den skulle behöva innehåll alla Action-metoder som ett forum kräver. Det blir många, vilket betyder att man måste göra refactoring på något bättre sätt. Hur detta skall göras har jag inte ännu lyckats klurat ut.
/Erik

