Drupal l() and url() methods

Both l and url methods are handy to create urls for various entities. However, the variety of options available makes the usage exciting and complex.

l(): Formats an internal or external URL link as an HTML anchor tag.
Syntax: l($text, $path, array $options = array())

Creating an absolute URL using l(). The example below creates a node link.

<?php
 l
(t('New product'), 'node/123', array('absolute' => TRUE));
?>

Add the previous url or the current page url before going to the new page. This is important if you go to an edit page link and would want to come back to the listing page.

<?php
 l
(t('Another product'), 'node/123', array('query' => drupal_get_destination()));
?>

Open link in a new tab.

<?php
 l
(t('Another product'), 'node/123', array('attributes' => array('target' => 'blank')));
?>

Query Parameters:

<?php
l
(t('Add Product '), 'node/'.$args.'/edit', array(
   
'query' => array(
     
'destination' =>'node/'.$args,
    ),
  ));

l(t('Add new CBC '), 'node/add/test-report', array(
     
'query' => array(
       
'field_tr_patient_ref' =>$args,
       
'field_tr_test_ref' =>15,
      ),
    ));
?>

Add class:

<?php
 l
('text', 'path', array('attributes' => array('class' => 'myclass')));
?>

url() method: When a simple url is needed instead of a complete a tag link

<?php
$link
= url('node/'.$node->nid, array('absolute' => TRUE));
?>

--
https://api.drupal.org/api/drupal/includes!common.inc/function/drupal_ge...
https://api.drupal.org/api/drupal/includes%21common.inc/function/l/7.x
https://api.drupal.org/api/drupal/includes%21common.inc/function/url/7.x