Custom route with parameters - zend-framework

I want to make customer route for url which have parameters.
<a href='javascript:void(0)' onclick="window.location='<?php echo
$this->url(array('module' => 'courses', 'controller' => 'course', 'action' => 'add',
'std_id' => $entry['std_id']), 'coursesadd', TRUE); ?>'">add course</a>
and here what I make routing
$route = new Zend_Controller_Router_Route('courses/std_id/:std_id',
array('module' => 'courses', 'controller' => 'course', 'action' => 'add'));
$routesArray = array('coursesadd' => $route );
$router->addRoutes($routesArray);
But It doesn't route correctly!

Your code looks fine, except what RockyFord sad - You should only pass these parameters to url() helper which are required by designated route (when 'default' route is used indeed it requires module, controller, action, but Your 'coursesadd' route requires only std_id parameter). Moreover You should improve Your route a little bit to:
$route = new Zend_Controller_Router_Route('courses/:std_id', array(
'module' => 'main',
'controller' => 'product',
'action' => 'add',
'std_id' => null //default value if param is not passed
));
The param_name/:param_value construction in Your route is redundant because You already named the parameter, so by using above route You'll get std_id param in controller using
$this->_getParam('std_id')
Default value in route definition would save from throwing Zend_Controller_Router_Exception: std_id is not specified but it looks like $entry['std_id'] is not set.

Related

zend framework 1.11.11 routing

http://xxxxxx.xxx/man/pant/man-yellow-box-pant
$router = $ctrl->getRouter(); // returns a rewrite router by default
$router->addRoute(
'location',
new Zend_Controller_Router_Route(
':category/:subcategory/:productname',
array(
'module' => 'default',
'controller' => 'product',
'action' => 'index',
'id' => 'id',
'name' => 'p_name'
)
)
);
above this code is not working..
$routers = $ctrl->getRouter(); // returns a rewrite router by default
$routers->addRoute(
'location',
new Zend_Controller_Router_Route(
':uname',
array(
'module' => 'default',
'controller' => 'user',
'action' => 'index',
'id' => 'uid',
'name' => 'u_name'
)
)
);
above is working...........
please suggest me why not working when both code write on same page, same project..
Your routes need to have a unique name. By calling them both 'location', the second one you add replaces the first.
I know writing mistake, if we change location to other then also not working because i have more URL condition and condition is we not use on module/controller/function name on URL, according to category or function we use to name On URL.
I have 4 module and 15 controller with as per more function and need to all URL make to SEO friendly so i did, if i change to location with username, he not working,and
$routers = $ctrl->getRouter(); // returns a rewrite router by default
$routers->addRoute('category',
new Zend_Controller_Router_Route(
':category/:subcategory/:productname',
array(
'module' => 'default',
'controller' => 'product',
'action' => 'index',
'id' => 'id',
'name' => 'p_name')));
Category and Subcategory system is not working.
how to set right path way as per my condition and not through 404 error
URL get Like http://testing.com/man/pant/man-yellow-box-pant

Zend routes two routes need same count of params

I don´t know what I do wrong. I got two named Zend route:
$route = new Zend_Controller_Router_Route(
'catalog/:categoryIdent/:productIdent/',
array(
'action' => 'viewproduct',
'controller' => 'catalog',
'module' => 'eshop',
'categoryIdent' => '',
'productIdent' => ''
),
array(
'categoryIdent' => '[a-zA-Z-_0-9]+',
'productIdent' => '[a-zA-Z-_0-9]+'
)
);
$router->addRoute('catalog_category_product', $route);
// catalog category route
$route = new Zend_Controller_Router_Route(
'catalog/:categoryIdent/:page/',
array(
'action' => 'viewcategory',
'controller' => 'category',
'module' => 'eshop',
'categoryIdent' => '',
'page' => ''
),
array(
'categoryIdent' => '[a-zA-Z-_0-9]+'
)
);
$router->addRoute('catalog_category', $route);
When I call catalog_category its all fine but when I try call to catalog_category_product is used viewcategory action from second route. It means its problem with :page variable in url, resp. same count of arguments in URL? I think that itsn´t nessesary I would like to get two different but similar routes - for example:
For category - catalog/category1/1
For product - catalog/category1/product1 (without number of page)
when I change form of route catalog_category_product to catalog/:categoryIdent/something/:productIdent/ so its working
here is route calls
$this->url(array('categoryIdent' => categoryIdent, 'productIdent' => productIdent), 'catalog_category_product', true);
$this->url(array('categoryIdent' => 'cerveny-cedr', 'page' => 'pageNumber'), 'catalog_category', true);
Thanks for any help
Keep in mind that routes are checked in reverse order, so the ZF router will check the catalog_category route before the catalog_category_product route when matching URLs. So, the number of arguments is not a problem, but since you've not put any sort of restriction on the 'page' parameter, all URLs that would normally match your catalog_category_product URL will be matched by catalog_category instead.
It sounds like 'page' should be numeric, so adding that restriction to your second route should fix the problem.

ZF Frontend and backend routing

