Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to get current controller and action name? #1031

Closed
askie opened this issue Oct 25, 2013 · 12 comments
Closed

how to get current controller and action name? #1031

askie opened this issue Oct 25, 2013 · 12 comments

Comments

@askie
Copy link

askie commented Oct 25, 2013

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

@uhho
Copy link
Contributor

uhho commented Oct 25, 2013

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.

@askie
Copy link
Author

askie commented Oct 25, 2013

thx

@askie askie closed this as completed Oct 25, 2013
@kokujin
Copy link
Contributor

kokujin commented Oct 26, 2013

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

@uhho
Copy link
Contributor

uhho commented Oct 26, 2013

@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!

@randallmeeker
Copy link

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

@clark0x
Copy link

clark0x commented Jun 5, 2014

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.

@randallmeeker
Copy link

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.

@echorohit
Copy link

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??

@lucasmonstrox
Copy link

Hello Guys, news?

@summit2020
Copy link

@clarkorz Thanks :)

@jdzl
Copy link

jdzl commented Sep 25, 2017

with request you can get info

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

@mikermcneil
Copy link
Member

mikermcneil commented Jun 9, 2020

In Sails ≥v1, from a view or layout.ejs:

<%= req.options.action %>

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

No branches or pull requests

10 participants