CakePHP-/admin/的admin路由,但不是所有其他路由(设置默认admin为false的方法?)


CakePHP - admin routing for /admin/, but not all the rest (way to set default admin false?)

我阅读了教程,发现要使用"admin"前缀,只需取消注释:

Configure::write('Routing.prefixes', array('admin'));

config/core.php文件。

我做到了,我的管理路由工作得很好——/admin/users/add点击了我的users_controller中的admin_add((函数。

问题是——它也改变了我的正常链接——也就是说,我的"注销"按钮现在试图转到/admin/users/LOGOUT,而不仅仅是/users/LOGOUT。我意识到我可以添加"admin"=>false,但我不想对我网站上的每个链接都这样做。

有没有一种方法可以让它只使用"admin"=>true或/admin/。。。去管理员,而不是所有其他的链接?

扩展用户Abba Bryant的编辑,看看如何在烹饪书中创建助手:http://book.cakephp.org/view/1097/Creating-Helpers

如果必须在所有链接上手动禁用路由是一件令人烦恼的事情(这对我来说会很麻烦!(,你可以创建一个新的助手MyCustomUrlHelper(当然名称不必那么长(,并让它使用核心UrlHelper为你生成URL。

class MyCustomUrlHelper extends AppHelper {
    public $helpers = array('Html'); 
    function url($controller, $action, $params ,$routing = false, $plugin = false) {
       //Example only, the params you send could be anything
       $opts = array(
          'controller' => $controller,
          'action'     => $action
          //....
       );
    }
   //another option
   function url($params) {
       //Example only, the params you send could be anything
       $opts = array(
          'controller' => $params['controller'],
          'action'     => $params['action']
          //....
       )
    }
   //just fill up $opts array with the parameters that core URL helper
   //expects. This allows you to specify your own app specific defaults
   return $this->Html->url($opts); //finally just use the normal url helper
}

基本上,你可以根据自己的需要使它变得冗长或简洁。它只是实际URL帮助程序的一个包装类,它将从内部完成工作。这允许您提供适用于特定应用程序的默认值。这还允许您在一个地方进行更改,并更新整个应用程序的路由。

编辑

您还可以检查传递的$opts数组是否为字符串。这样你就可以两全其美。

如果您使用前缀路由,请确保您在HtmlHelper::link调用中处理它,如so

<?php
    ...
    echo $html->link( array(
        'controller' => 'users',
        'action' => 'logout',
        'plugin' => false,
        'admin' => false,
    ));
    ...
?>

**编辑**您可以扩展AppHelper中的url函数来检查传递的数组,如果在url调用中尚未设置Routingprefix键,则可以将其设置为false。

然后,每次都需要在管理链接中指定前缀。

HtmlHelper接受两种提供URL的方式:它可以是一个与Cake相关的URL,也可以是一组URL参数。

如果您使用URL参数,默认情况下,如果您没有指定"admin"=>false参数,HtmlHelper会在您执行管理操作时自动在操作前加上"admin"。

IMHO,去掉这个参数的最简单方法是使用Cake相对URL作为字符串。

<?php
//instead of using
//echo $this->Html->link(__('logout', true), array('controller' => 'users', 'action' => 'logout'));
//use
echo $this->Html->link(__('logout', true), '/users/logout');

问候,

nIcO

我本周遇到了这个问题,这段代码似乎解决了它。如果它对你不起作用,请告诉我,我可以试着找出我还做了什么来让它起作用。

$this->Auth->autoRedirect = false;
$this->Auth->loginAction = array(Configure::read('Routing.admin') => false, 'controller' => 'users', 'action' => 'login');
$this->Auth->logoutRedirect = array(Configure::read('Routing.admin') => false, 'controller' => 'users', 'action' => 'logout');     
$this->Auth->loginRedirect = array(Configure::read('Routing.admin') => false, 'controller' => 'users', 'action' => 'welcome');

这真的很令人沮丧,所以我很高兴能帮上忙。

我参加聚会迟到了,但我有一个很好的答案:

您可以通过创建AppHelper类来覆盖默认行为。创建app/app_help.php并粘贴以下内容:

<?php
class AppHelper extends Helper{
    function url($url = null, $full = false) {
        if(is_array($url) && !isset($url['admin'])){
            $url['admin'] = false;
        }
        return parent::url($url, $full);
    }
}
?>

除非在调用link((或url((时指定,否则admin将设置为false。