Clearing all prefixes

痴心易碎 提交于 2019-12-11 05:58:28

问题


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?

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?


回答1:


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);
    }
}

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




回答2:


I know it's been 2 years since the question above was answered, though I think I found an even less intrusive way to accomplish what you want.
Set the prefix name dynamically by taking the current prefix value from $this->params and set it to false, like so

$this->Html->link('hello', array($this->params['prefix']=>false, 'controller'=>'posts','action'=>'index'));

The value of $this->params['prefix'] will be the one and relevant at that moment to set to false.

cheers



来源:https://stackoverflow.com/questions/8014296/clearing-all-prefixes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!