php 重定向 url 参数,php 后台转发和重定向的区别及kohana框架当前url加参数方式...

本文介绍了Kohana框架中URL的处理方式,包括如何获取当前URL、隐藏index.php的方法,以及实现列表分页的具体步骤。通过.htaccess配置实现了URL重写,使网站更加美观。同时展示了如何通过Pagination类实现内容的分页显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、重定向是浏览器行为,浏览器地址有变化;转发是后台服务器完成, url地址不变化。

2、kohana获取URL

当前url是http://soyoung.kohana.com/blog/add?id=3

var_dump(Url::site(‘blog/add‘, NULL, FALSE) );               打印string ‘/blog/add‘ (length=9)

var_dump(URL::query());                                                    打印string ‘?id=3‘ (length=5)

E:\html\kohana\modules\pagination\classes\kohana\pagination.php

public function url($page = 1)

{

// Clean the page number

$page = max(1, (int) $page);

// No page number in URLs to first page

if ($page === 1 AND ! $this->config[‘first_page_in_url‘])

{

$page = NULL;

}

switch ($this->config[‘current_page‘][‘source‘])

{

case ‘query_string‘: //不带之前url参数

return URL::site(Request::current()->uri(),NULL,FALSE).URL::query(array($this->config[‘current_page‘][‘key‘] => $page));

case ‘route‘: //带上之前url参数return URL::site(Request::current()->uri(array($this->config[‘current_page‘][‘key‘] => $page)),NULL,FALSE).URL::query();

}

return ‘#‘;

}

E:\html\kohana\application\classes\Controller\Blog.php

//列表分页

public function action_index() {

//first calc average count

$count = model::factory(‘Post‘)->posts_count();

//you can change items_per_page

$num = 2;

//you can configure routes and custom routes params

$pagination = Pagination::factory(array(

‘total_items‘ => $count,

‘items_per_page‘ => $num,

//‘current_page‘ => Request::current()->param("page"),

‘current_page‘ => array(‘source‘ => ‘route‘, ‘key‘ => ‘page‘),

)

);

$posts_list = model::factory(‘Post‘)->posts_list($pagination->items_per_page, $pagination->offset);

$view = View::factory(‘blog/list‘)->set(‘posts‘, $posts_list)->set(‘pagination‘,$pagination);

$view->render();

$this->response->body($view);

}

kohana隐藏index.php

步骤1:.htaccess

# Turn on URL rewriting

RewriteEngine On

# Installation directory

rewriteBase /

# Protect hidden files from being viewed

Order Deny,Allow

Deny From All

# Protect application and system files from being viewed

#RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]

RewriteRule ^(application|modules|system)/ - [F,L]

# Allow any files or directories that exist to be displayed directly

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite all other URLs to index.php/URL

#RewriteRule .* index.php/$0 [PT]

RewriteRule .* index.php [L]

步骤2:Url::site(‘blog/add‘, NULL, FALSE)

/**

* Fetches an absolute site URL based on a URI segment.

*

* echo URL::site(‘foo/bar‘);

*

* @param string $uri Site URI to convert

* @param mixed $protocol Protocol string or [Request] class to use protocol from

* @param boolean $indexInclude the index_page in the URL

* @return string

* @uses URL::base

*/

public static function site($uri = ‘‘, $protocol = NULL, $index = TRUE)

{

// Chop off possible scheme, host, port, user and pass parts

$path = preg_replace(‘~^[-a-z0-9+.]++://[^/]++/?~‘, ‘‘, trim($uri, ‘/‘));

if ( ! UTF8::is_ascii($path))

{

// Encode all non-ASCII characters, as per RFC 1738

$path = preg_replace_callback(‘~([^/]+)~‘, ‘URL::_rawurlencode_callback‘, $path);

}

// Concat the URL

return URL::base($protocol, $index).$path;

}

步骤3:bootstrap.php

* - string base_url path, and optionally domain, of your application NULL

* - string index_file name of your index file, usually "index.php" index.php

* - string charset internal character set used for input and output utf-8

* - string cache_dir set the internal cache directory APPPATH/cache

* - integer cache_life lifetime, in seconds, of items cached 60

* - boolean errors enable or disable error handling TRUE

* - boolean profile enable or disable internal profiling TRUE

* - boolean caching enable or disable internal caching FALSE

* - boolean expose set the X-Powered-By header FALSE

*/

Kohana::init(array(

‘base_url‘ => ‘/‘,

‘index_file‘ => ‘‘,

));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值