php 在 Codeigniter 中创建后页链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8937472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
creating back page links in Codeigniter
提问by Arslan
I have a page with URL http://arslan/admin/category/index/0/name/asc/10in Codeigniter.
In this URL, the uri_segment
start from 0
. This (0
) is the default search value, name
and asc
are the default sort field and order, and 10 is the pagination index.
我在Codeigniter 中有一个带有 URL http://arslan/admin/category/index/0/name/asc/10的页面。在此 URL 中,uri_segment
从0
. 此(0
)是默认的搜索值,name
并且asc
是默认排序字段和顺序,以及图10是分页索引。
Now if I move to an add page with URL (http://arslan/admin/category/add/) similarly like above "add" is the current function.
现在,如果我移动到带有 URL (http://arslan/admin/category/add/) 的添加页面,与上面类似,“添加”是当前功能。
Now if i want to go back through a link to back page... How can I divert the user back? I can't make the URL go back.
现在,如果我想通过链接返回到后页...如何将用户转移回来?我无法让 URL 返回。
Can somebody help me please?
有人可以帮我吗?
回答by dVw
I am not sure if i understand the question correctly, if not please ignore my answer, but I think you want a link to "go back to previous page", similar to the back-button in a web browser.
我不确定我是否正确理解了这个问题,如果不是,请忽略我的回答,但我认为您想要一个“返回上一页”的链接,类似于网络浏览器中的后退按钮。
If so you could use javascript to solve this by simply using this line:
如果是这样,您可以使用 javascript 通过简单地使用这一行来解决这个问题:
<a href="javascript:window.history.go(-1);">Go back</a>
回答by Ben Swinburne
I extend the session class by creating /application/libaries/MY_Session.php
我通过创建来扩展会话类 /application/libaries/MY_Session.php
class MY_Session extends CI_Session {
function __construct() {
parent::__construct();
$this->tracker();
}
function tracker() {
$this->CI->load->helper('url');
$tracker =& $this->userdata('_tracker');
if( !IS_AJAX ) {
$tracker[] = array(
'uri' => $this->CI->uri->uri_string(),
'ruri' => $this->CI->uri->ruri_string(),
'timestamp' => time()
);
}
$this->set_userdata( '_tracker', $tracker );
}
function last_page( $offset = 0, $key = 'uri' ) {
if( !( $history = $this->userdata('_tracker') ) ) {
return $this->config->item('base_url');
}
$history = array_reverse($history);
if( isset( $history[$offset][$key] ) ) {
return $history[$offset][$key];
} else {
return $this->config->item('base_url');
}
}
}
And then to retrieve the URL of the last page visited you call
然后检索您访问的最后一个页面的 URL
$this->session->last_page();
And you can increase the offset and type of information returned etc too
您也可以增加返回的偏移量和信息类型等
$this->session->last_page(1); // page before last
$this->session->last_page(2); // 3 pages ago
The function doesn't add pages called using Ajax to the tracker but you can easily remove the if( !IS_AJAX )
bit to make it do so.
该函数不会将使用 Ajax 调用的页面添加到跟踪器,但您可以轻松删除该if( !IS_AJAX )
位以使其这样做。
Edit:
If you run to the error Undefined constant IS_AJAX, assumed IS_AJAX
add the line below to /application/config/constants.php
编辑:如果你运行到错误未定义的常量 IS_AJAX,假设 IS_AJAX 添加下面的行 /application/config/constants.php
define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
回答by Ruben Müller
There are two ways to solve your problem: First you could place a link that is using the javascript back-function onclick, like this ...
有两种方法可以解决您的问题:首先,您可以放置一个使用 javascript back-function onclick 的链接,如下所示...
<a href="javascript:history.back()">go back</a>
... or you always save the current full page url into a cookie and use that for generating the back link - a helper could look like this (not tested) ...
...或者您总是将当前的完整页面 url 保存到 cookie 中并使用它来生成反向链接 - 一个助手可能看起来像这样(未经测试)...
/**
* save url to cookie
*/
if(!function_exists('urlhistory_save'))
{
function urlhistory_save()
{
$CI =& get_instance();
$CI->load->library('session');
$array = array(
'oldUrl' = $CI->session->userdata('newurl'),
'newurl' = $CI->uri->uri_string()
);
$CI->session->set_userdata($array);
}
}
/**
* get old url from cookie
*/
if(!function_exists('urlhistory_get'))
{
function urlhistory_get()
{
$CI =& get_instance();
$CI->load->library('session');
return $CI->session->userdata('oldurl');
}
}
In your controller you would use urlhistory_save() to save the current URL and in the view youd could use urlhistory_get() to retreive the old address like this:
在您的控制器中,您将使用 urlhistory_save() 保存当前 URL,在视图中您可以使用 urlhistory_get() 来检索旧地址,如下所示:
<a href="<?php echo base_url().urlhistory_get(); ?>go back</a>
回答by Geordy James
The most simplest way to redirect to your previous page , try this it work for me
重定向到上一页的最简单方法,试试这个对我有用
redirect($this->agent->referrer());
you need to import user_agent library too $this->load->library('user_agent');
您也需要导入 user_agent 库 $this->load->library('user_agent');
回答by Aamir Shahzad
You can create a Session to go to back page as:
您可以创建一个会话以返回到后页:
$this->session->set_userdata('ses_back_jobs','controller
name'.array_pop(explode('controller name',$this->input->server('REQUEST_URI'),2))); //Back page
Then if u want to redirect to some page use it:
然后,如果您想重定向到某个页面,请使用它:
redirect($this->session->userdata('ses_back_jobs'));
or use it to the anchor.
或将其用于锚点。