3

This seems like a very simple question, but I've only found complicated answers. I've got a Zend Framework application that requires users to login. The loginAction() and logoutAction() are defined in AuthController. I want to allow users to login via http://www.example.com/login rather than http://www.example.com/auth/login.

I know there are numerous ways of doing this, the 3 I've considered are:

  1. .htaccess rewrite
  2. Creating a LoginController and redirecting the indexAction() to auth/login
  3. Defining my own routes using Zend_Controller_Router_Rewrite.

I'd rather keep it out of #1 if possible. #2 is easy enough to understand, although it seems like a hack. It also could clutter the code with a bunch of 5-line "Controller" classes. I think #3 is the way to go but I don't fully understand how to use it effectively. I have tried Using Zend_Config with the RewriteRouter although I only defined the login route so every link became '/login' (I think I was missing a default route). I did this in my Bootstrap.php, I'm not sure if that was correct.

Is there a simple approach I'm missing? Am I using #3 incorrectly? Are there tutorials for this that I should read? (I've looked at the Zend documentation which is good but often I find myself asking, 'Where should this code go: in a controller, model, bootstrap, other?')

vascowhite
  • 18,120
  • 9
  • 61
  • 77
nachito
  • 6,975
  • 2
  • 25
  • 44

2 Answers2

10

for a defined purpose like you have a "named" route would be the simplest way to do it. While there are any number of ways to implement a named route the easiest is to put it in the application.ini:

    // /application/configs/application.ini
    resources.router.routes.login.route = /login
    resources.router.routes.login.defaults.module = default
    resources.router.routes.login.defaults.controller = auth
    resources.router.routes.login.defaults.action = login

putting it in your bootstrap is not wrong, it just doesn't seem as convienient to me.
Also doing it this way should (no guarantees) prevent any problems with the default routes.

When calling a route using the url() helper it is important to remember to use either the named route :

<?php echo $this->url(array(), 'routeName') ?>

or if you need to pass the normal 'controller' => , 'action' => :

<?php echo $this->url(array('controller' => 'index', 'action' => 'index'), 'default') ?>

near as I can tell 'default' in this context indicates this would be a default route as defined in Zend/Controller/Router/Route/Module.php

RockyFord
  • 8,529
  • 1
  • 15
  • 21
  • Per your instructions I've added these lines to my application/configs/application.ini and navigating to /login works great! However, all the links on the page (which are generated in a layout template with `$this->url(array('controller' => 'foo', 'action' => 'bar'))`) are now /login. This only happens on the login page though, the links (and routes) from the home page and all other controllers work fine. Any thoughts? – nachito Jan 23 '12 at 17:31
  • 1
    Figured this out, I should be using `$this->url(array('controller' => 'foo', 'action' => 'bar'), 'default')`. If the second parameter is `null` (or omitted) then the current route is assumed. – nachito Jan 23 '12 at 20:33
3

Just in case that you are interested in how to do it in your bootstrab in correct way.

If you have only one route to rewrite, just add function:

protected function _initRoute() {
    $router = Zend_Controller_Front::getInstance()->getRouter();
    $router->addRoute('login', //key of route !
        new Zend_Controller_Router_Route(
            'login', //this is url(www.url.com/login) that you want to rewrite, you can set whatever you want
            array(
                'module' => 'default',
                'controller' => 'auth',
                'action' => 'login',
            )
    ));
}

Now, every www.your-url.com/login request will point to www.your-url.com/default/auth/login (module/controller/action)

Edit:


If you want to use your new url in view file, you have to use key for that route

in view.phtml

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

The first parameter is array(), and the second one is key which you define in bootstrap. Than if you change URL to /new-login in bootstrap, all URLs will be changed in view files.

tasmaniski
  • 4,767
  • 3
  • 33
  • 65
  • Thank you for your answer, it seems to work in the same way that [this](http://stackoverflow.com/a/8966962/961455) does, with the same strange behavior on the /login page with all the links being to /login. Any thoughts? – nachito Jan 23 '12 at 17:40
  • Are you ask how to set all links dynamic point to new url ?? if not, can you little explain your "strange" behavior ? – tasmaniski Jan 23 '12 at 18:08
  • All the links on the page (which are generated in a layout template with $this->url(array('controller' => 'foo', 'action' => 'bar'))) are now /login. This only happens on the login page, the links (and routes) from the home page and all other controllers work fine. – nachito Jan 23 '12 at 18:29
  • I edit my answer, but I don't know where is the problem in your code. It isn't in routing, because both of answer is correct. try to set: "$router->addDefaultRoutes();" at the and of _initRoute() that is my blind shot... – tasmaniski Jan 23 '12 at 18:48
  • 1
    Hey. Set "... , 'default', true ..." !!! $this->url(array('controller' => 'foo', 'action' => 'bar'), 'default', true); :D – tasmaniski Jan 23 '12 at 20:05