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
Ability to add non dynamic parameters in a route #821
Comments
Will query parameters not work for you? So given: module.exports.routes = {
"/a": {
controller: "MyController",
action: "index"
}
}
|
No, the urls are not the same.
|
Is adding a request parameter to the URL an option? eg |
I know the feature, but I don't want dynamic parameters. |
Could you parse I just did a test in my controller to console.log that attribute and it showed |
I assume your controller could be something like
|
It is what I did actually, but it's not nice. |
I guess I don't quite understand the use case. If you want two different routes to do two different things, why not write two different controller actions for them instead of having them both use "index"? If you need them to share some functionality, you can use services. |
For example, I have 10 custom urls for landing pages. They are the same but with some variations. |
A policy could do multiple little tweaks off the request.route and then |
Or you could still use dynamic parameters for this. Putting this at the bottom of your routes file:
will give you access to "req.param('type')" in your action. Of course, this route matches everything, and will override blueprint actions for models. To get around that, you can either check the value of "type" in your action method against the list of allowed landing page types and do "get /_:type": "MyController.index"` so that going to "/_a" will trigger the action and |
I think the policy is the best choice but it's weird in all cases. |
My request is really invalid ? If I use a policy, I will parse the url too, it's bad. |
No offense intended. Your use case is understood--we've just given you several options for how to do this. I'm not sure what it is you don't like about dynamic parameters, but they exist to give you the functionality you're asking for. The idea is to keep logic out of the routes config file where it doesn't belong. If you want to have an arbitrary number of routes all doing the same thing with minor variations, you can have one route for the whole lot, and then manage the differences in the Controller. If you were managing a membership site with vanity URLs, for example, you'd want to manage the custom URLs (aka slugs) in a database, not in routes.js. |
I'm ok with dynamic parameters but as you said, my urls are in the root of the website and it's problematic. I want to manage the differences in the controller but I don't want to parse the URL because it is the responsability of the router. |
As long as you put the route at the bottom of your routes.js file, using dynamic parameters is only an issue for model blueprints, or if you're serving static assets from the root (don't do this). The blueprint problem is easily handled, either in your routes.js by explicitly writing the blueprint routes:
or in your Controller, especially if you have a predefined set of values for "type", as you do:
|
Ok, it's a solution but it seems like a trick. Even if I send a pull request to handle default/static parameter values, you will not accept it, right ? |
The router in Sails is just the router from Express. It's purpose is to parse the URL based on a set of regular expressions and find a matching function to handle it. The wildcard route above: This gives you:
@sgress454 solution works great and doesn't seem like a trick at all. You are simply checking if it is a valid landing page or not and if not sending it off to see if the request matches anything else in the middleware stack. |
I don't see And if I want Imagine the urls are localized. I don't want to use a switch in the controller with all the possibilities for languages. |
Alright I have a better idea of what you are trying accomplish now. I thought about it and came up with some code I think will make everyone happy. We don't have to mess with the router and you get a nice dictionary of routes that map to a controller with a key. https://gist.github.com/particlebanana/6445791 Using the example of routes by language I put together this gist you can play with. It uses a dictionary file that functions similar to the way i18n works but without the policy to detect region. You can add that later if you want. You can map out pages to a controller and action and then add all the various language translations in a single place. If you look at languageMap.js you can see what I mean. Then in the I know it's not your ideal solution but let me know what you think after playing with it a bit. You can just paste that file into bootstrap.js and add your language map and it will work. |
I don't doubt there are several solutions. My example of the localized urls is a special case I think. I don't want to skip the routes.js. It's a configuration file to route URLs to controller actions. A solution where I have to parse the URL is not nice because it is the responsability of the router. |
For anyone coming accross this for a solution. This is completely possible now. Like the original request states, add the data to the route's object like so
Then, in the controller you can access the data from I havn't come accross this personally but i'd make sure the key you are putting your data under dosn't clash with any existing data in the options object. |
For example:
The text was updated successfully, but these errors were encountered: