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

发布于 2024-10-30 02:48:08 字数 436 浏览 2 评论 0原文

我阅读了教程,发现要使用“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/... 的网址才能转到管理员,而不是所有其余的链接?

I read the tutorial, and found that to use the "admin" prefix, you can just uncomment the:

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

config/core.php file.

I did that, and my admin routing works great - /admin/users/add hits the admin_add() function in my users_controller.

The problem is - it's also changing my normal links - ie. my "LOGOUT" button now tries to go to /admin/users/logout instead of just /users/logout. I realize I can add 'admin'=>false, but I'd rather not have to do that for every link in my site.

Is there a way to make it so ONLY urls with either 'admin'=>true or /admin/... to go to the admin, and NOT all the rest of the links?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

属性 2024-11-06 02:48:08

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

如果必须在所有链接上手动禁用路由是一件烦恼(这对我来说!),您可以创建一个新的帮助器< code>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 数组是否是字符串。这样您就可以两全其美。

Expanding on user Abba Bryant's edit, have a look at how to create a helper in the cook book : http://book.cakephp.org/view/1097/Creating-Helpers

If having to disable routing manually on all your links is an annoyance (it would be to me!), you could create a new helper MyCustomUrlHelper (name doesn't have to be that long of course), and have it use the core UrlHelper to generate the URLs for you.

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
}

Basically you can make it as verbose or terse as you want. It's just a wrapper class for the the actual URL helper which will do the work from inside. This allows you to give defaults that work for your specific application. This would also allow you to make a change in one place and have the routing for the whole application be updated.

EDIT

You could also check whether the passed $opts array is a string. This way you can have the best of both worlds.

思念绕指尖 2024-11-06 02:48:08

确保您是否使用在 HtmlHelper::link 调用中处理它的前缀路由,如下所示

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

** 编辑 **
您可以扩展 AppHelper 中的 url 函数来检查传递的数组,并将 Routing.prefixes 键设置为 false(如果尚未在 url 调用中设置)。

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

Make sure if you use the prefix routing that you handle it in the HtmlHelper::link calls like so

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

** EDIT **
You could extend the url function in your AppHelper to inspect the passed array and set the Routing.prefixes keys to false if they aren't already set in the url call.

You would then need to specify the prefix in your admin links every time.

日久见人心 2024-11-06 02:48:08

HtmlHelper 接受两种提供 URL 的方式:它可以是 Cake 相关的 URL 或 URL 参数数组。

如果使用 URL 参数,则默认情况下如果不指定 'admin' => false 参数,如果您正在进行管理操作,HtmlHelper 会自动在操作前加上“admin”前缀。

恕我直言,摆脱此参数的最简单方法是使用 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

The HtmlHelper accepts two ways of giving the URL: it can be a Cake-relative URL or an array of URL parameters.

If you use the URL parameters, by default if you don't specify the 'admin' => false parameter the HtmlHelper automatically prefixes the action by 'admin' if you are on an admin action.

IMHO, the easiest way to get rid off this parameter is to use the Cake-relative URL as a string.

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

//use
echo $this->Html->link(__('logout', true), '/users/logout');

Kind regards,

nIcO

绾颜 2024-11-06 02:48:08

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

$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');

这真的很令人沮丧,所以我很高兴能提供帮助。

I encountered this problem this week and this code seemed to fix it. Let me know if it doesn't work for you and I can try to find out what else I did to get it to work.

$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');

This was really frustrating, so I'm glad to help out.

情话墙 2024-11-06 02:48:08

我迟到了,但我有一个很好的答案:

您可以通过创建 AppHelper 类来覆盖默认行为。创建 app/app_helper.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。

I'm late to the party, but I have a very good answer:

You can override the default behavior by creating an AppHelper class. Create app/app_helper.php and paste the following:

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

Unless it is specified when you call link() or url(), admin will be set to false.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文