PHP教程

【Laravel3.0.0源码阅读分析】url类url.php

本文主要是介绍【Laravel3.0.0源码阅读分析】url类url.php,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
<?php namespace Laravel; use Laravel\Routing\Router, Laravel\Routing\Route;

class URL {

	/**
	 * The cached base URL.
	 * 缓存的基本 URL。
	 * @var string
	 */
	public static $base;

	/**
	 * Get the full URI including the query string.
	 * 获取包含查询字符串的完整 URI。
	 * @return string
	 */
	public static function full()
	{
		return static::to(URI::full());
	}

	/**
	 * Get the full URL for the current request.
	 * 获取当前请求的完整 URL。
	 * @return string
	 */
	public static function current()
	{
		return static::to(URI::current());
	}

	/**
	 * Get the URL for the application root.
	 * 获取应用程序根的 URL。
	 * @param  bool    $https
	 * @return string
	 */
	public static function home($https = false)
	{
		$route = Router::find('home');

		// If a route named "home" exists, we'll route to that instead of using
		// the single slash root URI. THis allows the HTTPS attribute to be
		// respected instead of being hard-coded in the redirect.
        // 如果存在名为“home”的路由,我们将路由到该路由,而不是使用单斜杠根 URI。
        // 这允许遵守 HTTPS 属性,而不是在重定向中进行硬编码。
		if ( ! is_null($route))
		{
			return static::to_route('home');
		}

		return static::to('/', $https);
	}

	/**
	 * Get the base URL of the application.
	 * 获取应用程序的基本 URL。
	 * @return string
	 */
	public static function base()
	{
		if (isset(static::$base)) return static::$base;

		$base = 'http://localhost';

		// If the application URL configuration is set, we will just use that
		// instead of trying to guess the URL from the $_SERVER array's host
		// and script variables as this is more reliable.
        // 如果设置了应用程序 URL 配置,我们将只使用它而不是尝试从 $_SERVER 数组的主机和脚本变量中猜测 URL,因为这更可靠
		if (($url = Config::get('application.url')) !== '')
		{
			$base = $url;
		}
		elseif (isset($_SERVER['HTTP_HOST']))
		{
			$protocol = (Request::secure()) ? 'https://' : 'http://';

			// Basically, by removing the basename, we are removing everything after the
			// and including the front controller from the request URI. Leaving us with
			// the path in which the framework is installed.
            // 基本上,通过删除基本名称,我们从请求 URI 中删除了前端控制器之后的所有内容,包括前端控制器。 留给我们安装框架的路径。
			$script = $_SERVER['SCRIPT_NAME'];
            // basename-返回路径中文件名部分
			$path = str_replace(basename($script), '', $script);

			// Now that we have the base URL, all we need to do is attach the protocol
			// and the HTTP_HOST to build the full URL for the application. We also
			// trim off trailing slashes to clean the URL.
            // 现在我们有了基本URL,我们所需要做的就是附加协议和HTTP_HOST,以为应用程序构建完整的URL。 我们还修剪了尾部斜杠以清理 URL。
			$base = rtrim($protocol.$_SERVER['HTTP_HOST'].$path, '/');
		}

		return static::$base = $base;
	}

	/**
	 * Generate an application URL.
	 * 生成应用程序 URL。
	 * <code>
	 *		// Create a URL to a location within the application
	 *		$url = URL::to('user/profile');
	 *
	 *		// Create a HTTPS URL to a location within the application
	 *		$url = URL::to('user/profile', true);
	 * </code>
	 *
	 * @param  string  $url
	 * @param  bool    $https
	 * @return string
	 */
	public static function to($url = '', $https = false)
	{
		if (filter_var($url, FILTER_VALIDATE_URL) !== false) return $url;

		$root = static::base().'/'.Config::get('application.index');

		// Since SSL is not often used while developing the application, we allow the
		// developer to disable SSL on all framework generated links to make it more
		// convenient to work with the site while developing locally.
        // 由于在开发应用程序时不经常使用 SSL,我们允许开发人员在所有框架生成的链接上禁用 SSL,以便在本地开发时更方便地使用站点。
		if ($https and Config::get('application.ssl'))
		{
			$root = preg_replace('~http://~', 'https://', $root, 1);
		}

		return rtrim($root, '/').'/'.ltrim($url, '/');
	}

	/**
	 * Generate an application URL with HTTPS.
	 * 使用 HTTPS 生成应用程序 URL
	 * @param  string  $url
	 * @return string
	 */
	public static function to_secure($url = '')
	{
		return static::to($url, true);
	}

	/**
	 * Generate a URL to a controller action.
	 * 生成控制器操作的 URL。
	 * <code>
	 *		// Generate a URL to the "index" method of the "user" controller
	 *		$url = URL::to_action('user@index');
	 *
	 *		// Generate a URL to http://example.com/user/profile/taylor
	 *		$url = URL::to_action('user@profile', array('taylor'));
	 * </code>
	 *
	 * @param  string  $action
	 * @param  array   $parameters
	 * @return string
	 */
	public static function to_action($action, $parameters = array())
	{
		// This allows us to use true reverse routing to controllers, since
		// URIs may be setup to handle the action that do not follow the
		// typical Laravel controller URI conventions.
        // 这允许我们使用真正的反向路由到控制器,因为 URI 可能被设置来处理不遵循典型 Laravel 控制器 URI 约定的操作。
		$route = Router::uses($action);

		if ( ! is_null($route))
		{
			return static::explicit($route, $action, $parameters);
		}
		// If no route was found that handled the given action, we'll just
		// generate the URL using the typical controller routing setup
		// for URIs and turn SSL to false.
        // 如果没有找到处理给定操作的路由,我们将只使用 URI 的典型控制器路由设置生成 URL,并将 SSL 设置为 false。
		else
		{
			return static::convention($action, $parameters);
		}
	}

