Zend Router URL array merge - zend-framework

I have a problem with custom zend router.
this is my cat router
$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
$router->addRoute('categories', new Zend_Controller_Router_Route(
'video/k/:id/:title',array(
'controller' => 'video',
'module' => 'default' ,
'action' => 'k',
'id' => '',
'title' =>''
)
));
$params = Zend_Controller_Front::getInstance()->getRequest()->getParams();
when i try htttp://dev.dummy.com/video/k/1/foo works fine but
$this->url(array_merge($params, array('order' => 'title'))) or
$this->url(array_merge($params, array('order' => 'title')),'categories')
$this->url(array_merge($params, array('order' => 'title')),'categories', true)
doesnt return htttp://dev.dummy.com/video/k/1/foo/order/title
still returning htttp://dev.dummy.com/video/k/1/foo.
Hope this help.
Thanks.

Try setting the third parameter $reset of the url-helper to true:
$this->url(array_merge($params, array('order' => 'title')), 'categories', true)
This will reset the default parameters.
(Documentation at http://framework.zend.com/manual/1.12/en/zend.view.helpers.html#zend.view.helpers.initial)

When doing the array_merge, the integer keys will be reset to start from beginning. Just append your array instead of the array_merge:
$router->addRoute('categories', new Zend_Controller_Router_Route(
'index/test/:id/:title',array(
'controller' => 'index',
'module' => 'default' ,
'action' => 'test',
'id' => '',
'title' => ''
)
));
echo $this->url(array('order' => 'title'));
Tried it with http://dev.dummy.com/video/k/1/foo and http://dev.dummy.chom/video/k/1/foo/order/title/. In both cases, the url helper output was - htttp://dev.dummy.com/video/k/1/foo/order/title

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_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).

problem with Zend URL helper $this->url

I have situation with Zend route & $this->url method
IN my bootstrap.php I have few routes as
$route = new Zend_Controller_Router_Route(
'dashboard',
array(
'action' => 'index',
'controller' => 'index',
'module' => 'dashboard',
'isAdmin' => true
)
);
$router->addRoute('dashboard', $route);
$route = new Zend_Controller_Router_Route(
'logout',
array(
'action' => 'logout',
'controller' => 'index',
'module' => 'main',
'isAdmin' => true
)
);
$router->addRoute('logout', $route);
$route = new Zend_Controller_Router_Route(
'manage-users',
array(
'action' => 'list',
'controller' => 'index',
'module' => 'main',
'isAdmin' => true
)
);
$router->addRoute('manage-users', $route);
$route = new Zend_Controller_Router_Route(
'edit-user/:id',
array(
'action' => 'edit',
'controller' => 'index',
'module' => 'main',
),
array('id' => '[0-9]+')
);
$router->addRoute('edit-user', $route);
$route = new Zend_Controller_Router_Route(
'/manage-subcat/:ident',
array(
'action' => 'index',
'controller' => 'subcategory',
'module' => 'category',
'ident' => '',
array(
'ident' => '[a-zA-Z-_0-9]+',
)
)
);
$router->addRoute('manage-subcat', $route);
take a case of last route
in my view when I write
<?php echo $cat->CategoryName ?>
I get url as http://127.0.0.10/manage-subcat
& when I disable the last route in bootstrap & then I write in my view file
<?php echo $cat->CategoryName ?>
I get Url as same http://127.0.0.10/category/subcategory
Ideally I should get http://127.0.0.10/category/subcategory/ident/some-category for this one
& for previous one it should be http://127.0.0.10/manage-subcat/ident/some-category
This code sample is not working with custom routes as well as traditional routes, and I am trying to determine why this sample is not working correctly.
Besides the route declaration formatting error noted by zerocrates, could it be simply a typo in your code?
I notice you use $cat->CategoryName (singular $cat) in one place and $cats->catident (plural $cats) in another.
If passed a null parameter value, the router will omit that parameter from the generated URL.

Problem with Zend and addRoute()

With this code:
$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
$router->addRoute(
'test',
new Zend_Controller_Router_Route(
'/test/:action/:type/:id',
array(
'controller' => 'admin'
)
)
);
http://app/test/param1/param2/param3 -> OK
http://app/test/param1/param2/ -> FAIL
In the second case the application don't recognize param2.
It seems that the application needs the param3 in order to read the param2...
How can I do it?
Thanks!
Test with the code from #RageZ
$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
$router->addRoute(
'test',
new Zend_Controller_Router_Route(
'/test/:action/:type/:id',
array(
'controller' => 'admin',
'id' => 0
),
array(
'id' => '\d+'
)
)
);
http://app/test/ -> OK
http://app/test/some -> OK
http://app/test/some/more -> FAIL
http://app/test/some/more/andmore -> OK
Ideas?
try giving a default value to everything
$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
$router->addRoute(
'test',
new Zend_Controller_Router_Route(
'/test/:action/:type/:id',
array(
'controller' => 'admin',
'action' => 'index',
'type' => 'sometype',
'id' => 0
),
array(
'id' => '\d+'
)
)
);
Just tried your code in some test project and this fixed it hope it works for you!
You have to provide a default value if the parameter is optional.
$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
$router->addRoute(
'test',
new Zend_Controller_Router_Route(
'/test/:action/:type/:id',
array(
'controller' => 'admin',
'id' => 0
),
array(
'id' => '\d+'
)
)
);
Nothing to do with your question but it's a good practice to use the third param of addRoute. Zend Framework would verify that the parameters value match the format you have specified, in that case I suppose id is an integer.

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}',
)
);