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

查看:96
本文介绍了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行的"Controller"类使代码混乱.我认为#3是必经之路,但我不完全了解如何有效地使用它.我已经尝试将Zend_Config与RewriteRouter 尽管我只定义了登录路由,所以每个链接都变成了"/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') ?>

在这种情况下,我几乎可以告诉您默认",这表明这将是 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天全站免登陆