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

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
<Files .*>
	Order Deny,Allow
	Deny From All
</Files>

# 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 $index		Include 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' => '',
));

  

猜你喜欢

转载自www.cnblogs.com/hnhycnlc888/p/10965081.html