In codeIgniter I auto load the url_helper.php
In my site I also have a phpbb forum and so within codeigniter im trying to include a script from the forum.
The problem is, phpbb tries to declare a function redirect() but its already declared in the url_helper.php so i get the following error
Cannot redeclare redirect() (previously declared in
C:\Apache24\htdocs\system\helpers\url_helper.php:531) in
C:\Apache24\htdocs\forum\includes\functions.php on line
2562
What can I do go go around this? Can I unset the function or remove the url_helper entirly in my controller function?
Still a bit of a hack, but see: http://php.net/manual/en/function.rename-function.php
You could create your own url_helper, include the CI url_helper, and call after include:
rename_function('redirect', 'ci_redirect');
Ok, I got a work around. In the codeigniter's helper library, before declaring a function, it first checks if it has been declared before or not. So....
In my controller class's constructor method, I load all the phpbb files I need. this way it declares the phpbb redirection function and codeigniter goes "ohh there is already a redirect function" and so it doesn't declare the redirect function... Problem solved
Something like this:
class Register extends CI_Controller{
public function __construct()
{
/* START phpbb */
.
.
.
require_once('forum/common.php');
require_once('forum/includes/functions_user.php');
require_once('forum/includes/functions_module.php');
/* END phpbb */
//Continue as normal
parent::__construct();
}
public function index(){
//Your stuff works as normal now
}
}
Related
So I've been in the process of updating someones old CI 1 to CI 3 code. In process. In particular, the URI class extension is not working. I've read the CI documentation switched to __construct() and moved it to the application/core directory. I've checked SO and all cases are correct, but I still get the following error:
Call to undefined method MY_URI::last()
My code below
class MY_URI extends CI_URI {
function __construct()
{
parent::__construct();
}
function last()
{
return $this->segment(count($this->segments));
}
}
Thoughts as to why this may be happening with the switch? Checking StackOverflow it said chek your config settings by the config has the correct
$config['subclass_prefix'] = 'MY_';
I'm calling it with:
$lastURI = $this->uri->last();
Update: I've also tried the
exit('MY_URI.php loaded');
trick at the top which seems to work, but it still throws the error when I remark it out and never loads the extension.
Place your MY_URI.php file inside the application/core/MY_URI.php & update the function like following.
public function last(){
return $this->segment($this->total_segments());
}
call it like below
$last = $this->uri->last();
I am trying to create my own library so i can handle custom areas in my application. i have a small library located at application/libraries which i called randomizer. it looks like this:
Class Randomizer {
public function __construct()
{
parent::_construct();
$CI =& get_instance();
$CI->load->library('session');
$CI->load->database();
}
function test_function($name)
{
return 'Hi dear' . $name . 'welcome back!';
}
}
In my Controller i tried to test out the simple test_function:
public function index()
{
echo($this->Randomizer->test_function('John'));
exit;
}
And I am getting the following error
Call to a member function test_function() on a non-object
There are couple of possible errors i can see. first you are not loading the library you created inside your controller. in case you don't use $this->load->library('randomizer'); right before you call the library funcion. if you are going to use the library all over the controller then load it via the controller __consturct. Also i guess you are not extending an existing Class so the parent::_construct() is not needed. make sure you understand why you are using a library and when you should use helpers. Read more about Codeigniter Libraries
Please look closely to the documentation. There are a few problems.
First of all it seems that you have either not loaded the library ($this->load->library('randomizer')), otherwise you would get a syntax error because your are calling parent::_construct(), instead of parent::__construct. And you do not have to call that function anyway, because your class doesn't have a parent (it doesn't extend a class...).
Furthermore, although you declare your class with a capital, the variable will be lowercase. So you should call $this->randomizer->test_function()
I'm having trouble with accessing the session in an external .php script located in webroot.
Thought I'd write a function getSession() in one of my controllers and try to call it in the .php file.
So in steps:
I have file.php
In a controller I have a function getSession().
How to call the controllers function in the file.php?
Thank you.
EDIT
Meanwhile I fixed my bug, but still am curious how this is done and want other stack users to find a good answer to this so:
Its exactly like this:
In UsersController I have a function:
public function getSession() {
return $_SESSION['Auth']['User']['user_id'];
}
That I want to let's say print (for example) like this: print_r(Users.getSession) in the file test.php located in webroot/uploadify/test.php.
This file is not a class, but if it is required, then it shall be :)
#CaboOne: Maybe your answer was correct, I just wasnt sure what code to call (and enter) where :)
Supposed I have the following php file in webroot folder:
<?php
class TestingClass {
function getName(){
return "Test";
}
}
?>
I would do the following:
// This would bring you to your /webroot folder
include $_SERVER['DOCUMENT_ROOT'].'/another_file.php';
// Initializing the class
$example = new TestingClass;
// Call a function from the initialized class
$a_value = $example->getName();
// If you want to use $a_value in the view, you can then set
$this->set('a_value', $a_value);
I want to save some global vars to use in the website, like current user id, current user lever, and so on. Where is the best place to do it, or is it possible?
Setting it into constants.php is not working since "$this" is not recognized there.
The principal reason why I want this is because i don't like using sessions (I consider writing strings like $this->session->userdata('session_name') not so practical, writing something like CURR_UID is more easy to do it and read as well)
It's possible, but it isn't the way that Codeigniter was designed. Sessions are really the place for this kind of thing (namely, stuff that persists from one page view to the next), but you could wrap the session calls up in a library for beauty's sake if you wanted. Something like this:
// in libraries/User.php
class User {
protected $ci;
public function __construct() {
$this->ci = &get_instance();
}
public function id() {
return $this->ci->session->userdata('user_id');
}
// etc, etc.
}
Once you've written a few more helpers like id(), you can use them to access the relevant variables elsewhere in your application:
$this->load->library('user');
echo 'Current user ID is: ' . $this->user->id();
What you can do in this case is create a class My_Controller extends CI_Controller. Sort out all the functionality that you would need before actually loading any of the specific controller functionality.
Then any subsequent class you create you can do: class Whatever extends My_Controller.
Edit: I forgot to mention you should put the My_Controller class within the Application > Core folder.
I'm just starting with OOP (shame on me), so be gentle with me.
I have an ErrorHandler class that calls a function from my main Application class to include an error page.
So I'm using Application::status_page( $type ); in a function in the ErrorHandler class.
This is how the status_page function looks like (it's a function to include all kinds of custom messages):
public function status_page( $page )
{
// Include the status page that has been set in the routes
include( STATUS_PAGE_DIR . $this->status_pages[$page] . '.html' );
}
I'm now getting an Undefined property: ErrorHandler::$status_pages which makes total sence to me. But what is the best way to solve this? Maybe let the ErrorHandler class extend the main Application class?
I hope I was clear and thanks in advance for answering.
$status_pages must be defined in the header of the class
also, you must declare the function as
public static function status_page($page)
if you want to use it like that.