I want something that sounds fairly simple to me but appears not to be.
My problem is that I need 2 routes voor my application:
Whenever the module is admin apply the following route:
$router->addRoute(
'backend',
new Zend_Controller_Router_Route('/:module/:controller/:action/:id/:value', array('module' => 'admin', 'controller' => 'dashboard', 'action' => 'index', 'id' => ':id', 'value' => ':value'))
);
Which works great. An example url could be: http://localhost/server/domains/demo/admin/images/album/3 where admin is the module, images the controller and so on.
All I want is that when a user goes to http://localhost/server/domains/demo he is redirected to the default module, index controller and index action. Everything after demo/ should be considered a single parameter (with unknown / possible).
I tried several things, from using Route_Regex, trying (.*) or (\d+), things I found all around online. Tried switching values, making them static, turning on/off removeDefaultRouter, but nothing worked. Below you can see my current bootstrap. Any ideas?
$router = $this->frontController->getRouter();
$router->removeDefaultRoutes();
$router->addRoute(
'backend',
new Zend_Controller_Router_Route('/admin/:controller/:action/:id/:value', array('module' => 'admin', 'controller' => 'dashboard', 'action' => 'index', 'id' => ':id', 'value' => ':value'))
);
$router->addRoute(
'frontend',
new Zend_Controller_Router_Route('/default/:controller/:action/(.*)', array('module' => 'default', 'controller' => 'index', 'action' => 'index'))
);
Backend works fine, but whenon the http://localhost/server/domains/demo/ I get the following error: No route matched the request
When given an answer, please explain why, because Zend_Route has always been a little vague for me. Thanks in advance!
Temp fix
Below the temporary fix that I use. It works exactly how I want, but I still believe that the same is achievable with Zend_Route without checking if the module is admin.
$router = $this->frontController->getRouter();
$uri = explode('demo/', $_SERVER['REQUEST_URI']);
$uri = (isset($uri[1])) ? explode('/', $uri[1]) : $uri[0];
if($uri[0] == 'admin')
{
$route = new Zend_Controller_Router_Route('/:module/:controller/:action/:id/:value', array('module' => 'admin', 'controller' => 'dashboard', 'action' => 'index', 'id' => null, 'value' => null));
$router->addRoute('router', $route);
}
else
{
$route = new Zend_Controller_Router_Route('/*', array('module' => 'default', 'controller' => 'index', 'action' => 'index'));
$router->addRoute('router', $route);
}
It is worth doing if you really have to use this routes. For normal usage you can just generate links by
Zend_Layout::getMvcInstance()->getView()->Url(array('module' => 'admin'));
Zend_Layout::getMvcInstance()->getView()->Url(array('module' => 'default'));
and so on.
If you are not ok with this, try routes chaining:
$router = Zend_Controller_Front::getInstance()->getRouter();
$dispatcher = Zend_Controller_Front::getInstance()->getDispatcher();
$request = Zend_Controller_Front::getInstance()->getRequest();
$frontRoute = new Zend_Controller_Router_Route_Module(array(), $dispatcher, $request);
$backRoute = new Zend_Controller_Router_Route_Module(array('module' => 'admin'), $dispatcher, $request);
$route = new Zend_Controller_Router_Route('admin');
$router->addRoute('backend', $route->chain($backRoute));
$route = new Zend_Controller_Router_Route('default');
$router->addRoute('frontend', $route->chain($frontRoute));
Explenation:
Zend_Controller_Router_Route_Module is for definining modules routes for the application. By chaining it to normal route like new Zend_Controller_Router_Route('admin') you made links like /admin/[your_module_route] where [your_module_route] have defined defaults array('module' => 'admin') and can take other parameters too.
new Zend_Controller_Router_Route_Module(array(), $dispatcher, $request); is defined as an default route on the standard router.

Zend Controller Router : Define variables to point to a different action in one controller

