How to get the current route on server? - meteor

I am using Iron Router and Router.current().route.getName() to get the route name. Router.current() is only defined on the client.
How to get the current route name on server ?

Related

How to request for new traffic information using Route Handle (Here Routing API V8)

Currently I am referring to this document (https://developer.here.com/documentation/routing-api/api-reference-swagger.html) and am able to send request to calculate Route by giving route Parameters as shown below:
// Get an instance of the routing service version 8:
var router = platform.getRoutingService(null, 8);
//routing parameters
var routingParameters = {
'routingMode': 'fast',
'transportMode': 'car',
// The start point of the route:
'origin': '52.550028,13.302349',
// The end point of the route:
'destination': '58.553272,13.305195',
// Include the route shape in the response
'return': 'polyline,typicalDuration,summary,routeHandle'
};
// Call calculateRoute() with the routing parameters,
// the callback (onResult) and an error callback function (called if a
// communication error occurs):
router.calculateRoute(routingParameters, onResult,
function(error) {
alert(error.message);
});
However, if I need to use route handle for updated traffic information for already calculated route, how do i need to request ? Do we have any methods for using route handle like we use calculateRoute method for calculating the new route.
A route handle encodes a previously calculated route. A route can be decoded from a handle as long as the service uses the same map data and encoding that were used when retrieving the handle.
Thus it is suitable for caching routes compactly. It can be used to retrieve updated traffic information or other data along the route. However, a user should be prepared to recalculate the route when decoding the handle fails.
https://router.hereapi.com/v8/routes/{routeHandle}
use route handle to retrieve updated traffic information or other data along the route.
traffic
object (Traffic)
Traffic specific parameters.
link for more details : https://developer.here.com/documentation/routing-api/api-reference-swagger.html

How to get the client IP inside a Route

I'm trying to retrieve the customer's IP address during Iron Router's routing process. I have a server-side function for this (getIP), however the "waitOn" function inside the route won't wait for the server function to return:
waitOn: function () {
Meteor.call('getIP', function(error, clientIp){...}}
Can I force it to wait, or can I get the IP in any other way?
According to the documentation, the waitOn hook must return a Handler, a function or an array. the reason why it's not working for you, is that Meteor.call on the client is always async, and you have to define a callback function, which is called when the method responds.
Given that nature, you could only use the Meteor method, if the waitOn code supported a Promise, that could be resolved on the method callback.
The only way I see this is the following:
use a Meteor.onConnection hook, and store the current IP Address of the user in the user's profile (Meteor.users collection)
setup a global Subscription that publishes the entire user profile (since by default Meteor.user only publishes a few default document fields).
on the route waitOn, query the Meteor.user collection, and you will see the current detected IP Address of that user
I hope this helps and that it works for you.

Symfony 2.8 how to get Route option

With EventListener I use my lass TransactionListener. This class check permission for current url. Some URL are open. I check it by attribut "option" in routing.yml
For getting option on current route I use very slow method
$route = $this->router->getRouteCollection()->get($e->getRequest()->attributes->get('_route'));
Do I have other opportunity to get option-datafrom current route? How to get fully route-object from current route name or request?

Symfony redirect to dynamic route name

I'm using the Symfony CMF Routing Bundle to create dynamic routes (I'm using one example here):
$route = new Route('/dynamic-url');
$route->setMethods("GET");
$route->setDefault('_controller', 'AppBundle:MyRoute:getResponse');
$routeCollection->add('my-dynamic-route', $route);
The response is loaded from the getResponseAction() function inside the MyRouteController:
/**
* No annotations here, because I want the url to be dynamic from the database
*/
public function getResponseAction(Request $request) {
return $this->render('dynamic-page-template.html.twig');
}
When I go to '/dynamic-url', it works.
When in another controller, I want to redirect to this dynamic route, like this:
return $this->redirectToRoute('my-dynamic-route');
But I get this error: "None of the chained routers were able to generate route: Route 'my-dynamic-route' not found"
Also interesting: when I go to '/dynamic-url', the dev bar actually says that the Route name is 'my-dynamic-route'.
Edit
When I load all the routes, I don't see my dynamic route names:
$this->get('router')->getRouteCollection();
I think they should be in this list.
Since it's a dynamic route, which wasn't saved anywhere (like routing.yml ) it will be only availabe for Request where it has been defined. So at the end of Request your app will immediately "forget" about new Route generated at runtime.
When I load all the routes, I don't see my dynamic route names:
$this->get('router')->getRouteCollection();
I think they should be in this list.
Actualy No. It depends on where you call $this->get('router')->getRouteCollection();
Just try to call
dump($this->get('router')->getRouteCollection();)
right before the return statement in your Action where you're adding the my-dynamic-route route. I'm sure you'll see your my-dynamic-route in the list of routes, but if you call it anywhere else - you won't see it.
It's less about symfony rather about stateless nature of web (see Why say that HTTP is a stateless protocol?)
I started to think about this and pointed your question to an routing issue on symfony-cmf. You tagged with #symfony-cmf and i think this would be important feature for us.
I also think, when you persist your route with /my-route you should also ask the router for that name (or in case of the CMF with an content object with that a route.)
If you use the CmfRoutingBundle dynamic router, you should persist your routes to doctrine. The idea of dynamic here is that they can be created at runtime, e.g. in an admin interface or elsewhere by code. The DynamicRouter loads routes from the database.
If you use PHPCR-ODM, the route name is the repository id, typically something like /cms/routes/my-route and you can generate it with that name. If you have access to the route object loaded from the database, you can also generate from that, rather than the path. If you have to hardcode a path in your application, its an indication that probably a normally configured route would be better.
If you just add a route to the route collection on the fly, you would have to make sure that happens in each request, its only available when you add it. With the CMF ChainRouter you could register your own router that does that, but i would not know of a good use case for that. Either you know the route, then you can configure it in the routing.xml|yml|php config file. Or routes are loaded dynamically, in which point you should use DynamicRouter, potentially with a custom route loader.

Router.current().route.getName() is returning an error server side

Router.current().route.getName() is returning an error when I use it in a method call (server side method). I thought they say Iron-Router is supposed to work both client and server side. The error I get is
Error invoking Method 'mySeverSideMethod': Internal server error [500]
Please help.
You are half way right, the router works on both client and server. However the server-side implementation is meant for server side routes (eg. REST endpoints). There is no "state" sharing between client/server with iron:router (when invoked inside methods), so Router.current().route.getName() is going to throw you this error, because Router.current() is undefined.
Yes, iron:router can create server side routes, but that api is client only
From the docs:
Router.route('/download/:file', function () {
// NodeJS request object
var request = this.request;
// NodeJS response object
var response = this.response;
this.response.end('file download content\n');
}, {where: 'server'});
You have access to the NodeJS request object so you should be able to find what you need there, e.g. this.request.route, this.request.path.
When calling a Method, you're not going through a 'route' as defined by Iron-Router: it's a route defined by the Meteor framework. It does not care what route the client is on.
So, if you need to know from what page the client is calling the endpoint, you should pass it as a parameter to the Method.
Meteor.methods({
"myEndPoint": function(route) {
// use route here.
return //something
}
})

Resources