CakePHP - /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/... 的网址才能转到管理员,而不是所有其余的链接?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
扩展用户 Abba Bryant 的编辑,看看如何在烹饪书中创建助手: http://book.cakephp.org/view/1097/Creating-Helpers
如果必须在所有链接上手动禁用路由是一件烦恼(这对我来说!),您可以创建一个新的帮助器< code>MyCustomUrlHelper (名称当然不必那么长),并让它使用核心 UrlHelper 为您生成 URL。
基本上,您可以根据需要使其变得冗长或简洁。它只是实际 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.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.
确保您是否使用在 HtmlHelper::link 调用中处理它的前缀路由,如下所示
** 编辑 **
您可以扩展 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
** 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.
HtmlHelper 接受两种提供 URL 的方式:它可以是 Cake 相关的 URL 或 URL 参数数组。
如果使用 URL 参数,则默认情况下如果不指定 'admin' => false 参数,如果您正在进行管理操作,HtmlHelper 会自动在操作前加上“admin”前缀。
恕我直言,摆脱此参数的最简单方法是使用 Cake 相对 URL 作为字符串。
亲切的问候,
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.
Kind regards,
nIcO
我本周遇到了这个问题,这段代码似乎解决了它。如果它对您不起作用,请告诉我,我可以尝试找出我还做了什么来让它发挥作用。
这真的很令人沮丧,所以我很高兴能提供帮助。
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 was really frustrating, so I'm glad to help out.
我迟到了,但我有一个很好的答案:
您可以通过创建 AppHelper 类来覆盖默认行为。创建 app/app_helper.php 并粘贴以下内容:
除非在调用 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:
Unless it is specified when you call link() or url(), admin will be set to false.