我想知道你是否可以在PHP中重载函数,特别是在CodeIgniter中.例如,在我的控制器中,如果我要加载一个视图,但无论变量是作为参数提供还是留空,它都会有所不同.这是我尝试的概念,这是我在其他语言中学到的:
<?php
function load_view(){
$this->load->view('view');
}
function load_view($var){
$this->load->model('data');
$data = $this->data->getInfo($var);
$this->load->view('view', $data);
}
?>
Run Code Online (Sandbox Code Playgroud)
但是当我尝试这个时,我收到错误"致命错误:无法重新声明Controller :: load_view"......
任何帮助将不胜感激.提前致谢!
我正在使用Codeigniter的文件上传类来上传用户头像.有没有办法在用户上传新文件时替换用户的图像文件?我想用最新上传的头像替换现有头像.
我的图片上传控制器
function upload_avatar()
{
$config['upload_path'] = './uploads/avatars/';
$config['allowed_types'] = 'jpg|png';
$config['overwrite'] = FALSE; //overwrite user avatar
$config['encrypt_name'] = TRUE;
$config['max_size'] = '200'; //in KB
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = $this->upload->display_errors();
$this->session->set_flashdata('error', $error);
redirect('/settings/avatar');
}
else
{
$config['image_library'] = 'gd2';
$config['source_image'] = $this->upload->upload_path.$this->upload->file_name;
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = FALSE;
$config['width'] = 120;
$config['height'] = 120;
$this->load->library('image_lib', $config);
$this->image_lib->crop();
//Add image path to database
$avatar_path = 'uploads/avatars/' . $this->upload->file_name;
$user_id = $this->tank_auth->get_user_id();
$this->Settings_model->update_avatar($avatar_path, $user_id);
$this->session->set_flashdata('success', …Run Code Online (Sandbox Code Playgroud) 所以基本上当我在锚标记的href属性中回显codeigniter函数base_url()时,它似乎将它回显两次.例:
<a href="<?php echo base_url(); ?>">somelink</a>
Run Code Online (Sandbox Code Playgroud)
以上,如果您检查它,您的Chrome浏览器会显示:
<a href="www.mysitedomainname.com/www.mysitedomainname.com/">somelink</a>
Run Code Online (Sandbox Code Playgroud)
"mysitedomainname.com"只是我为这个例子编写的名字.出现这种情况的原因是什么?
我正在构建一个Web应用程序,后面是CodeIgniter,前面是Backbone/RequireJS.因此,它不是单页面Web应用程序,而是仍然在许多控制器操作中使用CI,而在不同时间使用Backbone用于各种视图.我有这样的RequireJS/Backbone设置:
<script type="text/javascript" data-main="main" src="require.js"></script>
Run Code Online (Sandbox Code Playgroud)
main.js看起来像:
require.config({
paths: {
jquery: "https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min",
underscore: "libs/underscore",
backbone: "libs/backbone",
//Lots of other stuff
},
shim: {
backbone: {
deps: ["underscore", "jquery"],
}
// lots of other stuff
}
});
require(['views/app'], function(AppView){
var app_view = new AppView;
});
Run Code Online (Sandbox Code Playgroud)
Main.js加载在我的主模板中,即在每个页面上,然后加载一堆在整个站点/应用程序中常见的视图 - 标题,导航等.
但是在不同的页面上我还想加载不同的视图以及常见的视图.因此,例如,在一个名为"发布"的页面上 - 几乎是一个应用程序本身 - 我这样做:
<script type="text/javascript" src="publish.js"></script>
Run Code Online (Sandbox Code Playgroud)
哪个是在main.js之后加载的,看起来像:
//publish.js
require(['views/publishview], function(PublishView){
var publishView = new PublishView;
});
Run Code Online (Sandbox Code Playgroud)
基本上,它的工作时间是一半,而另一半(当我刷新时)我得到了404s.
那么有一种巧妙的方法可以使用我的基本需求配置,但是在不同的时间也需要不同的部件吗?如果整个事情都是SPA,这似乎很容易做到,但对我来说,应用程序的很大一部分仍然使用CodeIgniter.
很抱歉没有更好的措辞.
我已经阅读了有关Stackoverflow的所有问题,并且没有看到问题的确切答案.我目前正在使用codeigniter分页库来使用限制和偏移量生成我的分页链接:
$config['base_url'] = base_url() . 'explore/featured/index/';
$config['total_rows'] = $this->Projects_model->get_featured_count();//Count featured projects
$config['per_page'] = 12;
$config['uri_segment'] = 4;
$config['display_pages'] = FALSE;
$config['next_link'] = 'Older →';
$config['prev_link'] = '← Newer';
$this->pagination->initialize($config);
$limit = $config['per_page'];
$offset = $this->uri->segment(4);
$data['pagination'] = $this->pagination->create_links();
$data['projects'] = $this->Projects_model->get_featured($limit, $offset); //get featured projects from database
Run Code Online (Sandbox Code Playgroud)
上面的代码限制了我的查询的输出值,并使用了我的第4个URI段中包含的偏移量.
如何将以下代码更改为页码??? 似乎是不可能的,因为我将不再能够在我的网址中使用偏移量.
有没有办法从控制器发送数据到视图.我正在做的是用户可以选择链接,根据链接用户点击,相应的数据传递给控制器,另一个视图从对应于该链接的控制器加载.但我不知道该怎么做.我在这里使用篝火是我的控制器代码: -
function my_tickets() {
$username = $this->auth->username();
$this->load->model('helpdesk_model');
$result_set = $this->helpdesk_model->my_tickets($username);
foreach($result_set->result() as $data ) {
$title[] = $data->title;
$category[]=$data->category;
$priority[]=$data->priority;
$date[]=$data->date;
$post_status[]=$data->post_status;
$userfile_path[] = $data->userfile_path;
}
$arraysize=count($title);
Template::set('arraysize',$arraysize);
Template::set('title',$title);
Template::set('category',$category);
Template::set('priority',$priority);
Template::set('date',$date);
Template::set('post_status',$post_status);
Template::set('username',$this->auth->username());
Template::set('userfile_path',$userfile_path);
Template::render();
}
function full_post(){
Template::render();
}
}
Run Code Online (Sandbox Code Playgroud)
在我的模型部分: -
function my_tickets($username){
$this->db->select('title,userfile_path,category,priority,date,post_status');
$this->db->where('username',$username);
$result = $this->db->get('tbl_tickets');
return $result;
}
Run Code Online (Sandbox Code Playgroud)
我的意见是: -
<?php
$arraysize =Template::get('arraysize');
$title[] = Template::get('title');
$category[]=Template::set('category');
$priority[]=Template::set('priority');
$date[]=Template::set('date');
$post_status[]=Template::set('post_status');
$username = Template::set('username');
$userfile_path = Template::set('userfile_path');
?>
<h3>Total Number Of posts : …Run Code Online (Sandbox Code Playgroud) 我是CodeIgniter的新手.我正在使用Phil Sturgeon的RestServer和RestClient.我一直在尝试在我的CodeIgniter RestClient控制器中发出POST请求来更新我的CodeIgniter RestServer中的数据,但它永远不会更新我的数据库中的数据.我认为我的POST请求不对.
这是我在控制器中的RestClient POST请求:
$result = $this->rest->post('newscontroller/news/format/json',
array('news_id' => $news_id,
'news_title' => $news_title,
'news_category' => $news_category ),
'json');
if(isset($result->status) && $result->status == 'success')
{
$data['message'] ='News has been updated.';
$this->load->view('otherpageview',$data);
}
else
{
$data['message'] ='Something has gone wrong';
$this->load->view('otherpageview',$data);
}
Run Code Online (Sandbox Code Playgroud)
似乎$ result没有得到任何值,因为我确实回显$ result-> status并且它没有任何显示.而且我在这个控制器的构造函数中也有这个:
// Load the rest client spark
$this->load->spark('restclient/2.1.0');
// Load the library
$this->load->library('rest');
// Run some setup
$this->rest->initialize(array('server' => 'http://api.therestserver.com/index.php/'));
Run Code Online (Sandbox Code Playgroud)
而在RestServer的控制器(即newscontroller)中,有这样的方法:
function news_post()
{
$news=array(
'news_id' => $this->post('news_id'),
'news_title' …Run Code Online (Sandbox Code Playgroud) 在我的网站上,我有一个全局Javascript文件,其中包括jQuery和下拉菜单的代码等.许多页面还有针对次要页面特定交互,表格等的自定义Javascript.
我在每个视图上设置的当前header.php文件是一个文件,基本上涵盖了从doctype到内容的开始,特定页面的视图文件以及footer.php关闭页面的所有内容.
目前global.js是从中链接的<head>.为了性能,我们应该把JS放在页面的最底部,但我无法找到一个好方法来做到这一点.我可以global.js使用自定义脚本块添加完整的脚本行,但这意味着我必须在每个页面上添加它,即使没有其他Javascript.有没有更好的方法将JS移到底部?
我使用控制器为页面做一些自定义uri路由,它目前工作得很好.
这是控制器的精简版本,它使用PHPTAL作为模板引擎.
public function index()
{
$this->tal->display('index');
}
public function view($url)
{
$this->loadView($url);
}
private function loadView($url)
{
if (file_exists(ROOTPATH . 'webroot/' . $url . '/index.html'))
{
$this->tal->display($url . '/index');
}
else
{
show_404();
}
}
Run Code Online (Sandbox Code Playgroud)
我最近注意到,每次访问页面控制器时,我的日志中都出现以下错误:
ERROR - 2013-02-06 10:58:23 --> 404 Page Not Found -->
我发现这很奇怪,因为页面按预期显示,并且肯定没有404标题,网络面板没有404状态.
我最后将其缩小到show_404()方法中调用的辅助loadView()方法.删除该行可以阻止错误完全出现在日志文件中.
只有在找不到视图文件时才应执行此§show_404()§,在这种情况下,它应该显示404错误页面.但是,无论是否调用该方法,该方法的日志记录部分似乎都是在每次调用页面控制器时执行的loadView().
我index()在浏览器中访问视图,一切似乎工作正常,加载了正确的视图文件,没有错误.但是,从该loadView()方法记录404错误消息.该loadView()方法甚至没有从该index()方法调用,但它在日志文件中的输出似乎表明它正在被执行.
我有以下代码块,预计会返回 count
$sql = "SELECT sum(count) as count
FROM multipleowners WHERE owner = ? " . $localityquery;
$queryarray = array($owner, $locality);
$query = $this->db->query($sql, $queryarray);
if ($query->num_rows() > 0)
{
$result = $query->row_array();
$count = $result['count'];
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试打印时,我得到空值$count.
我用过print_r($this->db->last_query());,得到了以下查询,
SELECT sum(count) as count FROM multipleowners WHERE owner = 'Davenports Harbour Trustee (2012) Limited' and locality = 'Auckland Central'
Run Code Online (Sandbox Code Playgroud)
当我直接执行此查询到我的时,Postgresql IDE我得到了countas 的输出2.
这个查询出错了什么地方?我怀疑的存在(与)中WHERE条款.我该如何解决 ?
更新 …
codeigniter ×10
php ×7
activerecord ×1
anchor ×1
avatar ×1
backbone.js ×1
base-url ×1
html ×1
http-post ×1
hyperlink ×1
javascript ×1
mysql ×1
overloading ×1
pagination ×1
parameters ×1
performance ×1
postgresql ×1
requirejs ×1
rest ×1
rest-client ×1
upload ×1
url ×1