Defense
Defense

Reputation: 259

Zend Framework: Router

That is my two routers:

->addRoute('viewTextMaterial', new Zend_Controller_Router_Route(':mCat/:mCatSub/:mId/:mTitle', array('controller' => 'index', 'action' => 'viewtextmaterial')))
->addRoute('viewNews', new Zend_Controller_Router_Route(':nCat/:nId/:nTitle/:page', array('controller' => 'index', 'action' => 'viewnews')))

In index.phtml file I add this:

<a href="<?= $this->url(array('mCat' => 'Test', 'mCatSub' => 'Test', 'mId' => 7, 'mTitle' => 'Test'), 'viewTextMaterial') ?>">Test</a>

Exp. for viewnews URL:

<a href="<?= $this->url(array('nCat' => News, 'nId' => 5, 'nTitle' => Some title, 'page' => 1), 'viewNews') ?>">some text</a>

But why, when I click a href, it redirect me to 'viewnews'?

Upvotes: 0

Views: 103

Answers (1)

codisfy
codisfy

Reputation: 2183

In my experience(which is not very great :) ) I think when you use the colon in front of a name, when you are defining a router i.e like

'/:mCat/:mCatSub/:mId/:mTitle',
      array(
           'controller' => 'index', 
           'action'    => 'viewtextmaterial'
            )

What you are telling the router to do is to route any url, which follows the above format('/:mCat/:mCatSub/:mId/:mTitle'), to be routed to the controller/action you mentioned there. eg.

someController/action/x/y

or

anoCont/act/a/b

would be routed to the same controller/action.

So in your case what you are doing is you are defining two routers with same options(which creates ambiguity), and by default the second defined route is used(Bottom to top matching).

you can use something like this

'/test/:mCatSub/:mId/:mTitle',
          array(
               'controller' => 'index', 
               'action'    => 'viewtextmaterial'
                )

so anything that starts with 'test' as controller(in the url) would now be routed to your desired controller/view.

Hope it works.. :) (If it doesn't please enlighten me :) )

Upvotes: 1

Related Questions