Add Page Parameter in Zend Framework Doesn't Work - zend-framework

I've been trying to figure this out these days. I got a problem when I want to add 'page' parameter in my URL for my pagination.
This is my router
->addRoute('budi',new Zend_Controller_Router_Route(':lang/budi',array('controller' => 'budi', 'action' => 'index', 'page' => 1), array('lang'=>$s, 'page' => '\d+')))
->addRoute('budi1',new Zend_Controller_Router_Route(':lang/budi/page/:page',array('controller' => 'budi', 'action' => 'index', 'page' => 1), array('lang'=>$s, 'page' => '\d+')))
Then I access my URL
http://localhost/learningsystem/en/budi
but when I hover on my pagination links, the page parameter doesn't appear. The URL is still
http://localhost/learningsystem/en/budi
but if I enter same URL with index in the end like this one
http://localhost/learningsystem/en/budi/index
or like this one
http://localhost/learningsystem/en/budi/page/1
the page parameter appears perfectly when I click the page 2 link http://localhost/learningsystem/en/budi/index/page/2
Actually, I don't want include 'index' or 'page' at first in my URL. Anyway, I use default pagination.phtml template from Zend. Anyone please help me to solve this problem?
Thank you very much

How about something like these?
$router->addRoute(
'budi',
new Zend_Controller_Router_Route_Regex(
'(.*)/budi',
array('controller' => 'budi', 'action' => 'index', 'page' => 1),
array(1 => 'lang', 2 => 'page'),
'%s/budi/page/%d'
)
);
$router->addRoute(
'budi1',
new Zend_Controller_Router_Route_Regex(
'(.*)/budi/page/(\d*)',
array('controller' => 'budi', 'action' => 'index'),
array(1=>'lang', 2=>'page'),
'%s/budi/page/%d'
)
);

Related

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.

Zend_Controller_Router_Route_Regex Optional Parameters with Reverse

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);

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 parameter exceptions

My problem is I want some parameter values, passed through URL, don't trigger the Zend routing but lead to defaul controller/action pair.
Right now I have following in my index.php:
// *** routing info ***
$router = Zend_Controller_Front::getInstance()->getRouter();
$router->addRoute('showpage', new Zend_Controller_Router_Route('/show/:title',
array('controller' => 'Show',
'action' => 'page')));
// annoying exceptions :(
$router->addRoute('addshow', new Zend_Controller_Router_Route('/show/add',
array('controller' => 'Show',
'action' => 'add')));
$router->addRoute('saveshow', new Zend_Controller_Router_Route('/show/save',
array('controller' => 'Show',
'action' => 'save')));
$router->addRoute('addepisode', new Zend_Controller_Router_Route('/show/addEpisode',
array('controller' => 'Show',
'action' => 'addEpisode')));
$router->addRoute('saveepisode', new Zend_Controller_Router_Route('/show/saveEpisode',
array('controller' => 'Show',
'action' => 'saveEpisode')));
without last 4 routers, URL /show/add leads to show/page, carrying title == 'add'.
Please, every help will be much appreciated.
You can use a regular expression to reject add, save, addEpisode and saveEpisode
$router->addRoute(
'showpage',
new Zend_Controller_Router_Route(
'/show/:title',
array(
'controller' => 'show',
'action' => 'page'
),
array(
'title' => '(?:(?!add)(?!save)(?!addEpisode)(?!saveEpisode).)+'
)
)
)
First, use Zend_Controller_Router_Route_Static for the static routes.
Secondly, I'm pretty sure you don't need to include the leading forward slash, though I'm not sure if this is an issue.
As routes are matched in reverse order, yours should work (I think). For anything not matching "saveEpisode", "addEpisode", "save" or "add", it should fall through to the "showpage" route.
The only other thing I could think of would be to make the "showpage" route more specific, something like
'show/page/:title'

Zend Framework Change parameter in route on the same spot?

I'm not sure how to fix this, or wat is the best way to approach this. Also couldn't find enough information to get me on the right way (could be that my searching sucks..)
Anyway, this is my problem:
I defined a route in my bootstrap file:
protected function _initRoutes()
{
$router = $this->frontController->getRouter();
$router->removeDefaultRoutes();
$router->addRoute(
'delete',
new Zend_Controller_Router_Route('/:controller/:action/:id/',
array('controller' => ':controller',
'action' => ':action',
'id' => ':id',
)
)
);
}
This works perfectly for my update and delete actions.
Now I've added the pagination to the indexpage. The pagination expects the page parameter. Because I haven't set this in my route, it cannot pass it, so my pagination doesn't work (as in switching between results).
I understand this. But what I want is that on the index page the id parameter isn't necessary and replace this with the page parameter.
Trying another route replacing id with page didn't work.
Is there a good way to solve this in the bootstrap or is it the best way to check for the action, and depending on the action, index or update/delete, define the route. The best place would than be a plugin?
Any advice or tips are greatly appreciated!
While working on another aspect of the application I came back to the same problem. I solved it, by specifying the routes much more.
First I deleted the $router->removeDefaultRoutes(); rule.
And then instead of (which didn't work):
$router->addRoute(
'crud',
new Zend_Controller_Router_Route('/:controller/:action/:id', array('controller' => ':controller', 'action' => ':action', 'id' => ':id'))
);
$router->addRoute(
'pagination',
new Zend_Controller_Router_Route('/:controller/:action/:page', array('controller' => ':controller', 'action' => ':action', 'page' => ':page'))
);
I now use this:
$router->addRoute(
'crud',
new Zend_Controller_Router_Route('/:controller/:action/:id', array('controller' => ':controller', 'action' => ':action', 'id' => ':id'))
);
$router->addRoute(
'pagination',
new Zend_Controller_Router_Route('/:controller/index/:page', array('controller' => ':controller', 'action' => 'index', 'page' => ':page'))
);