[D7] Tạo form page nhận nhiều tham số bằng hook_menu - Access form page by parameters with hook_menu

Post Reply
tthlan
Quản trị viên
Posts: 75
Joined: Tue Aug 23, 2016 8:13 am

[D7] Tạo form page nhận nhiều tham số bằng hook_menu - Access form page by parameters with hook_menu

Post by tthlan »

khi gọi một form view, hook_menu cho form chỉ cần đơn giản như sau

Code: Select all

function test_form_menu() {
    $items = array();

    $items['test_form'] = array(
        'title' => 'Test Form',
        'type' => MENU_NORMAL_ITEM,
        'page callback' => 'call_test_function',
        'access callback' => TRUE,
        'access arguments' => array('access content'),
        'expanded' => TRUE
    );
    return $items;
}

Dùng với dòng code 'page arguments' => array()

Lúc đó form url : /test_form

Muốn truyền dữ liệu cho form view, hook_menu sẽ được viết lại

Code: Select all

function test_form_menu() {
    $items = array();

    $items['test_form/'] = array(
        'title' => 'Test Form',
        'type' => MENU_NORMAL_ITEM,
        'page callback' => 'call_test_function',
        'page arguments' => array(),
        'access callback' => TRUE,
        'access arguments' => array('access content'),
        'expanded' => TRUE
    );
     return $items;
}

hàm call_test_function được xây dựng với bao nhiêu tham số thì sẽ nhận được chừng ấy số tham số truyền vào từ url


ex1 : url /test_form/abc/def/ghf , function chỉ có 1 tham số nhận

Code: Select all

function call_test_function($p1 = null) {
       var_dump($p1); // out put abc
}
ex2 : url /test_form/abc/def, function chỉ có 2 tham số nhận

Code: Select all

function call_test_function($p1 = null, $p2 = null) {
       var_dump($p1); // out put abc
       var_dump($p2); // out put def
}
ex3 : url /test_form/abc/def, function có 3 tham số nhận

Code: Select all

function call_test_function($p1 = null, $p2 = null, $p3= 'default'){
       var_dump($p1); // out put abc
       var_dump($p2); // out put def
       var_dump($p3); // out put 'default' : no url data
}
ex4 : url /test_form/abc/def/xyz, function có 3 tham số nhận

Code: Select all

function call_test_function($p1 = null, $p2 = null, $p3= 'default'){
       var_dump($p1); // out put abc
       var_dump($p2); // out put def
       var_dump($p3); // out put xyz
}


Dùng với số lần sử xuất hiện Wild Cast '%'

Lúc đó form url : /test_form

Muốn truyền dữ liệu cho form view, hook_menu sẽ được viết lại

Code: Select all

function test_form_menu() {
   $items = array();

   $items['test_form/%'] = array(
 	'title' => 'Search User',
	'type' =>MENU_NORMAL_ITEM,
 	'page callback' => 'test_one', // function test_one($p1 = null)
	'page arguments' => array(1), // nhận giá trị thứ 1 trên url test_form/abc
	'access callback' => TRUE,
	'access arguments' => array('access content'),
 	'expanded' => TRUE,
    );
	return $items;
}

function test_one($p1 = null)
{
	var_dump($p1); // output abc khi url là test_form/abc/xyx
}

Code: Select all

function test_form_menu() {
   $items = array();
   
   $items['test_form/%/%'] = array(
 	'title' => 'Search User',
	'type' =>MENU_NORMAL_ITEM,
 	'page callback' => 'test_two', // function test_one($p1 = null, $p2 = null)
	'page arguments' => array(1,2), // nhận giá trị thứ 1 trên url test_form/abc/xyz
	'access callback' => TRUE,
	'access arguments' => array('access content'),
 	'expanded' => TRUE,
    );
	return $items;
}

function test_two($p1 = null, $p2 = null)
{
	var_dump($p1, $p2 ); // output abc, xyz khi url là test_form/abc/xyx
		
	//var_dump($p1, $p2 ); // output abc, null khi url là test_form/abc
}
Khi hook_menu được xây dựng với cả test_form/% và test_form/%/% thì test_form/%/% - test_two function - sẽ ưu tiên được khi url là test_form/abc/xyz
Post Reply