Welcome to Abdul Malik Ikhsan's Blog

Zend Framework 2 : Routing – Literal, WildCard, Segment

Posted in Tutorial PHP, Zend Framework 2 by samsonasik on March 21, 2012

Routing adalah aksi untuk mencocokkan request dari action controller yg diberikan. Routing menghubungkan keduanya.  Proses request routing adalah kunci dari eksekusi flow aplikasi.  Sistem Route yang dimiliki oleh Zend Framework 2 ini antara lain sebagai berikut :

  1. Literal
  2. Segment
  3. Regex
  4. Part/TreeRouteStack
  5. WildCard
  6. Hostname
  7. Scheme

Pada kesempatan kali ini, saya akan mencoba memaparkan tentang route dengan cara Literal, WildCard, dan segment. Secara literal, route sama persis dengan yang kita tulis di route kita, misal “/contact” . WildCard route digunakan untuk mendapatkan semua variable ( * ) yang biasanya setelah route terakhir, misal * = /param1/value1/param2/value2. Sedang segment berupa pemecahan segment dari path URI, misal /:controller[/:action].

Ok, akan kita coba deh, anggaplah kita ingin mempunyai 2 tipe route sekaligus seperti ini :
1. /test/[:controller[/:action]]/*
2. /test/another/hello[/:name]
Maka kita bisa setting route seperti berikut :

    //route 1
   'test' => array(
        'type' => 'Zend\Mvc\Router\Http\Segment',
        'options' => array(
            'route' => '/test/[:controller[/:action]]',
            'constraints' => array(
                'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                'action' => '[a-zA-Z][a-zA-Z0-9_-]*'
            ),
            'defaults' => array(
                'module' => 'Test',
                'controller' => 'Test\Controller\TestController',
                'action' => 'index',
            ),
        ),
        'may_terminate' => true,
        'child_routes' => array(
            'wildcard' => array(
                'type' => 'Zend\Mvc\Router\Http\Wildcard',
                'options' => array(
                    'key_value_delimiter' => '/',
                    'param_delimiter' => '/',
                ),
                'may_terminate' => true,
            ),
        ),
    ),
   //end route 1

   //route 2
  'testanother' => array(
        'type' => 'Literal',
        'options' => array(
            'route' => '/test/another',
            'constraints' => array(
                'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
            ),
            'defaults' => array(
                'module'    => 'Test',
                'controller' => 'Test\Controller\AnotherController',
                'action' => 'index',

            ),
        ),

        'may_terminate' => true,
        'child_routes' => array(

            'sayhello' => array(
                'type' => 'Zend\Mvc\Router\Http\Segment',
                'options' => array(
                  'route' => '/hello[/:name]',
                  'defaults' => array(
                    'action'     => 'hello',
                    'name'       => 'samsonasik'
                  ),
                  'may_terminate' => true,
               ),
            ),

            'doaction' => array(
                'type' => 'Zend\Mvc\Router\Http\Segment',
                'options' => array(
                  'route' => '[/:action]',
                  'defaults' => array(
                    'action'     => 'doact'
                  ),
                  'may_terminate' => true,
               ),
            ),
        ),
    ),
    //end route 2

Sekarang kita bisa panggil deh :
1. http://localhost/aplikasi/public/test/test/about/id/1/address/pulau
yang berarti :
param1 bernama id bervalue 1
param2 bernama address bervalue pulau
2. http://localhost/aplikasi/public/test/another/hello/samsonasik
yang berarti :
param bernama name bervalue samsonasik

Contoh hasil jadi bisa didownload di sini.
Done !!!

Referensi :
http://packages.zendframework.com/docs/latest/manual/en/zend.mvc.routing.html

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: