我正在尝试为我的Codeigniter应用程序编写一个钩子.
我想抓住session我的hook.
这是加载钩子的代码:
$hook['pre_controller'] = array(
'function' => 'getNav',
'filename' => 'LoadNav.php',
'filepath' => 'hooks'
);
Run Code Online (Sandbox Code Playgroud)
这是我试图在钩子中加载的代码:
function getNav()
{
$CI =& get_instance();
$level = $CI->session->userdata('level');
}
Run Code Online (Sandbox Code Playgroud)
它不断抛出以下错误:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: hooks/LoadNav.php
Line Number: 7
Run Code Online (Sandbox Code Playgroud)
我知道我做错了什么?似乎get_instance方法无法正常运行?
任何帮助将不胜感激,谢谢
阿兰
我正在使用Codeigniter的验证类来验证我的表单.如果发现任何验证错误,您能否告诉我如何从控制器重定向到上一页?
在我的控制器中:
if ($this->form_validation->run() == FALSE){
//**** Here is where I need to redirect
} else {
// code to send data to model...
}
Run Code Online (Sandbox Code Playgroud) 我非常喜欢CodeIgniter的Active Record,以及它如何很好地允许我所有需要的数据库查询.
但我也一直在阅读像Doctrine这样的ORM.当我阅读Doctrine的文档时,使用Active Record似乎并不清楚,我看不出是什么让它更好(如果是).
什么是Doctrine允许使用Active Record无法实现的?Doctrine能否更快,更轻松,更好地完成同样的工作?或者它做的事情Active Record不能做?
如果人们可以发布显示我们正在谈论的内容的任务示例,那将是最好的.
谢谢,马修
我正在使用MS SQL Server和带有Active Record的CodeIgniter 2来处理我正在进行的项目,我只是偶然发现了这个问题:
当我提交包含中文或印地文字符的表单时,我将其存储在一个表中,当我查看它时,我得到的是问号.如果我尝试英文或希腊字符,一切似乎都很好.
我之所以认为这与我正在编写的PHP有关,是因为如果我直接在SQL Server Management Studio中复制粘贴中文文本,所有值都会在SQL Studio和SQL Studio上完美地存储和显示Web应用程序.
这些是我正在使用的数据库设置:
$db['local']['dbdriver'] = 'sqlsrv';
$db['local']['dbprefix'] = '';
$db['local']['pconnect'] = FALSE;
$db['local']['db_debug'] = TRUE;
$db['local']['cache_on'] = FALSE;
$db['local']['cachedir'] = '';
$db['local']['char_set'] = 'utf8';
$db['local']['dbcollat'] = 'utf8_general_ci';
$db['local']['swap_pre'] = '';
$db['local']['autoinit'] = TRUE;
$db['local']['stricton'] = FALSE;
Run Code Online (Sandbox Code Playgroud)
这是我正在测试的表的结构:
CREATE TABLE [dbo].[languages](
[id] [int] IDENTITY(1,1) NOT NULL,
[language] [nvarchar](1024) NULL,
[language_local] [nvarchar](1024) NULL,
[lang_code] [nvarchar](100) NULL,
[core] [bit] NULL,
CONSTRAINT [PK_languages] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE …Run Code Online (Sandbox Code Playgroud) 我想使用表单验证来要求密码具有BOTH字母和数字字符.这是我到目前为止所提出的:
$this->form_validation->set_rules('password', 'Password', 'required|matches[passconf]|min_length[8]|alpha_numeric');
Run Code Online (Sandbox Code Playgroud)
问题是"alpha_numeric"要求密码只包含字母或数字,它不需要两者.在这里寻求更强大的密码选项.
我正在为我正在开发的移动应用程序创建一个Web服务后端.(我是Obj-C开发人员,不是网页设计师!)基本上我想使用Codeigniter和Phil Sturgeon的RESTful API服务器https://github.com/philsturgeon/codeigniter-restserver但是,我有一些无法完成所有设置和工作.
我有MySQL数据库,其中设置了数据.我需要一些帮助来编写CodeIgniter PHP模型和控制器,它返回该数据库内部的JSON数据.我发现所有的教程和论坛帖子都是在控制器中处理硬编码数据,而不是MySQL数据库.理想情况下,我希望网址格式为http://api.mysite.com/v1/search?id=1&name=foo&city=bar,我可能会有50多个参数传递到网址中.
使用Phil的代码,我想出了这个作为我的控制器:
public function index_get()
{
if (!$this->get('id'))
{
$this->response(NULL, 400);
}
$data = $this->grid_m->get_id($this->get('id'));
if ($data)
{
$this->response($data, 200);
}
else
{
$this->response(NULL, 404);
}
}
Run Code Online (Sandbox Code Playgroud)
这只能得到一个搜索词:id?=#..我需要知道如何获得多个搜索词
这是我的Codeigniter模型:
<?php
class Grid_m extends CI_Model
{
function get_all()
{
$query = $this->db->get('grid');
if ($query->num_rows() > 0)
{
return $query->result();
}
return FALSE;;
}
Run Code Online (Sandbox Code Playgroud)
这只是在我的MySQL数据库中返回一切,无论我在URL中传递了什么id或url术语.
在开发我自己的自定义API时,我是一个很大的菜鸟,因此任何关于如何修复我的控制器和数据库模型的建议都将是一个巨大的帮助!
谢谢您的帮助!
布赖恩
我想要做的是保护一些敏感表单免受CSRF攻击,codeigniter但不是所有页面.
为了防止CSRF我在config.php中设置它,它适用于所有页面.是否有任何方法只通过在控制器中设置一些页面?
$config['csrf_protection'] = TRUE;
Run Code Online (Sandbox Code Playgroud) 我正在编写一个自定义的post_controller钩子.我们知道,codeigniter uri结构是这样的:
example.com/class/function/id/
Run Code Online (Sandbox Code Playgroud)
和我的代码:
function hook_acl()
{
global $RTR;
global $CI;
$controller = $RTR->class; // the class part in uri
$method = $RTR->method; // the function part in uri
$id = ? // how to parse this?
// other codes omitted for brevity
}
Run Code Online (Sandbox Code Playgroud)
我浏览了核心的Router.php文件,这让我很困惑.
谢谢.
我使用的是CodeIgniter 2.1.4版本.我在显示表单验证错误时遇到问题.表单验证正在返回false但未validation_errors()显示任何错误.我试过echo进去controller,view但没有结果.我通过ajax.no提出请求错误被抛出.
控制器:
<?php
class Dashboard extends Admin_Controller {
public function ajax_new_dist_center($id=NULL)
{
$this->load->model('dist_centre_m');
$this->load->helper(array('form', 'url'));
$this->load->library('Form_validation');
$validation = $this->dist_centre_m->rules;
$this->form_validation->set_error_delimiters('<li>', '</li>');
$this->form_validation->set_rules($validation);
if ($this->form_validation->run() == TRUE)
{
if($this->dist_centre_m->create($id))
echo 'New centre created';
else
{
$this->output->set_status_header('404');
echo 'Given center not found';
}
}
else
{
$this->output->set_status_header('400');
echo 'validation Failed';
$this->load->view('alert_error');
}
}
}
Run Code Online (Sandbox Code Playgroud)
视图:
<?php echo validation_errors();?>
<p>Testing Error</p>
Run Code Online (Sandbox Code Playgroud)
模型:
class dist_centre_m extends MY_Model {
protected $_table_name = …Run Code Online (Sandbox Code Playgroud) 我是 codeigniter 的新手。在每次采访中都询问了有关 hooks 的问题。我不明白什么是钩子为什么我必须使用它?它有什么好处。
codeigniter-2 ×10
codeigniter ×7
php ×6
validation ×2
activerecord ×1
api ×1
csrf ×1
database ×1
doctrine ×1
hook ×1
orm ×1
passwords ×1
unicode ×1