Quick links: Content - sections - sub sections
EN

Trace: jurl

jUrl, the automatic URL engine of Jelix.

(supported version of Jelix 1.3.x)

this article will talk about 2 points of the URL engine

  1. writting URL “à la jelix”
  2. the URL engines of Jelix

1 - Introduction

Classically, to add a link in an HTML page, we just put the URL manually as follow :

<a href="/my/path/to/my/internal/link" title="my uggly link">My Nice Site</a>

But, if you install this application in an environment where the path is different, one the click of this link will lead to a 404 error page

2 - writting URL "à la jelix"

the simplest

To avoid the previous issue, jUrl does its job, to a very simple way, as follow :

<a href="{jurl 'news~default:index',array('parm1'=>'value1')}" title="My beautiful link">My Nice Site</a>

Where :

  1. {jurl …} is the template plugin that will build the URL for you
  2. news is the module name,
  3. default is the name of my controller,
  4. and index is my method.

This will build an URL http://localhost/index.php?module=news&action=default:index&parm1=value1

the prettiest

But of course, there is another way to have URLs cleaner and shortest than that.

You can use jUrl directlty in your controller and assign the result to the template

A concret case of this usage through a controller, could be for a form which always uses the same fields to create and edit a news, but the action itself will be different.

Let's have a look to the following example which illustrates how to assign the result of jUrl to a template

the controller

function index() {
    $rep = $this->getResponse('html');
    $myUrl = jUrl::get('module~controller:action',
                    array('parm1'=>'what-an-amazing-engine'));
    $rep->body->assign('myurl', $myUrl);
    return $rep;
}

the template

<a href="{$myurl}">My Beautiful Site</a>

3 - URL engines of Jelix

Now another aspect of the URL engines of Jelix is its type, which can be :

  1. basic_significant
  2. significant

The type of engine basic_significant produces URLs à la “Cake PHP” example :

http://localhost/index.php/news/default/index?parm1=value1

which can be “translated” by :

http://localhost/index.php/module/controller/method?parm1=value1

The second URL Engine is the type significant which procudes the following URL :

http://localhost/news/what-an-amazing-engine

this result is how much lighter and shorter to find/memorize the news “what-an-amazing-engine” ;)

This engine uses a file named urls.xml, which can tell Jelix “this url is this module and the controller”

Thus for the URL http://localhost/news/what-an-amazing-engine, the urls.xml file could look like this :

<?xml version="1.0" encoding="UTF-8"?>
<urls xmlns="http://jelix.org/ns/urls/1.0">  
    <classicentrypoint name="index" default="true" noentrypoint="true">
        <url pathinfo="/news/:parm1" module="news" action="default:index">
           <param name="parm1" type="string"/>
        </url>
    </classicentrypoint>
</urls>

4 - Conclusion

  • URLs Generator allows precisely never have anything hardcoded and not freezing the choice of the URL engine
  • the engine basic_significant represents a good ratio btw elapsed time / quality of the generated URLs.
  • the engine significant is a little trickier to manage because we have to write every URL, with a risk to see some of them totally inoperative

with jelix, jUrl is the essential tool for porting its applications in heterogeneous environments and a very flexible implementation.

read more about jURL

en/tutorials/jurl.txt · Last modified: 2012/04/14 18:44 by laurent

Recent changes RSS feed Creative Commons License