Zend Framework custom URL - zend-framework

I make a project with Zend, i'm new. And this is my idea:
http://mydomain.com/username
Will show content same as:
--> http://mydomain.com/profile/index/u/username
and http://mydomain.com/username/gallery
Will show content same as:
--> http://mydomain.com/profile/gallery/u/username
( profile controller )
Can Zend does something like this?
Please help. Thanks so much.
And right now, I'm learning Zend 1.1.1 as primaty download on their website, but I 'm seeing Zend 2.x is Beta. Should I still learn Zend 1.x ? or waiting Zend 2.x come out.

Use .htaccess rewrites for this.
or add another route that executes the same controller action:
$router->addRoute(
'profile_2',
new Zend_Controller_Router_Route(':username', array(
'controller' => 'profile',
'action' => 'showUser'
))
);

U can use this piece of magic route ;)
$router = Zend_Front_Controller::getInstance()->getRouter();
$router->addRoute(
'user',
new Zend_Controller_Router_Route(':u/:action', array(
'controller' => 'profile',
'action' => 'index',
'u' => null
))
);

Related

Zend Framework Routing Technique

I would like to know if this method of setting up routing in Zend Framework would be considered a reasonable approach - I am quite new to using ZF.
In my bootstrap file I have a method set up like this:
protected function _initRouting()
{
$zfc = Zend_Controller_Front::getInstance();
$router = $zfc->getRouter();
$router->addRoute(
'home',
new Zend_Controller_Router_Route(
'',
array(
'controller' => 'index',
'action' => 'index'
)
)
);
$router->addRoute(
'saveStory',
new Zend_Controller_Router_Route(
'save-story/:date/:seolink/:saveStory',
array(
'controller' => 'story',
'action' => 'index',
'saveStory' => 1
)
)
);
etc etc with all my routes.
This works fine and I like the clarity of specifying routes like this but have a nagging feeling that more experienced ZF programmers would tell me that it is not the best way and that I should not do it in the bootstrap and that I should specify the routes in a seperate config file. If this is the case what form would the config file take and how and where would I read it.
Any advice would be appreciated....Because my technique works this question is more a stylistic question - what is the most 'elegant' approach.....Thanks
It's just down to personal preference. Like you, I prefer to define my routes in code, but some people prefer config files (and if you want to try this there are several examples in the manual).

Multiple Routes with Standard Router (Zend Framework)

I am trying to set up multiple routes to the same controller in zend as such:
URL | Controller::Action
=================================================================================
http://mysite/tasks/:level/ | Objectives::Objectives
http://mysite/tasks/:level/:objective/ | Objectives::tasks
http://mysite/tasks/:level/:objective/:taskID/ | Objectives::view
I've tried the following:
<?php
$router->addRoute('objectives', new Zend_Controller_Router_Route(
'task/:level/:objective/:taskID/',
array(
'controller' => 'objectives',
'action' => 'view'
)
));
$router->addRoute('objectives', new Zend_Controller_Router_Route(
'task/:level/:objective/',
array(
'controller' => 'objectives',
'action' => 'tasks'
)
));
$router->addRoute('objectives', new Zend_Controller_Router_Route(
'task/:level/',
array(
'controller' => 'objectives',
'action' => 'tasks'
)
));
?>
However the last rule seems to overwrite the previous rules in the router.. I've read the Zend Documentation for the router over and over, I have a feeling im just missing something - should I be using a different router class?
Any help is Much Appreciated
As suspected I was overwriting the previous rules. The first argument for addRoute() is a name for the route, not the controller you are routing to as I thought. Giving each route a unique name fixed the problem.

Pretty Zend Framework urls

I would like to have my urls like this:
/index
/contact
/articles
/articles/selection
...
Instead of:
/index/index
/index/contact
/articles/index
/articles/selection
...
Basically I have only one controller. Which solution is the best to perform this? (controllers and redirections, ZF routing, url rewriting, something else?)
Have a look at the documentation. The behaviour you want is configured as default in the default router:
http://framework.zend.com/manual/en/zend.controller.router.html
if the first param do not maps a module name, it will search for a controller and if this fails too, it is looking for an action in your IndexController.
Did you tried calling your url's like you want to?
What happens if you navigate to /index? Should be the same like /index/index
use zend routing :
$router = Zend_Controller_Front::getInstance()->getRouter();
$route_index = new Zend_Controller_Router_Route(':action', array(
'module' => 'default',
'controller' => 'index',
'action' => 'index'
));
$router->addRoute('route_index', $route_index );
$route_articles = new Zend_Controller_Router_Route('articles/:action', array(
'module' => 'default',
'controller' => 'articles',
'action' => 'index'
));
$router->addRoute('route_articles ', $route_articles );

Zend View: Domain to Action

I am looking for a method in the bootstrap that looks at the URL then loads a specific module/controller/action.
However I don't want the user to see it.
Is this possible and easy?
I'm unsure wether you are looking for Hostname Routing, or just normal Routing, so I will answer both.
Zend_Controller_Router_Route
ie domain.com/users/layke
Using Zend_Controller_Route you can create a route will will grab "layke" and use that as a parameter. This would be a a standard Route.
$route = new Zend_Controller_Router_Route(
'users/:username',
array(
'controller' => 'profile',
'action' => 'users'
)
);
$router->addRoute('user', $route);
Then also there is hostname matching....
Zend_Controller_Router_Route_Hostname
For instance, (as the guides provide)...
You could use..
:username.domain.com to map to
/default/users/:username.
Zend_Controller_Router
$hostnameRoute = new Zend_Controller_Router_Route_Hostname(
':username.example.com',
array(
'controller' => 'profile',
'action' => 'users'
)
);
$plainPathRoute = new Zend_Controller_Router_Route_Static('');
$router->addRoute('user', $hostnameRoute->chain($plainPathRoute));
Examples Documentation : Zend_Controller_Router
Sounds like you're after hostname routes: http://framework.zend.com/manual/en/zend.controller.router.html#zend.controller.router.routes.hostname
You can use Routes in Zend (http://framework.zend.com/manual/en/zend.controller.router.html) and then mod_rewrite + .htacess for apache (http://httpd.apache.org/docs/current/mod/mod_rewrite.html) or the URL rewrite module in IIS (http://www.iis.net/download/URLRewrite)
These functions are "needed" for using Zend at all, as the URL mysite.com/demo1/demo2/ actually translates to something like mysite.com/index.php?controller=demo1?action=demo2

Zend Framework Change parameter in route on the same spot?

I'm not sure how to fix this, or wat is the best way to approach this. Also couldn't find enough information to get me on the right way (could be that my searching sucks..)
Anyway, this is my problem:
I defined a route in my bootstrap file:
protected function _initRoutes()
{
$router = $this->frontController->getRouter();
$router->removeDefaultRoutes();
$router->addRoute(
'delete',
new Zend_Controller_Router_Route('/:controller/:action/:id/',
array('controller' => ':controller',
'action' => ':action',
'id' => ':id',
)
)
);
}
This works perfectly for my update and delete actions.
Now I've added the pagination to the indexpage. The pagination expects the page parameter. Because I haven't set this in my route, it cannot pass it, so my pagination doesn't work (as in switching between results).
I understand this. But what I want is that on the index page the id parameter isn't necessary and replace this with the page parameter.
Trying another route replacing id with page didn't work.
Is there a good way to solve this in the bootstrap or is it the best way to check for the action, and depending on the action, index or update/delete, define the route. The best place would than be a plugin?
Any advice or tips are greatly appreciated!
While working on another aspect of the application I came back to the same problem. I solved it, by specifying the routes much more.
First I deleted the $router->removeDefaultRoutes(); rule.
And then instead of (which didn't work):
$router->addRoute(
'crud',
new Zend_Controller_Router_Route('/:controller/:action/:id', array('controller' => ':controller', 'action' => ':action', 'id' => ':id'))
);
$router->addRoute(
'pagination',
new Zend_Controller_Router_Route('/:controller/:action/:page', array('controller' => ':controller', 'action' => ':action', 'page' => ':page'))
);
I now use this:
$router->addRoute(
'crud',
new Zend_Controller_Router_Route('/:controller/:action/:id', array('controller' => ':controller', 'action' => ':action', 'id' => ':id'))
);
$router->addRoute(
'pagination',
new Zend_Controller_Router_Route('/:controller/index/:page', array('controller' => ':controller', 'action' => 'index', 'page' => ':page'))
);