Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
351 views
in Technique[技术] by (71.8m points)

php - Simple rewrites in Zend Framework

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?')

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...