1

In my IndexController, I currently have indexAction (homepage), loginAction and logoutAction. I'm trying to remove "/index/" from the URL to get domain.com/login instead of domain.com/index/login.

What is the cleanest way to achieve this? Is there a RegEx we can use? I don't ever want /index/ in the URL.

My current solution, which I believe can be improved upon, is below. Also, what does the first parameter in addRoute() do?

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
  protected function _initViewHelpers()
  {
    $front  = Zend_Controller_Front::getInstance();
    $router = $front->getRouter();
    $router->addRoute('login',
      new Zend_Controller_Router_Route('login/*', array(
        'controller' => 'index',
        'action'     => 'login'
      ))
    );
    $router->addRoute('logout',
      new Zend_Controller_Router_Route('logout/*', array(
        'controller' => 'index',
        'action'     => 'logout'
      ))
    );
  }
}

1 Answer 1

2

There is nothing to impove, you have to create route for every action. This will allow you to change route defaults (module/controller/action) without modifying your code.

First parameter is the route name, which you have to use with url() helper in your views:

<a href="<?php echo $this->url(array(), 'login', true); ?>">Login</a>

Update. You can use such route, if you want only one route without "index" in url:

$router->addRoute('default',
  new Zend_Controller_Router_Route(':action/*', array(
    'controller' => 'index',
  ))
);
Sign up to request clarification or add additional context in comments.

3 Comments

I can't use that because I'm using Zend_Navigation to manage the links. I'm looking for a way to use a RegEx so that I only have to use addRoute() once.
I updated my answer. BTW what's the problem with Zend_Navigation?
Thanks! That worked except I had to change the 1st param for addRoute() from 'default' to 'index' otherwise my URL ended up as domain.com/index/controller/login. There's no problem with Zend_Navigation, what I meant was I'm not building the hyperlinks myself, I'm simply printing $this->navigation()->menu(); from my layout.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.