Php CodeIgniter资源目录字符串帮助程序
我正在使用CodeIgniter 3.1.7,自动加载并启用url帮助程序 如何编写利用基本url的assets\u helper.php;解析https://something.tld/ 并将第一个组作为子文件夹如果没有组,它将使用基本资源文件夹 从 我知道只需编写就可以实现这一点,但很难维护,比如说,如果下次我计划将资产文件夹更改为/subfolder/1/2/3/assets/。我可以只更改helper文件中的1个变量,而不影响原始源代码。如果创建asset\u helper.php,则需要自动加载它。在codeigniter中,您可以向现有助手添加新函数。由于url\u助手已经自动加载并准备好使用,我将向url\u助手添加新函数 在application/helpers目录中创建一个新文件MY_url_helper.php。 复制并粘贴下面的代码Php CodeIgniter资源目录字符串帮助程序,php,arrays,string,codeigniter,helper,Php,Arrays,String,Codeigniter,Helper,我正在使用CodeIgniter 3.1.7,自动加载并启用url帮助程序 如何编写利用基本url的assets\u helper.php;解析https://something.tld/ 并将第一个组作为子文件夹如果没有组,它将使用基本资源文件夹 从 我知道只需编写就可以实现这一点,但很难维护,比如说,如果下次我计划将资产文件夹更改为/subfolder/1/2/3/assets/。我可以只更改helper文件中的1个变量,而不影响原始源代码。如果创建asset\u helper.php,则需
<?php
function assets_url($dir,$file=null){
$assets_dir = base_url()."path/to/your/assets_folder/";
$asset_path = $assets_dir.$dir;
if(!empty($file)){
$asset_path .="/$file";
}
return $asset_path;
}
将/path/to/your/assets\u folder/替换为末尾的资产文件夹“/”非常重要 你可能会发现我写的这个模型很有用,需要修改 $this->asset->load'item' 如果项目位于$this->assets数组中,则将从该数组中加载该项目。 否则,您必须传递url或名称:$this->asset->load'item.js' 您还可以传递数组$this->asset->loadarray'item1.js'、'item2.css' 它将使用CI中的link_tag函数和自定义脚本_tag函数,并返回包含适当js/css和标记的字符串 将需要对常量资产_DIR、ROOT进行一些修改和定义。但功能是存在的
class Asset_loader_model extends CI_Model {
private $types = array('css', 'js');
private $assets = array();
public function __construct() {
parent::__construct();
$this->assets = $this->n_config->get('plugin_assets');
}
/**
* Loads an asset or assets by key
*
* @param string $asset
* @param boolean $return Default to false
*/
public function load($asset, $return = false) {
$out = '';
if (is_array($asset)) {
foreach ($asset as $src) {
$out .= $this->load($src, true);
}
} else {
if (isset($this->assets[$asset])) {
// asset is in the plugins array
$out .= $this->__load($this->assets[$asset]);
} elseif (is_file(ROOT . ASSETS_DIR . $asset)) {
// asset must be a /js/include.js
$out .= $this->__load($asset);
} else {
trigger_error("Could not find asset {$asset}.", E_USER_WARNING);
}
}
if ($return) {
return $out;
} else {
echo $out;
}
}
/**
* Loads a single asset array by key
*
* @param key $asset
* @return string
*/
private function __load($asset) {
$out = '';
if (is_array($asset)) {
foreach ($asset as $type => $srcs) {
if (!in_array($type, $this->types)) {
continue;
}
if (is_array($srcs)) {
foreach ($srcs as $src) {
$out .= $this->load_individual($src, $type);
}
} else {
$out .= $this->load_individual($srcs, $type);
}
}
} else {
$type = $this->get_type($asset);
if (in_array($type, $this->types)) {
$out .= $this->load_individual($asset, $type);
}
}
return $out;
}
/**
* Gets type based on extension
*
* @param string $src
* @return string $type
*/
public function get_type($src) {
return pathinfo($src, PATHINFO_EXTENSION);
}
/**
* Outputs src in proper html tag
*
* @param string $src
* @param string $type
*/
private function load_individual($src, $type) {
if (!is_http($src)) {
$src = base_url(ASSETS_DIR . $src);
}
switch ($type) {
case 'js':
return script_tag($src);
break;
case 'css':
return link_tag($src);
break;
}
}
}
资产管理辅助工具:
if (!function_exists('script_tag')) {
/**
* Script
*
* Generates link to a JS file
*
* @param mixed $src Script source or an array
* @param bool $index_page Should index_page be added to the js path
* @return string
*/
function script_tag($src = '', $index_page = false) {
$CI = & get_instance();
$script = '<script ';
if (is_array($src)) {
foreach ($src as $k => $v) {
if ($k === 'src' && !preg_match('#^([a-z]+:)?//#i', $v)) {
if ($index_page === true) {
$script .= 'src="' . $CI->config->site_url($v) . '"';
} else {
$script .= 'src="' . $CI->config->slash_item('base_url') . $v . '"';
}
} else {
$script .= $k . '="' . $v . '" ';
}
}
} else {
if (preg_match('#^([a-z]+:)?//#i', $src)) {
$script .= 'src="' . $src . '"';
} elseif ($index_page === true) {
$script .= 'src="' . $CI->config->site_url($src) . '"';
} else {
$script .= 'src="' . $CI->config->slash_item('base_url') . $src . '"';
}
}
return $script . ' type="text/javascript"' . "></script>";
//return $script . '></script>'; // w3c no need for type
}
}
/**
* Checks if a string contains a url
*
* @param string $str
* @return boolean
*/
function is_http($str) {
return preg_match("/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/", $str);
}
class Asset_loader_model extends CI_Model {
private $types = array('css', 'js');
private $assets = array();
public function __construct() {
parent::__construct();
$this->assets = $this->n_config->get('plugin_assets');
}
/**
* Loads an asset or assets by key
*
* @param string $asset
* @param boolean $return Default to false
*/
public function load($asset, $return = false) {
$out = '';
if (is_array($asset)) {
foreach ($asset as $src) {
$out .= $this->load($src, true);
}
} else {
if (isset($this->assets[$asset])) {
// asset is in the plugins array
$out .= $this->__load($this->assets[$asset]);
} elseif (is_file(ROOT . ASSETS_DIR . $asset)) {
// asset must be a /js/include.js
$out .= $this->__load($asset);
} else {
trigger_error("Could not find asset {$asset}.", E_USER_WARNING);
}
}
if ($return) {
return $out;
} else {
echo $out;
}
}
/**
* Loads a single asset array by key
*
* @param key $asset
* @return string
*/
private function __load($asset) {
$out = '';
if (is_array($asset)) {
foreach ($asset as $type => $srcs) {
if (!in_array($type, $this->types)) {
continue;
}
if (is_array($srcs)) {
foreach ($srcs as $src) {
$out .= $this->load_individual($src, $type);
}
} else {
$out .= $this->load_individual($srcs, $type);
}
}
} else {
$type = $this->get_type($asset);
if (in_array($type, $this->types)) {
$out .= $this->load_individual($asset, $type);
}
}
return $out;
}
/**
* Gets type based on extension
*
* @param string $src
* @return string $type
*/
public function get_type($src) {
return pathinfo($src, PATHINFO_EXTENSION);
}
/**
* Outputs src in proper html tag
*
* @param string $src
* @param string $type
*/
private function load_individual($src, $type) {
if (!is_http($src)) {
$src = base_url(ASSETS_DIR . $src);
}
switch ($type) {
case 'js':
return script_tag($src);
break;
case 'css':
return link_tag($src);
break;
}
}
}
if (!function_exists('script_tag')) {
/**
* Script
*
* Generates link to a JS file
*
* @param mixed $src Script source or an array
* @param bool $index_page Should index_page be added to the js path
* @return string
*/
function script_tag($src = '', $index_page = false) {
$CI = & get_instance();
$script = '<script ';
if (is_array($src)) {
foreach ($src as $k => $v) {
if ($k === 'src' && !preg_match('#^([a-z]+:)?//#i', $v)) {
if ($index_page === true) {
$script .= 'src="' . $CI->config->site_url($v) . '"';
} else {
$script .= 'src="' . $CI->config->slash_item('base_url') . $v . '"';
}
} else {
$script .= $k . '="' . $v . '" ';
}
}
} else {
if (preg_match('#^([a-z]+:)?//#i', $src)) {
$script .= 'src="' . $src . '"';
} elseif ($index_page === true) {
$script .= 'src="' . $CI->config->site_url($src) . '"';
} else {
$script .= 'src="' . $CI->config->slash_item('base_url') . $src . '"';
}
}
return $script . ' type="text/javascript"' . "></script>";
//return $script . '></script>'; // w3c no need for type
}
}
/**
* Checks if a string contains a url
*
* @param string $str
* @return boolean
*/
function is_http($str) {
return preg_match("/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/", $str);
}