問題描述
這似乎是一個非常簡單的問題,但我只找到了復雜的答案.我有一個需要用戶登錄的 Zend Framework 應用程序.loginAction()
和 logoutAction()
在 AuthController
中定義.我想允許用戶通過 http://www.example.com/login 而不是 登錄 rel="nofollow">http://www.example.com/auth/login.
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.
我知道有很多方法可以做到這一點,我考慮過的 3 種方法是:
I know there are numerous ways of doing this, the 3 I've considered are:
- .htaccess 重寫
- 創建 LoginController 并將 indexAction() 重定向到 auth/login
- 使用 Zend_Controller_Router_Rewrite 定義我自己的路由.
如果可能的話,我寧愿將它排除在 #1 之外.#2 很容易理解,雖然它看起來像一個黑客.它還可能會用一堆 5 行控制器"類來混淆代碼.我認為 #3 是要走的路,但我不完全了解如何有效地使用它.我試過 使用 Zend_ConfigRewriteRouter 雖然我只定義了登錄路由,所以每個鏈接都變成了/login"(我想我錯過了一個默認路由).我在 Bootstrap.php 中做了這個,我不確定那是否正確.
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.
有沒有我遺漏的簡單方法?我是否錯誤地使用了#3?是否有我應該閱讀的教程?(我看過 Zend 文檔,這很好,但我經常發現自己在問,'這段代碼應該放在哪里:在控制器、模型、引導程序中,還是在其他中?')
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?')
推薦答案
對于一個定義的目的,比如你有一個 命名" 路由,這將是最簡單的方法.雖然實現命名路由的方法有很多種,但最簡單的方法是將它放在 application.ini 中:
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.
當使用 url() 助手調用路由時,重要的是要記住使用命名路由:
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') ?>
或者如果你需要傳遞普通的 'controller' => , 'action' => :
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 表示這將是 Zend/Controller/Router/Route/Module.php 中定義的默認路由
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
這篇關于Zend Framework 中的簡單重寫的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!