I have a problem, everything was working but then I tried to put my functions in my own libraries (to use them in different controllers) and it doesn't work.
I have SIGNUP controller with this:
$this->load->library('Check_functions');
// We check the form
$return_verif_form_signup = $this->check_functions->verif_form_signup($language);
which calls my librarie Check_functions:
class Check_functions {
public function verif_form_signup($language) {
if ($this->input->post()){
// Verification rules
$this->form_validation->set_rules('name', 'lang:name', 'trim|required|xss_clean');
....
if ($this->form_validation->run($this)) {
extract($_POST);
...
...
}
But I get the error:
Fatal error: Call to a member function post() on a non-object
Does anyone know how I could fix it?
thanks!
EDIT:
I have found the problem, the callback function is not called. If I replace callback_free_email by REQUIRED and I don't enter an email, my form is not submitted, so it's okay.
But if I have the following code, my form is always submitted. So the callback function is never called...
This is my code (i'm using HMVC):
class Check_functions {
private $CI;
public function __construct(){
$this->CI =& get_instance();
}
public function verif_form_signup($language) {
if ($this->CI->input->post()){
$this->CI->form_validation->set_rules('name', 'lang:field_name', 'trim|required|min_length[3]|max_length[25]|xss_clean');
$this->CI->form_validation->set_rules('email_signup', 'lang:field_email', 'callback_free_email');
...//other rules
if ($this->CI->form_validation->run($this->CI)) {
.....
}
}
}
public function free_email($str) {
return FALSE; // I have temporarly set that so I see if my function is called
}
}
I have a file called MY_Form_validation.php as suggested here: http://codeigniter.com/forums/viewthread/143057/#769347
class MY_Form_validation extends CI_Form_validation{
function run($module = '', $group = ''){
(is_object($module)) AND $this->CI = &$module;
return parent::run($group);
}
}
I really don't know what's wrong... why my callback function is not called?
Thank you for your help!
when you are writing libraries, you have to manually grab the Codeigniter instance like this
$CI =& get_instance();
then you would use $CI where you would normally use $this to interact with loaded codeigniter resources
so...
instead of
$this->input->post();
you would write
$CI->input->post();
Docs explain it here http://codeigniter.com/user_guide/general/creating_libraries.html
EXAMPLE LIBRARY STRUCTURE
class Examplelib {
// declare your CI instance class-wide private
private $CI;
public function __construct()
{
// get the CI instance and store it class wide
$this->CI =& get_instance();
}
public function lib_function()
{
// use it here
$this->CI->db->etc()
}
public function another_func()
{
// and here
$this->CI->input->post();
}
}
I finally found a workaround, instead of using the callback in my rule, I do a test later and call a verification function.
It’s works well like that.
Thanks for your help!
Related
I have created a function in my views and have tried to make a query within the php function but i do not know I am getting and unexpected error which was never been seen in my entire career can anyone help me out to get rid from this error?
Severity: Error --> Using $this when not in object context /application/views/includes/create_calendar.php 51
This is my controller
class Booking extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array("form", "url"));
$this->load->library('session');
$this->load->database();
}
public function get_calendar() {
$this->load->view('includes/create_calendar');
}
}
This is the file in which I am trying to run a query views/includes/create_calendar.php
function getCalender($year = '',$month = '') {
$sql = $this->db->get('calendar');
if($day == $sql->row()->day_off) { $per_off = $sql->row()->per_off; }
echo "<p>".$day." Off</p>";
}
In CodeIgniter usage, if you need to use one of the classes that is available in the "super object", you must first get an instance of the super object:
$CI =& get_instance();
$CI->db->get('calendar');
This DB class may not even be loaded. If that's the case, then before you use db:
$CI->load->database();
To clarify, as soon as you go in that function, you've removed $this from the variable scope, like this simple test:
class Test {
public $foo = 'bar';
public function index() {
function baz(){
return $this->foo;
}
echo baz();
}
}
$t = new Test;
$t->index();
You could do something like the following, but that's really messy:
class Test {
public $foo = 'bar';
public function index() {
function baz($x) {
return $x->foo;
}
echo baz($this);
}
}
$t = new Test;
$t->index();
The best thing for you to do is find another cleaner way to do what you are doing. You wouldn't normally want calls to your database and functions in your views. It goes against what MVC is all about.
Im using phalcon 2.0.0 and i am trying to call a function with in another function but from the same class like shown below, for some reason i get a blank page. And when i comment the calling of 2nd function from first, the page loads properly.
<?php
use Phalcon\Mvc\User\Component;
class Testhelper extends Component {
public function f1($data) {
$tmp = $this->f2($data);
return $tmp;
}
public function f2($data) {
return '5'; // just testing
}
}
And btw im accessing the f1 function by the volt function extender like this
$compiler->addFunction('customfunc', function($resolvedArgs, $exprArgs) {
return 'Testhelper ::f1('.$resolvedArgs.')';
});
if someone could help me, it would be deeply appreciated.
Thanks guys
You are trying to call TestHelper f1() statically in Volt, where your class does not expose that function as a static.
You can change your code like this:
<?php
use Phalcon\Mvc\User\Component;
class Testhelper extends Component
{
public static function f1($data)
{
$tmp = self::f2($data);
return $tmp;
}
public static function f2($data)
{
return '5'; // just testing
}
}
and your Volt function will work. However you have to bare in mind that because you are calling things statically you won't have immediate access to all the di container services that the Component offers like so:
$this->session
$this->db
You will need to modify your code to pick the di container using the getDefault()
Another option is to use the code as you have right now, but register the TestHelper in your di container like so:
$di->set(
'test_helper',
function () {
return new TestHelper();
}
);
and then your volt function will need to change to:
$compiler->addFunction(
'customfunc',
function ($resolvedArgs, $exprArgs) {
return '$this->test_helper->f1('.$resolvedArgs.')';
}
);
I am trying to initialize data in index function of controller, so that initialized data can be used in subsequent functions of controller. But the problem is data is not being displayed when I am trying to access it from other function. All of this is just to follow a sort of object oriented pattern.
Here is my code.
class Dashboard extends CI_Controller
{
private $account_data; /*Declaration*/
private $profile_data;
function __construct() {
// code...
}
function index() /*Here I am initializing data*/
{
$this->load->model('db_model');
$this->account_data = $this->db_model->get_row();
$this->profile_data = $this->db_model->get_row();
$this->load->view('user/dashboard');
}
function function account_details()
{
print_r($this->account_data); // This displays nothing
}
/*other function...*/
}
Idea is to get data once and use it for other functions and if data is updated again calls a function to initialize it.
But it is not working out. Please help me. Also suggest if I am following right approach.
Thanks for your time.
index method is not initializer, its default page/sub_method,
if you call the "*account_details*" in url as index.php/dashboard/account_details the index wont be called.
try put the code on constructor,
class Dashboard extends CI_Controller
{
private $account_data; /*Declaration*/
private $profile_data;
function __construct() { /*Here I am initializing data*/
parent::CI_Controller(); // Thank you Sven
$this->load->model('db_model');
$this->account_data = $this->db_model->get_row();
$this->profile_data = $this->db_model->get_row();
}
function index()
{
$this->load->view('user/dashboard');
}
function function account_details()
{
print_r($this->account_data); // This displays nothing
}
/*other function...*/
}
Note : don't the models or other computations on __construct() if you don't need on all methods of this controller.
create a private method like "model_initializer()" put this codes on this scope, and the call it in your other methos as $this->model_initialize(); if you need.
Thanks yo Sesama Sesame for note,
For my current project i decided to create a library for some common functionalities.
Ex : Login_check,get_current_user etc.
With my little knowledge i created a simple one but unfortunately its not working.
Here my library :
FileName : Pro.php and located in application/libraries
class Pro{
public function __construct()
{
parent::_construct();
$CI =& get_instance();
$CI->load->helper('url');
$CI->load->library('session');
$CI->load->database();
}
function show_hello_world()
{
$text = "Hello World";
return $text;
}
}
?>
And i tried to load it on my controller :
<?php
class Admin extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->library(array('session'));
$this->load->library("Pro");
}
function index()
{
echo($this->Pro->show_hello_world());
}
}
?>
I cant see any erros there...but i am getting a blank page.
Whats wrong with me ??
Thank you .
Edit : I got this error :
Call to a member function show_hello_world() on a non-object in C:\wamp\www\Project\application\controllers\admin.php on line 13
One thing I notice: remove the parent::__construct() from your library constructor, because it's not extending anything so has no parent to call.
Also, enable error reporting by setting the environment to "development" in index.php, and you might also want to raise the logging threshold to 4 in config/config.php so you log errors.
Try this simple test-case:
file Pro.php in application/libraries:
class Pro {
function show_hello_world()
{
return 'Hello World';
}
}
Controller admin.php in application/controllers
class Admin extends CI_Controller
{
function index()
{
$this->load->library('pro');
echo $this->pro->show_hello_world();
}
}
while your class name is capitalized, all your references to the library when loading it and using it should be lower case. you also do not need the constructor, as the other commenter mentioned.
so instead of:
echo($this->Pro->show_hello_world());
you should have:
echo($this->pro->show_hello_world());
I prefer the standard php autoloader approach, with this you dont need to change your classes at all, you can use your standard classes without modifications
say for instance you class is class 'Custom_Example_Example2' and is stored in libraries
in sub folders you can add this autoloader in the master index.php
make sure it is added below the defined APPPATH constant
//autoload custom classes
function __autoload($className) {
if (strlen(strstr($className, 'Custom_')) > 0 ||
strlen(strstr($className, 'Other1_')) > 0 ||
strlen(strstr($className, 'Other2_')) > 0) {
$exp = explode('_', $className);
$file = APPPATH.'libraries';
if(!empty($exp)) {
foreach($exp as $segment) {
$file .= '/'.strtolower($segment);
}
}
$file .= '.php';
require_once $file;
//debug
//echo $file.'<br />';
}
}
This will look for class calls matching the 'Custom_' prefix
and reroute them to the relative location in this case
you only need to define the base prefix not the sub folders / classes
these will be auto detected by this code
APPPATH.'libraries/custom/example/example2.php'
You can call it in the controller the standard php way
$class = new Custom_Example_Example2;
or
$class = new custom_example_example2();
You can modify the script to your liking currently it expects all folders and filenames in the library to be lowercase but you can remove the strtolower() function to allow multiple casing.
you can change the require once to echo to test the output by uncommenting this line and refresh the page, make sure you have a class init / test in the controller or model to run the test
echo $file.'<br />';
Thanks
Daniel
In Pro.php
class Pro{
protected $CI;
public function __construct() {
$this->CI = & get_instance();
}
public function showHelloWorld(){
return "Hello World";
}
}
In your controller
class Staff extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->database();
$this->load->helper(array('url_helper', 'url'));
$this->load->library("pro");
}
public function index() {
echo $this->pro->showHelloWorld();die;
}
}
Just do these things you can access your custom library in codeignitor.
I have a PHP web application built with CodeIgniter MVC framework. I wish to test various controller classes. I'm using Toast for unit testing. My controllers have no state, everything they process is either saved into session or passed to view to display. Creating a mock session object and testing whether that works properly is straightforward (just create a mock object and inject it with $controller->session = $mock).
What I don't know, is how to work with views. In CodeIgniter, views are loaded as:
$this->load->view($view_name, $vars, $return);
Since I don't want to alter CI code, I though I could create a mock Loader and replace the original. And here lies the problem, I cannot find a way to derive a new class from CI_Loader.
If I don't include the system/libraries/Loader.php file, the class CI_Loader is undefined and I cannot inherit from it:
class Loader_mock extends CI_Loader
If I do include the file (using require_once), I get the error:
Cannot redeclare class CI_Loader
Looks like CI code itself does not use require_once from whatever reason.
Does anyone here have experience with unit testing CodeIgniter powered applications?
Edit: I tried to inject a real loader object at run-time into a mock class, and redirect all calls and variables with __call, __set, __get, __isset and __unset. But, it does not seem to work (I don't get any errors though, just no output, i.e. blank page from Toast). Here's the code:
class Loader_mock
{
public $real_loader;
public $varijable = array();
public function Loader_mock($real)
{
$this->real_loader = $real;
}
public function __call($name, $arguments)
{
return $this->real_loader->$name($arguments);
}
public function __set($name, $value)
{
return $this->real_loader->$name = $value;
}
public function __isset($name)
{
return isset($this->real_loader->$name);
}
public function __unset($name)
{
unset($this->loader->$name);
}
public function __get($name)
{
return $this->real_loader->$name;
}
public function view($view, $vars = array(), $return = FALSE)
{
$varijable = $vars;
}
}
Alternatively, you could do this:
$CI =& get_instance();
$CI = load_class('Loader');
class MockLoader extends CI_Loader
{
function __construct()
{
parent::__construct();
}
}
Then in your controller do $this->load = new MockLoader().
My current solution is to alter the CodeIgniter code to use require_once instead of require. Here's the patch I'm going to send to CI developers in case someone needs to do the same until they accept it:
diff --git a/system/codeigniter/Common.php b/system/codeigniter/Common.php
--- a/system/codeigniter/Common.php
+++ b/system/codeigniter/Common.php
## -100,20 +100,20 ## function &load_class($class, $instantiate = TRUE)
// folder we'll load the native class from the system/libraries folder.
if (file_exists(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT))
{
- require(BASEPATH.'libraries/'.$class.EXT);
- require(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT);
+ require_once(BASEPATH.'libraries/'.$class.EXT);
+ require_once(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT);
$is_subclass = TRUE;
}
else
{
if (file_exists(APPPATH.'libraries/'.$class.EXT))
{
- require(APPPATH.'libraries/'.$class.EXT);
+ require_once(APPPATH.'libraries/'.$class.EXT);
$is_subclass = FALSE;
}
else
{
- require(BASEPATH.'libraries/'.$class.EXT);
+ require_once(BASEPATH.'libraries/'.$class.EXT);
$is_subclass = FALSE;
}
}
I can't help you much with the testing, but I can help you extend the CI library.
You can create your own MY_Loader class inside /application/libraries/MY_Loader.php.
<?php
class MY_Loader extends CI_Loader {
function view($view, $vars = array(), $return = FALSE) {
echo 'My custom code goes here';
}
}
CodeIgniter will see this automatically. Just put in the functions you want to replace in the original library. Everything else will use the original.
For more info check out the CI manual page for creating core system classes.
I'm impressed by the code you are trying to use.
So now I'm wondering how the 'Hooks' class of CodeIgniter could be of any help to your problem?
http://codeigniter.com/user_guide/general/hooks.html
Kind regards,
Rein Groot
The controller should not contain domain logic, so unit tests make no sense here.
Instead I would test the controllers and views with acceptance tests.