CodeIgniter extend CI_URI undefined method - php

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();

Related

Class 'Drupal\Component\Utility\XSS' not found (Drupal 8)

In a working Drupal 8 site in THEME.theme I have
function fcx_preprocess_page(&$variables) {
$variables['get']['vguid'] = \Drupal\Component\Utility\XSS::filter($_GET['vguid']);
}
function fcx_preprocess_node(&$variables) {
$variables['get']['vguid'] = \Drupal\Component\Utility\XSS::filter($_GET['vguid']);
}
I have verified that the file core/lib/Drupal/Component/Utility/Xss.php exists with permissions 0664 and declares class XSS. In that file the method is declared public static function filter(...
When accessing pages which reference get.vguid I get error Class 'Drupal\Component\Utility\XSS' not found
I have no idea what further steps I should take. Is the call incorrect? Searching on it seems to suggest the usage is correct...
Having a quick look on the docs the class name is Xss:
namespace Drupal\Component\Utility;
class Xss {}
So call it like:
\Drupal\Component\Utility\Xss

Is autoloading classes from an autoloaded class function allowed in PHP?

I am using Drupal 7 and I am using my custom module's .info file along with the "files[] = ...." directive to allow autoloading of my classes when required. Each class only contains static public functions and everything is working nicely when these functions are called from a function of the general scope.
Working example:
If I go to /devel/php and call the function ClassTwo::getValue() everything is executed without error.
Example with error:
If I call the function ClassTwo::getValue() from within another autoloaded function ClassOne::generate() an exception is thrown that the class ClassTwo() doesn't exist. The error is solved if I specifically include the file containing the class ClassTwo.
I don't believe it has something to do with Drupal's autoloading mechanism though. Is my scenario allowed in PHP 5.3?
For better clarification I have added below example code. That's the nearest I can provide to working code.
mymodule/mymodule.info
name = "MyModule"
core = "7.x"
files[] = "includes/plugins/ClassOne.inc"
files[] = "includes/plugins/ClassTwo.inc"
mymodule/mymodule.module
function mymodule_page_callback() {
return ClassOne::generate();
}
mymodule/includes/plugins/ClassOne.inc
Class ClassOne {
static public function generate() {
$value = ClassTwo::getValue();
// Process $value
return $value;
}
}
mymodule/includes/plugins/ClassTwo.inc
Class ClassTwo {
static public function getValue() {
// Somehow retrieve the value.
return $value;
}
}
UPDATE: Using Drupal's xautoload module with PSR-4 everything works incredibly fine and I have turned to this method as a solution. If anyone has an answer though, other than PSR-0/PSR-4, I would like to know it.

function in codeIgniter help conflicting with another function

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
}
}

CakePHP How to call a controller function from an external 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);

php OOP - call static method from another class within class

I have the following class to another class in my main class.
class Products
{
public function __get( $key ){
return trim(functions::mssql_escape_string_rev($this->fields[ $key ]));
}
}
This beings back error: Call to undefined method functions::mssql_escape_string_rev()
Is there something wrong with my syntax or can this not be done?
Below is code used to autoload classes, this works for everything else so I know there is nothign wrong with the code. It just doesnt seem to initiate within the class.
// autoloader function called when we try to instantiate a class but haven't included the file
function __autoload($resource_name){
$resource_name = trim($resource_name);
try {
$filepath = CLASS_PATH."/class.".$resource_name.".inc.php";
if(#!include($filepath)){
throw new Exception('');
}
} catch(Exception $e) {
exit("Could not find the required file: ".$resource_name);
}
}
*******EDIT*****
Please ignore this, I made a stupid mistake and included the functions::mssql_escape_string_rev twice. Sorry for timewasting..
As the error says the problem is that functions::mssql_escape_string_rev() is not defined.
Since we can't see what you think is the definition we can not really help you.
For me it looks like the call should be Functions::mysql_escape_string_rev() with capital F and mysql.
Update
Calling static functions from another class works normally: http://codepad.org/wrfm5X7j
Maybe you are calling mysql_escape_string_rev before you included the functions class.

Categories