Zend Framework 中的简单重写

2023-05-30php开发问题
1

本文介绍了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 中的简单重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17