php codeigniter 在视图中包含公共文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16446242/
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
codeigniter include common file in view
提问by Alessandro Minoccheri
Hi all I have a site developed in codeigniter and I wanto to store into a file called common.php some javascript/PHP function that I use in many pages. I have tried in this mode:
大家好 我有一个在 codeigniter 中开发的网站,我想将一些 javascript/PHP 函数存储到一个名为 common.php 的文件中,我在许多页面中使用了这些函数。我在这种模式下尝试过:
require(base_url().'application/libraries/common.php'); //I have tried also include
This return me this error:
这让我返回这个错误:
A PHP Error was encountered
Severity: Warning
Message: require() [function.require]: http:// wrapper is disabled in the server configuration by allow_url_include=0
I'm going to my php.ini and I turn On allow_url_include, restart apache and when I try to load the page return me now this error:
我要去我的 php.ini 并打开 allow_url_include,重新启动 apache,当我尝试加载页面时,现在返回此错误:
A PHP Error was encountered
Severity: Warning
Message: require() [function.require]: http:// wrapper is disabled in the server configuration by allow_url_include=0
Filename: backend/hotel_view.php
Line Number: 6
A PHP Error was encountered
Severity: Warning
Message: require(http://demo.webanddesign.it/public/klikkahotel.com/application/libraries/common.php) [function.require]: failed to open stream: no suitable wrapper could be found
Filename: backend/hotel_view.php
Line Number: 6
Fatal error: require() [function.require]: Failed opening required 'http://demo.webanddesign.it/public/klikkahotel.com/application/libraries/common.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/public/klikkahotel.com/application/views/backend/hotel_view.php on line 6
What can I do to include a simple file into my pages?
我该怎么做才能在我的页面中包含一个简单的文件?
回答by xbonez
Pull it into whichever views you want using $this->load->view('common');
You can include other views from either the controller or the view.
将其拉入您想要使用的任何视图$this->load->view('common');
您可以包含来自控制器或视图的其他视图。
Example 1
示例 1
your_controller.php
your_controller.php
public function index() {
$this->load->view('homepage');
}
views/homepage.php
意见/主页.php
<?php
$this->load->view('common');
?>
<body>
<!-- html -->
</body>
Example 2
示例 2
your_controller.php
your_controller.php
public function index() {
$this->load->view('common');
$this->load->view('homepage');
}
回答by ajtrichards
You should use APPPATH
or BASEPATH
or just type the full path to the file.
您应该使用APPPATH
orBASEPATH
或只键入文件的完整路径。
For security, require_once
should be passed a local file, not a URL. I wouldn't really suggest using require_once()
in CodeIgniter. It might be better to use:
为了安全起见,require_once
应该传递一个本地文件,而不是一个 URL。我真的不建议require_once()
在 CodeIgniter 中使用。使用以下方法可能会更好:
$this -> load -> view('common_file');
$this -> load -> view('common_file');
回答by Kevin Mesiab
How To Make Global Functions In CodeIgnitor
如何在 CodeIgnitor 中创建全局函数
To create global functions, define them in CodeIgnitor Helperfiles and auto loadthem. Here's how:
要创建全局函数,请在CodeIgnitor Helper文件中定义它们并自动加载它们。就是这样:
Create Your Helper
创建您的助手
To create [helpers][2], create a .php file in the application/helpers/
folder and save your functions there.
要创建 [helpers][2],请在application/helpers/
文件夹中创建一个 .php 文件并将您的函数保存在那里。
Note: It is good practice to use the following format, to avoid function name collisions:
注意:最好使用以下格式,以避免函数名称冲突:
if ( ! function_exists('my_function_name'))
{
function my_function_name($arg)
{
/* Your code here */
}
}
Making Them Global
使它们全球化
If you use these functions all the time (Global), auto-load them.
如果您一直使用这些功能(全局),请自动加载它们。
- Open the file:
/config/autoload.php
- Find the section
$autoload['helper'] = array();
- Add the name of your helper file (excluding the.php extension).
- 打开文件:
/config/autoload.php
- 找到该部分
$autoload['helper'] = array();
- 添加帮助文件的名称(不包括 .php 扩展名)。
For instance, if you created a helper file called myhelper.php, It should look something like this:
例如,如果您创建了一个名为 myhelper.php 的帮助文件,它应该如下所示:
$autoload['helper'] = array('myhelper');
$autoload['helper'] = array('myhelper');
Using Your Global Functions
使用全局函数
Now your functions will be available throughout the site. Just call them wholesale:
现在,您的功能将在整个站点中可用。就叫他们批发:
my_sample_function('argument');
my_sample_function('argument');
回答by prash.patil
You should use APPPATH or BASEPATH or just type the full path to the file. require_once(APPPATH.'libraries/common.php');
您应该使用 APPPATH 或 BASEPATH 或只键入文件的完整路径。 require_once(APPPATH.'libraries/common.php');
回答by Fel
You Can include anyware using
您可以使用任何软件
<?php $this->load->view('file'); ?>
回答by machineaddict
base_url()
refers to the web path likehttp://localhost/myproject/
. You cannot include a remote file, actually you should not. It's a security risk. See Can't include file on remote serverBuilding a custom library is a good choice and if you are using it a lot in your website, you can include it in
application/config/autoload.php
under the section$autoload['libraries']
. It will autoload every time you reload the application/website based on codeigniter. Example:$autoload['libraries'] = array('common');
if your library is calledcommon.php
and is located inapplication/libraries/
DO NOTput functions into a viewer, that's why libraries and helpers exists. A viewer should contain only what a user should see. Example: a view is some form of visualisation of the model.
base_url()
指的是像http://localhost/myproject/
. 你不能包含远程文件,实际上你不应该。这是一个安全风险。请参阅无法在远程服务器上包含文件构建自定义库是一个不错的选择,如果您在网站中经常使用它,则可以将其包含在
application/config/autoload.php
部分下$autoload['libraries']
。每次您根据 codeigniter 重新加载应用程序/网站时,它都会自动加载。示例:$autoload['libraries'] = array('common');
如果您的库被调用common.php
并且位于application/libraries/
不要将函数放入查看器,这就是库和助手存在的原因。查看器应该只包含用户应该看到的内容。示例:视图是模型的某种形式的可视化。
回答by Tudor
For solve in more efficient way this problem I have done so:
为了以更有效的方式解决这个问题,我这样做了:
You create a new helper (in application/helpers) with name (es. common_helpers.php, the underscore is important). In this file, you put all the functions for example build pieces of html in common.
您创建一个新的助手(在 application/helpers 中),名称为(es.common_helpers.php,下划线很重要)。在此文件中,您将所有功能(例如构建 html 片段)放在一起。
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
function getHead(){
require_once(APPPATH."views/common/head.php");
}
function getScripts(){
require_once(APPPATH."views/common/scripts.php");
}
function getFooter(){
require_once(APPPATH."views/common/footer.php");
}
In your controller you call only one view in respect of MVC and call the functions from your custom helper.
在您的控制器中,您只调用一个关于 MVC 的视图,并从您的自定义助手调用函数。
class Hello extends CI_Controller {
public function index(){
$this->load->helper('common');
$this->load->view('index');
}
}
回答by Aman Attari
You can write php function in helper file
steps - create a helper file name common_helper
inside application/helper
folder
- and create a function like
您可以在帮助文件步骤中编写 php 函数 -common_helper
在application/helper
文件夹内创建一个帮助文件名- 并创建一个函数,如
function getBoosters($id){
$ci=& get_instance();
$ci->load->database();
$sql = "select * from boosters where id ='".$id."' ";
$query = $ci->db->query($sql);
return $query->result();
}
This common function you can use where you want by loading this helper.
您可以通过加载此帮助程序在需要的地方使用此常用功能。
Suppose you want to use this method in FrontController simply load the helper by this line
假设你想在 FrontController 中使用这个方法,只需通过这一行加载 helper
$this->load->helper('common');
Now you can call the method. Add your js code in footer.php
, all the functions are available on every page you can easily use them
现在您可以调用该方法。加入你的js代码footer.php
,所有的功能都在每个页面上你可以轻松使用