Iron Router: Get the name of the current controller - meteor

I know you can get the name of the current route's name with Router.current().route.getName(), but how can I get the name of the current route's controller?
Here is an example of how I define my controller(s):
Router.configure
layoutTemplate: 'LayoutFluid'
yieldRegions:
"footer": {to: "footer"}
"header": {to: "header"}
#QuantifyController = RouteController.extend
layoutTemplate: "LayoutSidebar"
yieldRegions:
"footer": {to: "footer"}
"header": {to: "header"}
"QuantifyMenu": {to: "sidebar"}
action: ->
#render()
Router.map ->
#route "Home",
path: "/"
#route "Blog",
path: "/blog"
#route "QuantifyIndex",
path: "/quantify"
controller: "QuantifyController"
#route "QuantifyNewProject",
path: "/quantify/new"
controller: "QuantifyController"
#route "Quantify..." #you get the idea
FWIW, the reason I need to get this is for CSS purposes. I am adding the route's name as a css class to the body, which means that when defining shared styles for views that use the QuantifyController, I need to do body.QuantifyIndex, body.QuantifyNewProject, body.Quantify... which means every time I add a new Quantify.. route I need to also add it to the css which is simply not ideal. If I can get the controller name I can just use body.QuantifyController {...} in the CSS which is much more ideal.

Got it! After hours of searching, I found it defined at:
Router.current().route.options.controller.
Note that if the current controller is the default controller (RouteController) you will get undefined.
#SG_ your comment helped me get there. thanks again.

Related

Best way to change routes loading order

I have routes in my controller loaded in /config/routes/annotations.yaml
controllers:
resource: ../../src/Controller/
type: annotation
I have routes in /config/routes.yaml
about:
path: /about
controller: Symfony\Bundle\FrameworkBundle\Controller\TemplateController::templateAction
defaults:
template: front/about.html.twig
...
Dynamic routes in "controllers" are overriding my "static" routes.
What is the best way to load "static" routes before those in controllers.
I made it work by commenting the content in /config/routes/annotations.yaml and pasting it in the end of /config/routes.yaml but I don't feel it's the best way to do it...
Technically this might not be enough though. Any route loaded after this will be ignored, and if you are using annotations, you have to put this action in the last action of the last controller sorted alphabetically.
Configure this route in yml, and put it at the end of routes.yml.
This route will be the last to execute (performance hit), and it will catch all requests, so make sure you throw 404-s properly.
(Am I right in assuming that the client wants to be able to configure routes completely? eg CMS pages? Had that situation a couple of times)
It didn't want to change my url nor using the alphabetical trick as suggested in the comments.
I fixed it by changing the order of imported routes in the kernel.
Instead of:
protected function configureRoutes(RouteCollectionBuilder $routes)
{
$confDir = $this->getProjectDir().'/config';
$routes->import($confDir.'/{routes}/*'.self::CONFIG_EXTS, '/', 'glob');
$routes->import($confDir.'/{routes}/'.$this->environment.'/**/*'.self::CONFIG_EXTS, '/', 'glob');
$routes->import($confDir.'/{routes}'.self::CONFIG_EXTS, '/', 'glob');
}
I
protected function configureRoutes(RouteCollectionBuilder $routes)
{
$confDir = $this->getProjectDir().'/config';
# routes loaded in routes.yaml
$routes->import($confDir.'/{routes}'.self::CONFIG_EXTS, '/', 'glob');
# routes loaded in routes/annotations.yaml
$routes->import($confDir.'/{routes}/*'.self::CONFIG_EXTS, '/', 'glob');
$routes->import($confDir.'/{routes}/'.$this->environment.'/**/*'.self::CONFIG_EXTS, '/', 'glob');
}
You can avoid changing the kernel logic by putting the controller with annotations after the static route in routes.xml:
browserconfig:
path: /browserconfig.xml
controller: Symfony\Bundle\FrameworkBundle\Controller\TemplateController::templateAction
defaults:
template: browserconfig.xml.twig
app_document:
resource: App\Controller\DocumentController
type: annotation

