1

I'm getting into trouble with passing paramters by URL in Zend Framework.

For example, I have a Controller like this:

class MyController extends AbstractActionController {
    public function indexAction() { ... }
    public function updateAction($id) { ... }
}

I want to access the update page by a URL like this: http://example.com/mycontroller/update/1

So how to pass the value 1 to the controller?

Can anyone help me? Thanks a lot.

BTW, I have changed the default router from

'route'    => '/[:controller[/:action]]'

to

'route'    => '/[:controller[/:action[/:id]]]',

but it still doesn't work. And here's the error message below.

Warning: Missing argument 1 for Module\Controller\MyController::updateAction() call in ...

Haozhe Xie
  • 3,438
  • 7
  • 27
  • 53

1 Answers1

2

I've solved the problem!

You should change the function as following:

public function updateAction()
{
    $paramName = $this->getEvent()->getRouteMatch()->getParam('id');

    // Do something with $paramName

    return array();
}

There're more methods to get parameters in Zend Framework2: How to access route, post, get etc. parameters in Zend Framework 2

Community
  • 1
  • 1
Haozhe Xie
  • 3,438
  • 7
  • 27
  • 53