	/**
	 * Generate a action URL from a route definition
	 * 从路由定义生成操作 URL
	 * @param  array   $route
	 * @param  string  $action
	 * @param  array   $parameters
	 * @return string
	 */
	protected static function explicit($route, $action, $parameters)
	{
	    // current — 返回数组中的当前值,每个数组中都有一个内部的指针指向它“当前的”单元,初始化时会指向该数组中的第一个值。
		$https = array_get(current($route), 'https', false);
        // key-从关联数组中取得键名
		return static::to(static::transpose(key($route), $parameters), $https);
	}

	/**
	 * Generate an action URI by convention.
	 * 按照约定生成操作 URI。
	 * @param  string  $action
	 * @param  array   $parameters
	 * @return string
	 */
	protected static function convention($action, $parameters)
	{
		list($bundle, $action) = Bundle::parse($action);

		$bundle = Bundle::get($bundle);

		// If a bundle exists for the action, we will attempt to use it's "handles"
		// clause as the root of the generated URL, as the bundle can only handle
		// URIs that begin with that string.
        // 如果存在用于操作的包,我们将尝试使用它的“handles”子句作为生成 URL 的根,因为包只能处理以该字符串开头的 URI。
		$root = $bundle['handles'] ?: '';

		$https = false;

		$parameters = implode('/', $parameters);

		// We'll replace both dots and @ signs in the URI since both are used
		// to specify the controller and action, and by convention should be
		// translated into URI slashes.
        // 我们将同时替换URI中的点和@符号,因为两者均用于指定控制器和操作,并且按照惯例应将其转换为URI斜杠。
		$uri = $root.'/'.str_replace(array('.', '@'), '/', $action);

		$uri = static::to(str_finish($uri, '/').$parameters);

		return trim($uri, '/');
	}

	/**
	 * Generate an application URL to an asset.
	 * 生成资产的应用程序 URL。
	 * @param  string  $url
	 * @param  bool    $https
	 * @return string
	 */
	public static function to_asset($url, $https = null)
	{
		if (is_null($https)) $https = Request::secure();

		$url = static::to($url, $https);

		// Since assets are not served by Laravel, we do not need to come through
		// the front controller. So, we'll remove the application index specified
		// in the application config from the generated URL.
        // 由于 Laravel 不为资产提供服务,因此我们不需要通过前端控制器。
        // 因此,我们将从生成的URL中删除在应用程序配置中指定的应用程序索引。
		if (($index = Config::get('application.index')) !== '')
		{
			$url = str_replace($index.'/', '', $url);
		}

		return $url;
	}

	/**
	 * Generate a URL from a route name.
	 * 从路由名称生成 URL。
	 * <code>
	 *		// Create a URL to the "profile" named route
	 *		$url = URL::to_route('profile');
	 *
	 *		// Create a URL to the "profile" named route with wildcard parameters
	 *		$url = URL::to_route('profile', array($username));
	 * </code>
	 *
	 * @param  string  $name
	 * @param  array   $parameters
	 * @param  bool    $https
	 * @return string
	 */
	public static function to_route($name, $parameters = array())
	{
		if (is_null($route = Routing\Router::find($name)))
		{
			throw new \Exception("Error creating URL for undefined route [$name].");
		}

		// To determine whether the URL should be HTTPS or not, we look for the "https"
		// value on the route action array. The route has control over whether the URL
		// should be generated with an HTTPS protocol string or just HTTP.
        // 为了确定 URL 是否应该是 HTTPS,我们在路由操作数组中查找“https”值。
        // 路由可以控制是使用 HTTPS 协议字符串还是仅使用 HTTP 生成 URL。
		$https = array_get(current($route), 'https', false);

		$uri = trim(static::transpose(key($route), $parameters), '/');

		return static::to($uri, $https);
	}

	/**
	 * Substitute the parameters in a given URI.
	 * 替换给定 URI 中的参数。
	 * @param  string  $uri
	 * @param  array   $parameters
	 * @return string
	 */
	public static function transpose($uri, $parameters)
	{
		// Spin through each route parameter and replace the route wildcard segment
		// with the corresponding parameter passed to the method. Afterwards, we'll
		// replace all of the remaining optional URI segments.
        // 遍历每个路由参数并用传递给方法的相应参数替换路由通配符段。 之后,我们将替换所有剩余的可选 URI 段。
		foreach ((array) $parameters as $parameter)
		{
			if ( ! is_null($parameter))
			{
				$uri = preg_replace('/\(.+?\)/', $parameter, $uri, 1);
			}
		}

		// If there are any remaining optional place-holders, we'll just replace
		// them with empty strings since not every optional parameter has to be
		// in the array of parameters that were passed.
        // 如果还有任何剩余的可选占位符,我们将用空字符串替换它们,因为并非每个可选参数都必须在传递的参数数组中。
		$uri = str_replace(array_keys(Router::$optional), '', $uri);

		return trim($uri, '/');
	}

}

github地址: https://github.com/liu-shilong/laravel3-scr    

这篇关于【Laravel3.0.0源码阅读分析】url类url.php的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!