我是CodeIgniter的新手.我正在将它用于我的个人项目.
我想遵循的一件事就是坚持最好的编码实践.其中一个是DRY规则(不要重复自己).我尝试在适当的模型类中定义一些常量,并在代码中的其他地方使用它们,所以当涉及到一点变化时,它必须只在一个地方完成.
我有这个类Hero_attributes,它有几个常量,例如:
class Hero_attributes
{
const ID = 'id';
const NAME = 'name';
const LEVEL = 'level';
const MAG_LEVEL = 'maglevel';
const VOCATION = 'vocation';
const GENDER = 'sex';
const SPEED = 'speed'
(...)
}
Run Code Online (Sandbox Code Playgroud)
我还制作了一个tenkai_config.php自动加载的自定义配置文件.在配置数组中,我想使用Hero_attributes类中的常量作为键,即:
$config['vocations']['default'] = array(
'hero' => array(
Hero_attributes::LAST_LOGIN => 0,
Hero_attributes::LEVEL => 1,
Hero_attributes::MAG_LEVEL => 80,
Hero_attributes::RESIDENCE => 1,
Hero_attributes::POSITION_X => 73,
Hero_attributes::POSITION_Y => 181,
Hero_attributes::POSITION_Z => 6,
(...)
)
);
Run Code Online (Sandbox Code Playgroud)
但是,我收到Fatal error: Class 'Hero_attributes' not found …
如何检查用户是否在或没有登录在图 ??
这样的事情:
if ($this->ion_auth->logged_in())
{
// do something ..
}
else
{
// do something else ..
}
Run Code Online (Sandbox Code Playgroud)
以及如何将用户数据变为变量?
非常感谢.
在过去的两年里,我是一名平板php程序员.现在我想转向MVC架构,所以我使用codeigniter它看起来非常简单.我想了解一些最佳实践,因为我正在开发中codeigniter.
我有一个controller叫building,一个model被叫building_data和一个叫做的视图building_view.现在我想通过检查很多条件来显示建筑物清单.我正在使用平面PHP进行以下操作
section - A,section -B和section-c作为HTML输出.现在,MVC我正在做以下事情
building_data(型号)building_data的$data数组中buildingbuilding_view并输出HTML(我可以在视图中进行基于条件的数据分类(不使用mysql查询)吗?!My actual question)我是否在不违反MVC架构规则的情况下做正确的事情?
这是我的模型:
function get_name() {
$this->db->select('first_name');
$this->db->where('email', $this->session->userdata('email'));
$query = $this->db->get('users');
if($query->num_rows() == 1) {
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的控制器方法:
function show_name() {
$this->load->model('users_model');
$this->users_model->get_name();
}
Run Code Online (Sandbox Code Playgroud)
以及传递名称以查看的方法:
function profile(){
$data['first_name'] = $this->show_name();
$this->load->view('members/template', $data);
}
Run Code Online (Sandbox Code Playgroud)
我在视图中什么都没有,我检查了我的查询,它工作正常.有什么我想念的吗?
我试过这段代码:
SET @num := 0, @type := NULL;
SELECT categories_name, products_name, products_url, categories_id,
@num := IF( @type = categories_name, @num +1, 1 ) AS row_number,
@type := categories_name AS dummy
FROM (
SELECT categories_name, products_name, products_url, ptc.categories_id
FROM toc_products_description pd
INNER JOIN toc_products_to_categories ptc ON pd.products_id = ptc.products_id
INNER JOIN toc_categories_description cd ON cd.categories_id = ptc.categories_id
AND pd.language_id =1
AND cd.language_id =1
) AS x
GROUP BY x.categories_name, x.products_name, x.products_url, x.categories_id
HAVING row_number <=2
Run Code Online (Sandbox Code Playgroud)
在SQL上(并且运行良好),现在我需要在codeigniter上实现此代码query().问题是我无法插入
SET @num := …Run Code Online (Sandbox Code Playgroud) 有没有人知道Codeigniter的Google双因素身份验证教程?
我在Codeigniter中使用Ion Auth身份验证库.当我加载页脚视图时,我收到CSRF错误(此表单帖子未通过我们的安全检查).当我删除页脚视图时,它工作正常!我有什么问题吗?谢谢!
function edit_user($id) {
//I'm only posting the last part of the code of edit_user function in the auth controller
$this->load->view('layout/header');
$this->_render_page('auth/edit_user', $this->data);
$this->load->view('layout/footer'); // I'm getting an error when I load this footer view.
}
Run Code Online (Sandbox Code Playgroud)
这是我的观点中的代码.
<h1><?php echo lang('edit_user_heading');?></h1>
<p><?php echo lang('edit_user_subheading');?></p>
<div id="infoMessage"><?php echo $message;?></div>
<?php echo form_open(uri_string());?>
<p>
<?php echo lang('edit_user_fname_label', 'first_name');?> <br />
<?php echo form_input($first_name);?>
</p>
<p>
<?php echo lang('edit_user_lname_label', 'last_name');?> <br />
<?php echo form_input($last_name);?>
</p>
<p>
<?php echo lang('edit_user_company_label', 'company');?> <br /> …Run Code Online (Sandbox Code Playgroud) 我有一个文件app/config/template.php:
$config['head_meta'] = array(
'charset' => 'UTF-8',
'description' => '',
'keywords' => '',
'stylesheets' => array(
'template.css'
),
'scripts' => array(
'plugins/jquery-2.0.3.min.js',
'plugins/bootstrap.min.js'
),
'end_scripts' => array(
'template.js'
)
);
Run Code Online (Sandbox Code Playgroud)
我需要在控制器的函数中覆盖该描述app/controllers/pages.php:
function contact($pagename = 'Contact', $slug = 'contact'){
// load dependencies
$this->load->library('form_validation');
$this->lang->load($slug);
// page settings
$this->config->set_item('page_name', $this->lang->line('menu_subtitle_contact'));
$this->config->set_item('page_slug', $slug);
$description = $this->config->item('description', 'head_meta');
var_dump($description);
$data['view'] = $this->load->view($slug, '', TRUE);
$this->load->view('templates/default', $data);
}
Run Code Online (Sandbox Code Playgroud)
我怎么想这样做?在CI2的文档中,有一个示例来覆盖config的值,如下所示:$this->config->set_item('configItem', 'value');
但是如果我的配置项是一个数组应该怎么样?我试过$this->config->set_item('description', 'head_meta', 'NewValue');但它没用
谢谢你的建议
我的程序将扩展文件gif,png和jpg保存在我的文件夹中,但是,它不保存SVG图像.我想知道它为什么不保存?
我在图书馆的功能:
function uploadImage($idImage, $local){
$config['upload_path'] = $local;
$config['allowed_types'] = 'svg|gif|png|jpg';
$CI =& get_instance();
$CI->load->library('upload', $config);
$CI->upload->do_upload($idImage);
return $CI->upload->data();
Run Code Online (Sandbox Code Playgroud)
}
OBS:$ idImage是一个名为ID HTML的var,它不是数据库的ID!