Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Best method to determine if request is in cms / admin


Go to End


4 Posts   3691 Views

Avatar
brice

Community Member, 52 Posts

4 October 2011 at 4:53pm

Edited: 04/10/2011 4:53pm

I'd like to decorator to augment searchable fields only if the request is within the cms / admin section of the site. So far I do the detection as follows;

if(substr(Controller::curr()->getRequest()->getURL(), 0, 6) == 'admin/')

There's a few flaws I see in this;

1. It is long.
2. It is hardcoded & I don't account for the admin section changing from 'admin/'. (e.g. it doesn't use a static method to determine the actual base CMS url).

Is there a better & more recommended method?

Thanks!

Avatar
MarcusDalgren

Community Member, 288 Posts

4 October 2011 at 8:20pm

I've no idea if it's the recommended way of doing it or not but I use

if (is_subclass_of(Controller::curr(), "LeftAndMain"))

This won't break if the URL changes but if the base class changes you're screwed.

Avatar
swaiba

Forum Moderator, 1899 Posts

4 October 2011 at 11:37pm

I have decorated the LeftAndMain class to manipulate certain things. I would register if I have passed through there in order to tell if I was in the admin section.

since you might read this - Brice - I' really appreciate a response on your excellent looking sortweight module - I've been trying to use it on a fresh 2.4.5 install with ModelAdmin - if it is designed for that (or I'm missing something basic) please let me know...
http://www.silverstripe.org/all-other-modules/show/14921?start=8

Barry

Avatar
Jare

Community Member, 39 Posts

14 February 2016 at 2:18pm

I'm just asking if the solution given by MarcusDalgren is still the best way for SS3? I have run into this question multiple times now and wonder that it would make sense that the framework would have this functionality built in. At least the solution still works in SilverStripe 3.1.16.