Iron Router - Get Route URL as Variable/String - meteor

In Iron Router, I can get the URL of the route and redirect by doing...
Router.go('ROUTE_NAME', { param: parm })
This returns the url (i.e. /whatever/whatever) and then redirects to that url.
How can I get JUST the URL and NOT redirect?

You can access the route object directly and ask for the path:
Router.routes['ROUTE_NAME'].options.path
or
Router.routes['ROUTE_NAME'].path()
or, if you want the absolute URL:
Router.routes['ROUTE_NAME'].url()

If you want the curent url:
Router.current().url

this is how to process a route and get it as string :
> Router.url("your.url.with.:param1.:optional?", {
param1: "azertyui",
optional: "qsdfghjk"
});
"http://localhost:3000/your/url/with/azertyui/qsdfghjk"

Related

Different domain and url shortener for asp.net mvc app

I would like to implement a simple URL shortener feature like Bitly.
My Controller name: WebController
My Action name: Redirect
As the name suggests the action redirects the user from the short URL to the full URL.
To call this action I need: https://myappdomain.com/web/redirect?id=3422
But I would like to be able to call this feature in a much shorter way with a different (shorter) domain and without the need to call the action name: https://shorterdomain.com/3422
Can you guide me how can I do this? I am a bit lost even for what to search for:(
Add a route to the shorter URL so MVC knows what controller and action will handle the request. Something like this:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(name: "redirection",
pattern: "{id:int}",
defaults: new { controller = "web", action = "redirect" });
... your existing routes
});

How to redirect from `/` to `/foo/<id>` using FlowRouter and MeteorJS?

In my scenario, I want everyone that visits our root URL to be auto-redirected to a url containing a document for collaboration and instant gratification.
Here, the router.coffee code is:
FlowRouter.route '/',
action: ->
console.log "I'm home!"
FlowRouter.go 'myProject'
name: 'myHome'
FlowRouter.route '/my/:projectId',
subscriptions: (params) ->
#register 'currentProject', Meteor.subscribe 'project', params.projectId
action: ->
BlazeLayout.render 'myBody'
name: 'myProject'
I want the root URL to redirect to /my/:projectId but I'm unsure of how to retrieve the auto-generated projectId and redirect using with either FlowRouter.go or FlowRouter.redirect.
Is this possible?
If yes, how?
Thanks for your help!
Since the data may not be available when the route action execute,
the best is to re-route at the template level.
It might be a good idea to use the Template.[name].onCreated() function
and put inside it something like the following code:
pID = ... // Get the user project ID from wherever you saved it
var params = {projectId: pID};
// Set the project URL including the :projectId parameter and re-route the user
FlowRouter.go("myProject", params);

Confusion over creating routes in Iron Router

I have a route defined as such:
Router.route('/posts/:_id/:commentsLimit?', {
name: 'PostTemplate',
controller: PostTemplateController
});
My question is why when I define a new route for editing the post, it is getting redirected to the route above?
Router.route('/posts/edit/:_id', {
name: 'PostEditTemplate',
controller: PostEditTemplateController
});
The url /posts/edit/anIdOfSomeKind is matched by both your routes. In these cases, Iron Router will pick the route that matches the url and was created first. So changing the order in which your routes are created will probably fix your problem.

Symfony 2 redirect route

I have the following route that works via a get:
CanopyAbcBundle_crud_success:
pattern: /crud/success/
defaults: { _controller: CanopyAbcBundle:Crud:success }
requirements:
_method: GET
Where Canopy is the namespace, the bundle is AbcBundle, controller Crud, action is success.
The following fails:
return $this->redirect($this->generateUrl('crud_success'));
Unable to generate a URL for the named route "crud_success" as such route does not exist.
500 Internal Server Error - RouteNotFoundException
How can I redirect with generateUrl()?
Clear your cache using php app/console cache:clear
return $this->redirect($this->generateUrl('CanopyAbcBundle_crud_success'));
If parameters are required pass like this:
return $this->redirect($this->generateUrl('CanopyAbcBundle_crud_success', array('param1' => $param1)), 301);
The first line of your YAML is the route name that should be used with the router component. You're trying to generate a URL for the wrong route name, yours is CanopyAbcBundle_crud_success, not crud_success.
Also, generateUrl() method does what it says: it generates a URL from route name and parameters (it they are passed). To return a 403 redirect response, you could either use $this->redirect($this->generateUrl('CanopyAbcBundle_crud_success')) which is built into the Controller base class, or you could return an instance of Symfony\Component\HttpFoundation\RedirectResponse like this:
public function yourAction()
{
return new RedirectResponse($this->generateUrl('CanopyAbcBundle_crud_success'));
}

trying to use params on main route (/) of my meteor app

I'm trying to use params on my main route. But in fact the params are not set and they are used in the path :
Router.map(function funcClientRouterMap(){
this.route('home', {
path: '/:_redirect?',
action: function funcClientRouterMapAction(){
console.log(this.path, this.params);
}
})
});
now if i try manual redirection here is what i get :
Router.go('home'); // it redirects on / => ok
Router.go('home', {_redirect: test}); // this.path = /test, and this.params is empty
How can i use _redirect like a params and not a route ?
Thanks
Router.go accepts a path as its first argument (per the docs). So if you were trying to programmatically cause the same result as the user going to /redirectMeSomewhere, you just use:
Router.go('/redirectMeSomewhere');
And this.params._redirect should be 'redirectMeSomewhere'.
Note that like #apendua implies, if you have other routes it could cause chaos to have a route defined as /:anything, because the other routes may never get triggered. If the above doesn't do the trick, try commenting out all your other routes to see if that changes anything.

Resources