清除所有前缀 [英] Clearing all prefixes

查看:168
本文介绍了清除所有前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在现有的CakePHP应用程序中有几个前缀。我也有一点主导航在布局指向共享方法。我知道我可以显式设置每个前缀为false,以避免与前缀链接,但有一个简单的路径,只是告诉Cake不使用任何前缀,无论哪一个上下文可能当前存在?

I have several prefixes in play in an existing CakePHP app. I also have a bit of primary navigation in the layout that points to shared methods. I know I can explicitly set each prefix to false to avoid linking with the prefix, but is there a shortcut path that simply tells Cake to no use any prefixes no matter which one's context may currently exist?

例如,我在一个地方可以注册的页面( / realtor / users / register )。我有一个类似的前缀给检查员和承包商,因为注册过程略有不同。由于我没有通过验证,因此在主导航栏中有一个登录链接,但登录操作是由所有用户类型共享的,应该无需任何前缀即可访问。

For example, I'm on a page where a realtor can register (/realtor/users/register). I have a similar prefix for inspectors and contractors because the registration process is slightly different. Since I'm not authenticated, there's a Login link in the primary nav, but the login action is shared by all user types and should be accessed without any prefix.

<?php echo $this->Html->link( 'Login', array( 'controller' => 'users', 'action' => 'login', 'realtor' => false, 'inspector' => false, 'contractor' => false ) ) ?>

我想能够在链接中关闭所有前缀,单独关闭每个可能的前缀。可能?

I'd like to be able to, in the link, just turn off all prefixing rather than turning off each possible prefix independently. Possible?

推荐答案

如果丢失路由功能不是一个问题,您可以传递一个字符串而不是数组到link()方法:

If loosing the routing capabilities is not a problem for you, you could pass a string instead of an array to the link() method:

<?php 
echo $this->Html->link('Login', '/users/login');
?>

EDIT

为了保持路由机制,这里有一个小助手可以做的窍门:

To keep routing mechanism, here is a little Helper that would do the trick:

class MyHtmlHelper extends HtmlHelper
{
    public function link($title, $url = null, $options = array(), $confirmMessage = false)
    {
        $prefixes = Router::prefixes();

        foreach($prefixes as $prefix)
        {
            $url[$prefix] = false;
        }

        return parent::link($title, $url, $options, $confirmMessage);
    }
}

当然你可以改变方法名称保持标准的link()方法。我用Cake2测试这个,但是这应该与Cake1.3一起使用

Off course you could change the method name if you want to keep the standard link() method. I tested this with Cake2, but this should work with Cake1.3

这篇关于清除所有前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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