In AngularJS I used ui-router for redirecting inside of my app(changing state).
It has 2 possible options to redirect
In template ui-sref='stateName'
In controller $state.go()
I just start play with Angular (4) and I found only way how to change route from template with something like:
Template routerLink="routePath"
Is there some way as there was in ui-router to change route from component?
constructor(private router:Router) {}
changeRoute() {
this.router.navigate(...)
// this.router.navigateByUrl(...)
}
See also https://angular.io/docs/ts/latest/api/router/index/Router-class.html
you can navigate via router like this
router.navigate([URL])
or like this
router.navigateByUrl('whole_string_containing_full_path')
Here router is an instance created in constructor, which is imported from router firstly, basically there is no difference between these two
First one accept array where as second one accept url in the form of string.
Related
I am new to .net core and not very good with the routing understanding. I am trying to make api calls using the route template as follows
routeTemplate: "api/{controller}/{action}/{id}"
When I created the .net core project of api type, it added a controller with the name of Values and the way its accessing the api calls is placing the routes on top of the controller.. [Route("api/[controller]")]
How can I set my controller to use the action step?
I basically want to be able to make calls to my api like this
https://localhost:44345/api/MyProcessor/getValues
https://localhost:44345/api/MyProcessor/AllDefinations
Where MyProcessor is the name of the controller.
You want to make calls to your api like:https://localhost:port/api/[controllerName]/XXX.
1.If the last XXX is defined to action. There are two options as
follow:
Change the [Route] template:
[Route("api/[controller]/[action]")]
Change [HttpGet] which is based on [Route("api/[controller]")]:
[HttpGet("[action]")]
2.If the last XXX is defined to any other name. You could change the [HttpGet] which is based on [Route("api/[controller]")]:
[HttpGet("getValues")]
I have admin part in my project which use React.js and React router.
And I need to render the same view on each request which starts with /admin in case user hit Refresh or come straight to url.
I don't need to pass any parameters to view. How can it be done?
You can define your route like this in play:
/admin/*url controllers.Application.admin(url)
How can I use the url() controller helper inside the console?
I have to generate some urls using an action of the console, but if I call $this->controller->url()->fromRoute(...);, I can only ask for routes defined in the console router.
In other words, I need the ability to call all the routes of the application's main router.
Thanks
I'm not sure, that it is a good decision, but you can change router manually:
// ConsoleController.php
// Change router to HTTP
$this->getEvent()->setRouter($this->getServiceLocator()->get('HttpRouter'));
// Get any HTTP route
var_dump($this->url()->fromRoute('your_http_route'));
// Change it back, if you want
$this->getEvent()->setRouter($this->getServiceLocator()->get('Router'));
I'm writing a a meteor application right now, and am getting used to the new Iron Router package (as opposed to the router package that I used before).
I have a collection of routes that should only be accessible by users with specific properties. To be specific, I'm using the Roles package. My way of achieving this at the moment is to define a before function that runs a conditional, and redirects to a login or error page if the user doesn't have the proper role. Here's just a quick (coffeescript) example:
this.route 'home',
path: '/'
template: 'dashboard'
before: ->
unless Meteor.userId()
this.redirect 'userUnauthorized'
do this.stop
My question is, is there a better way to do this? It seems like there should be some way to add a permission set, and apply that permission set to a route as opposed to writing the access conditionals for every route.
You could setup a global "Before" in your routes. I'm not using roles in my current app, but we are redirect users globally based on whether or not they are signed in.
You could probably do something like this:
Router.before(function() {
unless(Meteor.userId()) {
this.redirect('userUnauthorized');
do (this.stop)
}
}, {except: ['userUnauthorized', 'otherPageUnauthorizedUsersAllowedToSee']});
We use something similar right below our Router.configure()
Short question, tried finding an IRC for sails for such a quick one but got lost so here goes. I have a controller with a route of '/userposts'. I know sails offers some default REST-like functionality without backend code needed but what if I want to overwrite the default POST action what would I do?
I'm forced to write a POST route such as post /userposts/create or I can overwrite the default action and post straight to /userposts which will identify my overwriting and execute it.
I hope I'm making sense. I basically want to create a custom POST route and be able to
socket.post('/userposts', {title: "Foo", content: "Bar"}, function(response){});
I tried with create but it doesn't get executed on a post to /userposts
Sails.js provides blueprints out of the box.
These blueprints provide you with the following CRUD routes that are enabled by default
/:controller/find/:id?
/:controller/create
/:controller/update/:id
/:controller/destroy/:id
To modify the default functionality for your controllers, look at the settings in config/controllers.js
Your routes are defined within config/routes.js, in your case you have a model UserPosts and a corresponding controller named UserPostsController.
In your UserPosts controller, create a function createPost and specify the route(s) to this method
'POST /userposts/create': 'UserPostsController.createPost'
which is shorthand for
'POST /userposts/create': {
controller: 'userposts',
action: 'createPost'
}
you can also override the /:controller route
'POST /userposts': 'UserPostsController.createPost'
These routes will map any POST requests made to the createPost function.
For more information, be sure to check out the Sails.js documentation