Zend Route Overwriting each other - zend-framework

Edit, Slight problem caused by the fix in the respond below:
Now these rules clash:
$router->addRoute('view-category', new Zend_Controller_Router_Route(':id/category/:page', array('module' => 'default', 'controller' => 'category', 'action' => 'view', 'page' => null)));
$router->addRoute('management/category', new Zend_Controller_Router_Route('management/category/', array('module' => 'management', 'controller' => 'category', 'action' => 'index')));
So basically /management/category/reset gets captured by the view-category rule, even if I switch there order. This never used to be an issue.
Ideally if anything caught /management or /administration it would ignore the :name/category rule. Is it possible to make /management and /administration ignore previous rules and route to its controller action as there are no specific rules otherwise in those areas.
OLD QUESTION:
$router->addRoute('view-category', new Zend_Controller_Router_Route(':id/category', array('module' => 'default', 'controller' => 'category', 'action' => 'view')));
$router->addRoute('view-category-page', new Zend_Controller_Router_Route(':id/category/:page', array('module' => 'default', 'controller' => 'category', 'action' => 'view')));
These rules clash which stops paginator working on the /category-name/category URL.
Is there away to combine them?

Try add default value for "page" param.
$router->addRoute('view-category',
new Zend_Controller_Router_Route(':id/category/:page',
array('module' => 'default',
'controller' => 'category',
'action' => 'view',
'page' => null)
)
);

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

Routing from controller to Custome route

I want to route from controller to custom route
I make a custom route
$reportRoute = new Zend_Controller_Router_Route('blogs/blog_id/:blog_id', array('module' => 'blogs', 'controller' => 'blog', 'action' => 'index','blog_id' =>NULL));
$routesArray = array('blogs' => $reportRoute);
$router->addRoutes($routesArray);
and I want to make rediorection from controller to index page
I make like this, but it doesn't work
$this->_helper->redirector->gotoRoute(array('module' => 'blogs', 'controller' => 'blog', 'action' => 'index', 'blog_id' => $this->blog_id));
you are not passing the name of route as the second argument for gotoRoute method to build url.
So code should be like
$this->_helper->redirector->gotoRoute(array('module' => 'blogs', 'controller' => 'blog', 'action' => 'index', 'blog_id' => $this->blog_id),'blogs')

Zend_Controller_Router_Route dynamic route

Assuming the following router:
$router->addRoute('listOfFacilities',
new Zend_Controller_Router_Route('/:lang/:type/',
array('module' => 'default',
'controller' => 'search',
'action' => 'index',
'lang' => 'de',
'type' => ':type'
)));
Result will be:
http://example/en/Hotels/
lang == en
type == Hotels
It's possible to do in such way that for only one language, for example the german, the result is http://example/Hotels/ and not http://example/de/Hotels/?
Tanks,
From a route point of view, just ditch the lang parameter to just have the "type" one.
$router->addRoute('listOfFacilities',
new Zend_Controller_Router_Route('/:lang/:type/',
array('module' => 'default',
'controller' => 'search',
'action' => 'index',
'lang' => 'de',
'type' => ':type'
)));
$router->addRoute('listOfFacilities_default',
new Zend_Controller_Router_Route('/:type',
array('module' => 'default',
'controller' => 'search',
'action' => 'index',
'type' => ':type'
)));
And from a controller point of view, just look for an empty lang parameter to define the default one (in your case, de).
If your page is already redirecting to /de/Hotels, then in our controller test for this :
if($lang == 'de' && $type == 'Hotels') {
$this->_redirect('/Hotels');
}

Zend_Router_Regex and additional parameter

I have no idea how realize it.
I have this router
$route = new Zend_Controller_Router_Route_Regex(
'category/([-\w]+)(?:/(\d+))?',
array('module' => 'dynamic', 'controller' => 'index', 'action' => 'show-category'),
array(1 => 'category', 2 => 'pageNumber'),
'category/%s/%d'
);
$router->addRoute('dynamic-categories', $route);
Assembled url in view will be /category/news/1 by using $this->url(...) helper. I want that my first page of news will be /category/news. Should I use other route to do it or use router chaining. I'm frustrated. Thank you for help.
You can use Zend_Controller_Router_Route for straight forward mapping:
new Zend_Controller_Router_Route(
'/category/:category/:page',
array(
'module' => 'dynamic',
'controller' => 'index',
'action' => 'show-category',
'category' => 'news',
'page' => 1
)
)
Will match /category/, /category/news/, /category/othercategory or /category/news/4 (examples only of course).

Zend_Router requirements mismatch

I have two routes that match a url with the same apparent pattern, the difference lies in the $actionRoute, this should only be matched if the variable :action on it equals 'myaction'.
If I go to /en/mypage/whatever/myaction it goes as expected through $actionRoute.
If I go to /en/mypage/whatever/blahblah it gets rejected by $actionRoute and matched by $genRoute.
If I go to /en/mypage/whatever it should be matched by $genRoute but it gets matched by $actionRoute instead throwing and exception because the action noactionAction() does not exist.
Don't know what I'm doing wrong, I'd appreciate your help.
$genRoute = new Zend_Controller_Router_Route(
':lang/mypage/:var1/:var2',
array(
'lang' => '',
'module' => 'mymodule',
'controller' => 'index',
'action' => 'index',
'var1' => 'noone',
'var2' => 'no'
),
array(
'var1' => '[a-z\-]+?',
'lang' => '(es|en|fr|de){1}'
)
);
$actionRoute = new Zend_Controller_Router_Route(
':lang/mypage/:var1/:action',
array(
'lang' => '',
'module' => 'mymodule',
'controller' => 'index',
'action' => 'noaction',
'var1' => 'noone',
),
array(
'action' => '(myaction)+?',
'var' => '[a-z\-]+?',
'lang' => '(es|en|fr|de){1}',
)
);
$router->addRoute('genroute',$genRoute);
$router->addRoute('actionroute',$actionRoute);
By providing a default value ('noaction') for action in your $actionRoute, you are making that variable optional. If you remove this it should all work fine. Also the 'var' key in the regexp patterns in your second route should probably be 'var1' as it is in your first.
So, you probably want your second route to be:
$actionRoute = new Zend_Controller_Router_Route(
':lang/mypage/:var1/:action',
array(
'lang' => '',
'module' => 'mymodule',
'controller' => 'index',
'var1' => 'noone',
),
array(
'action' => '(myaction)+?',
'var1' => '[a-z\-]+?',
'lang' => '(es|en|fr|de){1}',
)
);