You are here

重定向

g089h515r806 的头像
Submitted by g089h515r806 on 星期一, 2009-08-17 09:09

老葛的Drupal培训班 Think in Drupal

用来处理表单的函数,应该把$form_state['redirect']设置为一个Drupal路径,比如node/1234,这样就可以将用户重定向到这个页面了。如果#submit属性中有多个函数,那么将会使用最后一个函数设置的$form_state['redirect']。如果没有函数把$form_state['redirect']设置为一个Drupal路径,那么用户将返回原来的页面(也就是,$_GET['q']的值)。在最后一个提交函数中返回FALSE,将会阻止重定向
    通过在表单中定义#redirect属性,就可以覆写在提交函数中$form_state['redirect']设置的重定向了,比如
 
$form['#redirect'] = 'node/1'
$form['#redirect'] = array('node/1', $query_string, $named_anchor)
 
    如果使用drupal_goto()中所用的参数术语,那么最后的一个例子将被改写为
 
$form['#redirect'] = array('node/1', $query, $fragment)
 
   表单重定向的判定,是由includes/form.inc中的drupal_redirect_form()完成的。而实际的重定向则由drupal_goto()实现,它为Web服务器返回一个Location头部。drupal_goto()的参数与后一个例子中的参数一致:drupal_goto($path = '', $query = NULL, $fragment = NULL)。
 

Drupal版本:

评论

biglazy 的头像

官方勘误:http://www.drupalbook.com/errata2?page=5    Page 228

Error: 

Last sentence of paragraph: "Returning FALSE from the final submit funciton avoids redirection."

Correction: 

Delete sentence

Description of the Error: 

Redirection always occurs after the final submiit. Depending on the #submit value, it may go to a different page, but there is always a redirect at this point.