Zend_Controller_Router_Route_Regex Optional Parameters with Reverse - zend-framework

I've successfully created my Route using Regex. I have Several Optional Parameters in my route that I don't want displayed in the URL Helper unless the user has specified them. How can I accomplish this?
This is what I currently have
$route = new Zend_Controller_Router_Route_Regex(
'([a-zA-Z-_0-9-]+)-Widgets(?:/page/(\d+))?(?:/limit/(\d+))',
array(
'controller' => 'widget',
'action' => 'list',
),
array(
1 => 'color',
2 => 'page',
3 => 'limit'
),
'%s-Widgets/'
);
$router->addRoute('color_widgets', $route);
I then call the URL Helper with following code
echo $this->url(array('page' => $page), 'color_widgets', false);
This results in /Blue-Widgets/ and does not send the Page to the URL. I can fix this by changing the reverse in the Router
$route = new Zend_Controller_Router_Route_Regex(
'([a-zA-Z-_0-9-]+)-Widgets(?:/page/(\d+))?(?:/limit/(\d+))',
array(
'controller' => 'widget',
'action' => 'list',
'page' => 1
),
array(
1 => 'color',
2 => 'page',
3 => 'limit'
),
'%s-Widgets/page/%d'
);
However this doesn't solve my problem say I have a Url
/Blue-Widgets/page/1/limit/10 The limit doesn't show, again I can fix that with the following
$route = new Zend_Controller_Router_Route_Regex(
'([a-zA-Z-_0-9-]+)-Widgets(?:/page/(\d+))?(?:/limit/(\d+))',
array(
'controller' => 'widget',
'action' => 'list',
'page' => 1,
'limit' => 10
),
array(
1 => 'color',
2 => 'page',
3 => 'limit'
),
'%s-Widgets/page/%d/limit/%d'
);
The problem with this is the user is at
/Blue-Widgets/ and I want to take them to the next page of Blue Widgets with following code
echo $this->url(array('page' => $page), 'color_widgets', false);
They are actually taken to
/Blue-Widgets/page/2/limit/10
When I actually want to take them to
/Blue-Widgets/page/2
How can I accomplish this with the Zend Framework.

It is not possible to use a regex reverse route with a variable number of values.
You could:
Write a different route for every optional parameter (not recommended)
Use a different route structure
You could, for instance change your route to:
$route = new Zend_Controller_Router_Route(
'widgets/:color/*',
array(
'controller' => 'widget',
'action' => 'list',
'page' => 1,
'limit' => 10
),
array(
'color' => '[a-zA-Z-_0-9-]+',
'page' => '\d+',
'limit' => '\d+',
)
);
Another option would be to create your own custom route class, which can parse and build the correct uri.

You've give wrong Regex match variable indexes, that's why you get weird results. Your code should look like this:
$route = new Zend_Controller_Router_Route_Regex(
'([a-zA-Z-_0-9-]+)-Widgets(?:/page/(\d+))?(?:/limit/(\d+))',
array(
'controller' => 'widget',
'action' => 'list',
),
array(
1 => 'color',
3 => 'page',
5 => 'limit'
),
'%s-Widgets/'
);
$router->addRoute('color_widgets', $route);

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

a link pass multiple parameter in zend framework

