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
}
Code: Select all
function call_test_function($p1 = null, $p2 = null) {
var_dump($p1); // out put abc
var_dump($p2); // out put def
}
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
}
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
}