I'm just new with Zend and I have a little trouble with Zend Routers. I've searched about it, but nothing found...
I want to be able to define a router for each defined variable at uri level to point to a different action in one controller.
I'm working with lang and modules so I defined at bootstrap application the next initRoutes function:
protected function _initRoutes()
{
$front = Zend_Controller_Front::getInstance();
$router = $front->getRouter();
$defaultRoute = new Zend_Controller_Router_Route(
':lang/:module/:controller/:action',
array(
'lang' => 'es',
'module' => 'default',
'controller' => 'index',
'action' => 'index'
),
array(
'lang' => '^(en|es)$',
'module' => '^(default|admin)$'
)
);
$router->addRoute('defaultRoute', $defaultRoute);
return $router;
}
I want to be able to access forum sections and forum topics by their defined action.
Something like :
mydomain/forum -> forum/index
mydomain/forum/section -> forum/sectionAction
mydomain/forum/section/topic -> forum/topicAction
and also with the lang and module defined at uri level like :
mydomain/lang/module/forum
mydomain/lang/module/forum/section
mydomain/lang/module/forum/section/topic
So I have this :
class ForumController extends Zend_Controller_Action
{
public function indexAction()
{
}
public function sectionAction()
{
}
public function topicAction()
{
}
Then I created the next routes inside the Default_Bootstrap :
$forumRoutes = new Zend_Controller_Router_Route(
':lang/:module/forum',
array(
'lang' => 'es',
'module' => 'default',
'controller' => 'forum',
'action' => 'index'
)
);
$sectionRoutes = new Zend_Controller_Router_Route(
':lang/:module/forum/:section',
array(
'lang' => 'es',
'module' => 'default',
'controller' => 'forum',
'action' => 'section',
'section' => ''
)
);
$topic = new Zend_Controller_Router_Route(
':lang/:module/forum/:section/:topic',
array(
'lang' => 'es',
'module' => 'default',
'controller' => 'forum',
'action' => 'topic',
'section' => '',
'topic' => ''
)
);
$router->addRoute('forumTopics', $topic);
$router->addRoute('forumSections', $section);
$router->addRoute('forum', $forumRoutes);
Now, this only works if I define the lang and module at uri level, but doesn't work if I defined like => mydomain/forum/section | section/topic. This also brings me another problem with my navigation->menu. If I define "forum" as a static variable at router definition, when I hover over at any label defined at navigatoin.xml, the uri level have the same value for every one of them.
I've tried to make a chain like this:
$forumRoutes = new Zend_Controller_Router_Route(
':lang/:module/forum',
array(
'lang' => 'es',
'module' => 'default',
'controller' => 'forum',
'action' => 'index'
)
);
$section = new Zend_Controller_Router_Route(
':section',
array(
'action' => 'section',
'section' => ''
)
)
$topic = new Zend_Controller_Router_Route(
':topic',
array(
'action' => 'topic',
'topic' => ''
)
)
$chainedRoute = new Zend_Controller_Router_Route_Chain();
$chainedRoute->chain($topic)
->chain($section)
->chain($forumRoutes);
$router->addRoute($chainedRoute);
But this doesn't work as I expected.
Any help would be appreciated, thanks.
You are new to Zend. You said it. So here are some explanations:
Ideally the URL on Zend application is:
example.com/controller/action/param-name/:param-value
So in which case, if there is an action called Edit under UsersController, it will be:
example.com/users/edit
if action is add, it will be :
example.com/users/add
when you specify first parameter as a variable, it will collide with controller requests. Example: if you say controller is User but first parameter accepts a value and puts it in emplyees then a request as example.com/employees and example.com/user will both point towards employees controller even if the usercontroller exists! which again is a theory!
What you might want to do is, leave the routes to only accept dynamic values rather routing! You do not want users to route your application but, route user to different sections of the application.
About language then you need to use Zend_Locale which will check for HTML language that is
<html lang="nl"> or <html lang = "en">
Hope things are clear! :)
Here's a quick example which should help you work with routes like that in ZF.
If you are using a default project structure like the one you get when starting a new project using Zend Tool go into the Application folder.
In your Bootstrap.php set up something like this:
protected function _initRouteBind() {
// get the front controller and get the router
$front = Zend_Controller_Front::getInstance();
$router = $front->getRouter();
// add each custom route like this giving them a descriptive name
$router->addRoute(
'addTheDescriptiveRouteNameHere',
new Zend_Controller_Router_Route(
'/:controller/:id/:action/:somevar',
array(
'module' => 'default',
'controller' => ':controller',
'action' => ':action',
'id' => ':id',
'somevar' => ':somevar'
)
)
);
}
My example above is just to illustrate how you would use a route where the controller, the action and a couple of parameters are set in the url.
The controller and action should be resolved without any additional work however to get the 'id' or 'somevar' value you can do this in your controller:
public function yourAction()
{
// How you get the parameters to pass in to a function
$id = $this->getRequest()->getParam('id');
$somevar = $this->getRequest()->getParam('somevar');
// Using the parameters
$dataYouWant = $this->yourAmazingMethod($id);
$somethingElse = $this->yourOtherAmazingMethod($somevar);
// Assign results to the view
$this->view->data = $dataYouWant;
$this->view->something = $somethingElse;
}
So while you will want to make sure your methods handle the parameters being passed in with care (after all it is user supplied info) this is the principle behind making use of the route parameter binding. You can of course do things like '/site' as the route and have it direct to a CMS module or controller, then another for '/site/:id' where 'id' is a page identifier.
Also just to say a nice alternative to:
$id = $this->getRequest()->getParam('id');
$somevar = $this->getRequest()->getParam('somevar');
which wasn't that nice itself anyway since it was assuming a parameter was going to be passed, using shorthand conditional statement in conjunction with action helpers is:
$id = ($this->_hasParam('id')) ? $this->_getParam('id') : null;
$somevar = ($this->_hasParam('somevar')) ? $this->_getParam('somevar') : null;
If you are not familiar with this, the
($this->_hasParam('id'))
is the conditional test which if true assigns the value of whatever is on the left of the ':' and which if false assigns the value of whatever is on the right of the ':'.
So if true the value assigned would be
$this->_getParam('id')
and null if false.
Does that help? :-D

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.