hi i am new in zend framework 2.2.0. i want to create the a link that go to delete page right now i am passing only id in the url so i want to pass another id in it.
Add to Trash
right now in this link message id is passing i also want to pass one more id named "did" in this link
Add to Trash
how can i get this ?
thanks in advance
You should use url view helper's third argument ($options) to pass your variables in the query string. Example:
$route = 'message';
$param = array('action' => 'delete');
$opts = array(
'query' => array(
'id' => $message->message_id,
'did' => $message->deliver_id
)
);
$this->url($route, $params, $opts);
You have to add "did" to your message route like this:
'router' => array(
'routes' => array(
'message' => array(
'type' => 'segment',
'options' => array(
'route' => '/:action/:id[/:did]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]+',
'id' => '[0-9]+',
'did' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Application\Controller\Index',
),
),
),
),
),
echo $this->url('message',array('action'=>'delete', 'id' => $message->message_id,'did'=>$message->deliver_id);
// output: /delete/1/2

Unknown Item type error in socialengine even registered in manifest.php file of module

I am getting strange error. please help
I am creating custom module named "Advinvite"
I have created table : "engine4_advinvite_matchinvites"
I have created DbTable : "Advinvite_Model_DbTable_Matchinvites"
I have created DbTable's Row class model : "Advinvite_Model_Matchinvite" And Primary key in the table is "matchinvite_id"
I have also created Api: "Advinvite_Api_Core"
The main part I have also registered the item in manifest.php file of settings folder of module like :
// Items ---------------------------------------------------------------------
'items' => array(
'advinvite_matchinvite',
)
Now regarding Error :
I am getting error "Unknown Item Type : advinvite_matchinvite" when i want to use
Engine:Api:_()->getItem('advinvite_matchinvite',its_Identity)
I am sending the manifest file code too below. please can you check it for me and help me. It has been come in my life first time (weird error) and i am trying to solve this for last three/four days. Even Everything i did correctly why it is happening Not understanding. Please help me mate. It will be very appreciated.
And also if you can help me with this too means Routes are also not working....Don't know why these both errors are coming
Help Me Please.
Manifest.php Code:
<?php return array (
'package' =>
array (
'type' => 'module',
'name' => 'advinvite',
'version' => '4.0.0',
'path' => 'application/modules/Advinvite',
'title' => 'Advanced Invites',
'description' => 'This will invite the users for matches and nets',
'author' => 'Gitesh Dang',
'callback' =>
array (
'class' => 'Engine_Package_Installer_Module',
),
'actions' =>
array (
0 => 'install',
1 => 'upgrade',
2 => 'refresh',
3 => 'enable',
4 => 'disable',
),
'directories' =>
array (
0 => 'application/modules/Advinvite',
),
'files' =>
array (
0 => 'application/languages/en/advinvite.csv',
),
// Items ---------------------------------------------------------------------
'items' => array(
'advinvite_matchinvite',
),
// Routes --------------------------------------------------------------------
'routes' => array(
'advinvite_general' => array(
'route' => 'invites/:action/*',
'defaults' => array(
'module' => 'advinvite',
'controller' => 'index',
'action' => '(match|matchview)',
),
'reqs' => array(
'action' => '\D+',
)
),
)
),
); ?>
Waiting for postive response.
Kind regards
Mates,
It has been fixed.
The issue was minor :)
Items was registered in package array and it should be registered outside of package array. Package array should be closed to files array only.
Corrected Manifest File :
<?php return array (
'package' =>
array (
'type' => 'module',
'name' => 'advinvite',
'version' => '4.0.0',
'path' => 'application/modules/Advinvite',
'title' => 'Advanced Invites',
'description' => 'This will invite the users for matches and nets',
'author' => 'Gitesh Dang',
'callback' =>
array (
'class' => 'Engine_Package_Installer_Module',
),
'actions' =>
array (
0 => 'install',
1 => 'upgrade',
2 => 'refresh',
3 => 'enable',
4 => 'disable',
),
'directories' =>
array (
0 => 'application/modules/Advinvite',
),
'files' =>
array (
0 => 'application/languages/en/advinvite.csv',
),
),
// Items ---------------------------------------------------------------------
'items' => array(
'advinvite_matchinvite',
),
// Routes --------------------------------------------------------------------
'routes' => array(
'advinvite_general' => array(
'route' => 'invites/:action/*',
'defaults' => array(
'module' => 'advinvite',
'controller' => 'index',
'action' => '(match|matchview)',
),
'reqs' => array(
'action' => '\D+',
)
),
)
); ?>
Cheers !! :)

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