How to get the variables in routing in zend framework 1.12 - zend-framework

I am new to routing in zf. I don't understand some terms in route
$route = new Zend_Controller_Router_Route(
'author/:username',
array(
'controller' => 'profile',
'action' => 'userinfo'
)
);
$router->addRoute('user', $route);
How do we get :username here? from where do we get this?

Username here is parameter pass by the user. So correct link using this route will be http://somepage.com/author/John. In your controller you can get this variable just like POST and GET variables - $this->getParam('author');
If you want to allow user use link without parameter (default parameter) you can add to array - 'author' => null (i usually use this in pagination)

Related

Stopping zend route default paramaters 'key' displaying in the URL

I have a route defined as below.
$route['manage-vehicles'] = new Zend_Controller_Router_Route(
'vehicles/manage/page/:page',
array(
'controller' => 'vehicles',
'action' => 'manage',
'page' => '1'
)
);
When the 'page' parameter is not specifically defined (e.g. in a menu constructed using the navigation component), the resultant URL is
/vehicles/manage/page
I would much prefer or the URL not to to display the default paramater key in this scenario
i.e. /vehicles/manage
Any ideas how to accomplish this would be appreciated?
Thanks.
EDIT: For clarity, I would like vehicles/manage/page/1 etc to display when the 'page' parameter is defined
The 'page' string you have in your route is not required for the page parameter to work, so all you need to do is change your route to:
$route['manage-vehicles'] = new Zend_Controller_Router_Route(
'vehicles/manage/:page',
array(
'controller' => 'vehicles',
'action' => 'manage',
'page' => '1'
)
);
you've told it which thing in the URL is 'page', so you don't need the prefix there. That's just part of the default route where the parameters are not predefined.

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

What is Route, where and how to define it?

I have several modules in my zend application. On one of the view script of my modules, I created a URL as such
$links['create'] = $this -> url(array("controller" => "roles", "action" => "create"), "custom");
This brings an error, saying Route "custom" is not define.
What is Route? Where to define it and How?
In my bootstrap file i have initialized my routing by adding following function
public function _initRouting() {
// Get Front Controller Instance
$front = Zend_Controller_Front::getInstance();
// Get Router
$router = $front->getRouter();
$routedetialevent = new Zend_Controller_Router_Route(
'/events/detail/:id',
array(
'controller' => 'events',
'action' => 'detail'
)
);
$routeregister = new Zend_Controller_Router_Route(
'/index/register/:id',
array(
'controller' => 'index',
'action' => 'register'
)
);
$routerdetail = new Zend_Controller_Router_Route(
'/commentaries/details/:id',
array(
'controller' => 'commentaries',
'action' => 'details'
)
);
$router->addRoute('post', $routedetialevent);
$router->addRoute('register', $routeregister);
$router->addRoute('detail', $routerdetail);
}
as i have added the custom route in my events, commentaries whenever i visit detail page i dont have to write id in my url so my url will be like
http://localhost/example/events/detail/3
If i wouldnt have added route than my url would be like
http://localhost/example/events/detail/id/3
The Zend Framework manual has pretty decent documentation about routes and the router, including descriptions of several ways to define routes.
At a very basic level, routes are used both to parse URLs into parameters (like which controller and action should be used), and to do the reverse: take parameters and produce a URL.
For your purposes, unless you want to change how ZF will build your URL, you can just drop the "custom" part off of your url call.

Zend Framework routing

I'm trying to setup some routes for my ZF app but not getting too far. I have a controller 'WebServiceController', it has an index action and a lookupTransaction action. I want to use routes like this:
ws/
ws/lookupTransaction
Ideally I'd like anything with a 'ws/' prefix to go to the WebServiceController and match the action name. I'm not sure how to do that yet but I am trying to get each route working so I added these two routes:
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
function _initRoutes()
{
$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
$router->addRoute('ws', new Zend_Controller_Router_Route('ws/', array(
'controller' => 'web-service',
'action' => 'index',
)));
$router->addRoute('ws/lookupTransaction', new Zend_Controller_Router_Route('ws/lookupTransaction', array(
'controller' => 'web-service',
'action' => 'lookup-transaction',
)));
}
}
The first one works as expected but the second one doesn't, I just get 'Application Error'. What am I doing wrong? Just out of interest, if I remove my two routes and try and go to:
web-service/lookup-transaction
I still get the same error!
Solved
Here is how I can make it work with camel cased action name and camel cased URL.
$router->addRoute('ws', new Zend_Controller_Router_Route('ws/:action', array(
'controller' => 'web-service',
'action' => 'index',
)));
$router->addRoute('ws-lookupTransaction', new Zend_Controller_Router_Route('ws/lookupTransaction', array(
'controller' => 'web-service',
'action' => 'lookup-transaction',
)));
Thanks
Ziad
Try just this one route as a solution to both problems:
$router->addRoute('ws', new Zend_Controller_Router_Route('ws/:action', array(
'controller' => 'web-service',
'action' => 'index',
)));
the action parameter then serves as a default, so if no action is specified in the URL, index will be used. Otherwise it will route to the action in the URL. So example.com/ws/lookupTransaction will go to lookuptransactionAction() in your controller.
If this still gives you an error, post the error message so we can see what the problem is.
The router actually transforms URLs to lowercase. So the correct URL should be all lowercase dash separated words. Also I'm not sure if it's possible to use slash in route name (the first parameter of addRoute()).

Zend Framework Router Getting /module/VALUE/controller/action

I've been googling around and I can't seem to find anything which explains the use of ZF router well. I've read the documentation on the site, which seems to only talk about re-routing.
I am trying to make the format:
/module/value/controller/action give /module/controller/action passing on value as a parameter
e.g.
/store/johnsmithbigsale/home/newstuff would route to /store/home/newstuff passing on johnsmithbigsale as the value to a parameter with a hidden namespace e.g. storeName.
Some help would be greatful!
You can use Zend_Controller_Router_Route to map your url parts to modules, controllers, actions, and parameters that can be used in the controller by $this->_getParam('varName'). You can define these routes in the application.ini file or in the application bootstrap.
// custom city route
$route = new Zend_Controller_Router_Route(
'cities/:city',
array(
'controller' => 'city',
'action' => 'view'
)
);
$this->addRoute('city', $route);
// custom buy widgets route
$route = new Zend_Controller_Router_Route_Regex(
'buy_(.+)_widgets/([0-9]+)(.*)',
array(
'controller' => 'widgets',
'action' => 'view'
),
array(
1 => 'nothing',
2 => 'widget_id',
3 => 'vars'
)
);
$this->addRoute('widgets', $route);
The regex route is kind of specific to my app, but you can see that each match can get mapped to a parameter.