Deniz Zoeteman
Deniz Zoeteman

Reputation: 10111

Route to index in controller

I would like to route back to index ('/') in my controllers. How would I do this, I tried the following:

public function randomAction()
{
  return $this->redirect()->toRoute('/');
}

But with this I get the following error:

Route with name "" not found

Any ideas why this is happening, and how I can properly route back to index? I could use /application, but I don't want that to appear in the address bar. Is my only option in this case toUrl('/')?

Upvotes: 0

Views: 54

Answers (2)

Tomdarkness
Tomdarkness

Reputation: 3820

You need to pass the name of the route to toRoute, not the actual route.

If you are using the ZF Skeleton Application then the "index" route is called home so you'd do the following:

return $this->redirect()->toRoute('home');

If not open your module config and find the routes array. The name of the route is the key in this array that points to your route configuration.

Upvotes: 1

Remi Thomas
Remi Thomas

Reputation: 1528

For example on the skeleton application, try the route home

public function randomAction()
{
  return $this->redirect()->toRoute('home');
}

ZF2 Docs

toRoute($route, array $params = array(), array $options = array()): Redirects to a named route, using the provided $params and $options to assembled the URL.

Upvotes: 1

Related Questions