how to get current controller and action name? #1031

askie opened this Issue Oct 25, 2013 · 9 comments


None yet

7 participants


I want to get current controller and action name string,
is that a method?


Yes, controller name and action are stored in in following format: = {
    controller : 'XXX',
    action: 'XXX'

You can access this variable both on back end and front end.



@askie askie closed this Oct 25, 2013

This is interesting, how can this value be accessed in the front-end Lukas?


You can access it the same way as any other JS variable you're setting in the controller.
The difference is, by default this variable is exposed on the front end (no need to set it via res.view({...})).

For example, let's say you've got a link and you want it to be highlighted only on particular controller/action:
In case of EJS templates it will be:

<a href="#" class="<%= ( === 'index') ? 'highlighted' : '' %>">Link</a>

Hope that helps!


did this go away? I'm using the latest version and don't have target on the req object.


try req.options.controller and req.options.action. But it seems that they only exist for custom blueprints action and custom routes. So if you have problem, add a route in config/routes.js.


That is the issue. If it is a custom action and/or route then I already know my controller / action. I was looking for this because I wanted to do a pre-flight check in a policy that ran before all my blueprint actions. I needed the action being preformed on the model.


How to get the controller and action name for request.referrer in sails. sails.log(req.referrer); gives me controller/action. How to get controller and action name from req.referrer directly??


Hello Guys, news?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment