Sails: how to get current controller and action name?

Created on 25 Oct 2013  路  12Comments  路  Source: balderdashy/sails

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

Most helpful comment

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.

All 12 comments

Yes, controller name and action are stored in req.target in following format:

req.target = {
    controller : 'XXX',
    action: 'XXX'
}

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

thx

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

@kokujin
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="<%= (req.target.action === '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?

@clarkorz Thanks :)

with request you can get info

options:
   { action: 'nameAction',
     controller: 'nameController',
     .....................
function(req,res) {
//use
req.options.controller
req.options.action
}

In Sails 鈮1, from a view or layout.ejs:

<%= req.options.action %>
Was this page helpful?
0 / 5 - 0 ratings

Related issues

visitsb picture visitsb  路  4Comments

radoslavpetranov picture radoslavpetranov  路  4Comments

MelwinKfr picture MelwinKfr  路  4Comments

Noitidart picture Noitidart  路  4Comments

anissen picture anissen  路  3Comments