Custom fields in a meteor route

I have my a number of routes inside my routes.js file and i want to introduce an extra field into it. Below is the route
Router.route('/', {
name: 'home',
controller: 'LoginController',
where: 'client',
action:'index'
});
Since i have a number of routes,i want to go through all the routes and get the route name like this
_.each(Router.routes, function(route){
console.log(route.getName());
});
I want to use the route names to generate links. Links require link names and i have the idea of putting the link names in the routes.
Router.route('/', {
name: 'home',
controller: 'LoginController',
where: 'client',
text: 'Login Link',
action:'index'
});
Is introducing custom fields in the routes allowed in meteor?.
I found out there is a title option but its not in the docs http://iron-meteor.github.io/iron-router/#route-specific-options
and used it this way
Router.route('/', {
name: 'login',
controller:'HomeController',
where: 'client',
title: 'login',
icon: 'this is the icon',
action:'index'
});
and to get the options
Router.routes.forEach(function(route){
console.log(route.getName());
console.log(route.options.title);
console.log(route.options.icon);
});
and this is the result
login
login
this is the icon
so even the custom option icon seems to be working.

Custom views (or routes) in FOSRestController

I am using FOSRest Bundle to implement Rest API's.
I have the function getTermsAction() which returns all the terms from the DB.
I have a template listTerms.html.twig which shows the list of terms.
I want to implement another function listTermsAction() which will render this template and return it.
Something like this (I added this to my controller)
/**
* #Route("/listTerms",name="listTerms")
*/
public function listTermsAction()
{
$view = $this->view(null,200)
->setTemplate("TermsBundle:Default:listTerms.html.twig");
return $this->handleView($view);
}
I tried to this by adding the following code in routing.yml file
list_terms:
pattern: /terms/listTerms
defaults: {_controller:TermsBundle:Terms:listTermsAction}
But it doesn't work!
It just shows me "null" in the browser
The Solution i have given works but that snippet should be above the rest route in my routing.yml
That is routing.yml should be like
list_term:
pattern: /terms/listTerms
defaults: {_controller:TermsBundle:Terms:listTerms}
terms:
resource: Madhuri\TermsBundle\Controller\TermsController
type: rest
prefix: /
list_term route should be above terms route

Match onBeforeAction to only a group of routes using Iron Router for Meteor

Using Iron Router for Meteor, I know I can use the parameter 'only' or 'except' to match named routers when defining an onBeforeAction, like the example in the guide:
Router.onBeforeAction(myAdminHookFunction, {
only: ['admin']
});
Imagine I have a lot of views not needing this on before action, like public pages, and a lot needing it, like admin panel. Is there any way to apply this onBeforeAction to a group of routes without explicit list each one? I mean, for example, apply to all routes starting with '/admin', something like:
only: ['/admin/*']
Or maybe apply the onBeforeAction to a parent route and then define nested routes? (I can't find if Iron Route supports nested routes), something like:
Router.route('/admin/', {
name: 'admin',
onBeforeAction: function() {},
routes: [
{name: 'users', path: '/admin/users', action: function() {}},
{name: 'posts', path: '/admin/posts', action: function() {}}
]
})
I would suggest creating controllers. Create a "base" controller and extend it wherever needed.

Iron-router & PathFor

When I am using route without params, I've got normal pathFor in my template. But when I am adding some "slug", like this:
#route "pagesSlug",
path: "/page/:_slug"
name: "page"
And got error in console:
You called Router.path for a route named page but that route doesn't seem to exist. Are you sure you created it?
No change when I add/remove name.
I've got empty {{pathFor ... in my template, and urlFor too empty.
In this case:
#route "articlesList",
path: "/articles"
waitOn: ()->
Meteor.subscribe 'articles'
I've got href="/articles" - no problem.
Update: Meteor 0.9.4, and 0.9.4 of Iron-Router.
The first parameter of the route function is the name of the route. Your route name is pagesSlug
To get the path by the pathFor helper, you need to also pass the parameter _slug
{{pathFor "pagesSlug" _slug="xxxx"}}

Resources