46363

How to make parameter optional in Zend Framework Router

Question:

I would like to know the .ini config file setup to make a route wherein the page number parameter is optional so that

http://news.mysite.com/national http://news.mysite.com/national/1

point to the same page.

I have the code as follows

resources.router.routes.news_list.type = "Zend_Controller_Router_Route_Regex" resources.router.routes.news_list.route = "([a-zA-Z0-9\-]+)/([0-9\-]+)" resources.router.routes.news_list.defaults.module = "news" resources.router.routes.news_list.defaults.controller = "index" resources.router.routes.news_list.defaults.action = "category" resources.router.routes.news_list.defaults.page = 1 resources.router.routes.news_list.map.1 = "categ" resources.router.routes.news_list.map.2 = "page" resources.router.routes.news_list.reverse = "%s/%s"

can some1 help me out with a solution to modify this code to make it work for both urls

Answer1:

You can do this without the regex route using:

routes.news_list.route = ":action/:page" routes.news_list.defaults.module = "news" routes.news_list.defaults.controller = "index" routes.news_list.defaults.action = "category" routes.news_list.defaults.page = 1

I assume that you want every request to go to the "news" module and the "index" controller. Only the action and the page are variable in the url.

Answer2:

resources.router.routes.news_list.type = "Zend_Controller_Router_Route_Regex" resources.router.routes.news_list.route = "([a-zA-Z0-9-]+)/?([0-9]+)?" resources.router.routes.news_list.defaults.module = "news" resources.router.routes.news_list.defaults.controller = "index" resources.router.routes.news_list.defaults.action = "category" resources.router.routes.news_list.defaults.page = 1 resources.router.routes.news_list.map.1 = "categ" resources.router.routes.news_list.map.2 = "page" resources.router.routes.news_list.reverse = "%s/%d"

Something like this could work too in case you need to use Regex.

Recommend

  • Internal link in email?
  • Regular Expression for UK Telephone Number
  • How to access span using beautifulSoup?
  • How to use anchors in Symfony routing?
  • React Bootstrap Checkbox in Nav -> NavItem does not rerender on click
  • ng build failed to execute 'replaceState' on 'History': A history state object w
  • How to use Vue Router in Vue 2
  • Transform upload with NodeJS Multer
  • How to pass props to react component through routing using react-router?
  • Can Angular 2 parse links received from external CMS to be resolved as internal
  • Execute particular component after click on md dialog button
  • Exported variable 'router' has or is using name 'Router' from external module bu
  • Heroku R10 Boot Timeout Error
  • Socket Lose Connection
  • angular2 rc3 router alpha 3.0.0.7 default route
  • JQuery not working in Ajax load page
  • How to initialize widget in component Vue?
  • Post request Axios : Network error
  • Angular 2 router auxiliary routes not working on redirectTo
  • toastr undefined with requirejs
  • What is the difference between sap.ui.core.routing.Router.navTo() and sap.m.routing.Targets.display(
  • Angular 2: TypeError: Cannot read property 'isRouteActive' of undefined in
  • Durandal Modal Won't Close
  • How to show different validation messages for email validation in Angular2 using Validators class?
  • angular.js ui-route how catch route or url or params after reject?
  • What products support 3-digit region subtags, e.g., es-419 for Latin-American Spanish?
  • why route of backbone needs the default route
  • How to unit test a Angular 4 component which uses router.paramMap
  • Webpack-dev-server and isomorphic react-node application
  • Highlight one bar in a series in highcharts?
  • Android screen density dpi vs ppi
  • How would I use PHP exceptions to define a redirect?
  • How to extract text from Word files using C#?
  • log4net write single file for each call to log.info
  • Benchmarking RAM performance - UWP and C#
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • need help with bizarre java.net.HttpURLConnection behavior
  • How can I remove ASP.NET Designer.cs files?
  • java string with new operator and a literal