Zend Framework 中的简单重写 [英] Simple rewrites in Zend Framework

查看:18
本文介绍了Zend Framework 中的简单重写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个非常简单的问题,但我只找到了复杂的答案.我有一个需要用户登录的 Zend Framework 应用程序.loginAction()logoutAction()AuthController 中定义.我想允许用户通过 http://www.example.com/login 而不是 登录href="http://www.example.com/auth/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:

  1. .htaccess 重写
  2. 创建 LoginController 并将 indexAction() 重定向到 auth/login
  3. 使用 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 中的简单重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