Why is arg() failing to get the router object on pages with aliased URLs? So I posted this question about how to get NID of current node and I'm assured that arg() should work to return the router item for the current page, regardless of aliasing. However, it doesn't work.

The relevant snippet is 

if(arg(0)=='node' &amp;&amp; is_numeric(arg(1))){ 
   $nid = arg(1); 
   dsm($nid);
}


I've also tried

$load = node_load();
$dsm($load);


and

$item = menu_get_item();
dsm ($item);


and 

$node = menu_get_object();
dsm($node);


For all of these methods, they work when I edit the page (and thus make the URL displayed become /node/%/edit) and when I just navigate to /node/% (where % is the NID) but they display either nothing or (in the case of arg()) only the alias (e.g. "about-our-company for the path alias /about-our-company).

How can I reliably get the NID of the current node?
arg() is intended to work with system paths. if you are viewing you node page with system path (node/#) or by alias then arg(1) must return nid of the node.

How are you defining your alias? Are you using path module interface?
Perhaps the alias is a result of a custom menu hook by some module, rendering a node by some arbitrary logic.  

I would also like to share a hack to overcome this issue if you get alias in arg() and this alias is truly defined by path module directly on node then drupal_lookup_path('source', 'your/node/alias') will return the system path of your node e.g. node/#