I have blueimp file upload working well with codeigniter. I use the UploadHandler library as is. But I want to extend it to replace two functions that create the unique filename. The code for this is in the BlueImp Github wiki.
I created an extended library thus:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_UploadHandler extends UploadHandler {
public function __construct() {
parent::__construct();
$CI =& get_instance();
$CI->load->library('UploadHandler');
}
protected function upcount_name_callback($matches) {
$index = isset($matches[1]) ? intval($matches[1]) + 1 : 1;
$ext = isset($matches[2]) ? $matches[2] : '';
return '-'.$index.''.$ext;
}
protected function upcount_name($name) {
return preg_replace_callback(
'/(?:(?:-([\d]+))?(\.[^.]+))?$/',
array($this, 'upcount_name_callback'),
$name,
1
);
}
}
When I try to run it, I get an 'unable to load UploadHandler' error. If I remove my MY extension, the original code runs. What is wrong with my extension code? Isn't this the proper way to extend CI libraries?
And, yes, the filename for my file is MY_UploadHandler.php
Thanks!
I noodled over this for a while and finally figured it out. To extend a custom library, you need to 'require once' the file it is extending, prior to the definition of the class.
EX:
require_once("UploadHandler.php");
class MY_UploadHandler extends UploadHandler
{
}
Hopefully that helps.
Related
I'm a very happy user of XCRUD V 1.6. I want to integrate it in Codeigniter 3 but when I use the codeigniter session-library in for my XCRUD-pages I get an error in XCRUD: "The verification key is out of date". When the session-library from codeigniter is not being used, XCRUD is working fine. Is there anyone who can tell me how to fix? I need the codeigniter session-library in my xcrud pages for user-details.
This is my current controller: (the session library is loaded in the login_model)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Cms extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->helper('url');
include('cms/xcrud/xcrud.php');
$this->load->model("login_model");
}
public function nieuws()
{
$xcrud = Xcrud::get_instance();
$xcrud->table('nieuws');
$output['output'] = $xcrud->render();
$this->_cms_output($output);
}
function useAuthorID($post_array) {
$post_array['afdeling_id'] = $this->login_model->id();
return $post_array;
}
function useCatID($post_array) {
$post_array['nieuwscat_afdeling_id'] = $this->login_model->id();
return $post_array;
}
function _cms_output($output = null)
{
$this->load->view('beheer/default',$output);
}
}
Assuming that you are using xcrud's CI integration examples, you may try the following in xcrud_config.php:
Use the same session name that your CI installation is using, if it differs from "PHPSESSID".
Use alternative session.
defined('BASEPATH') OR exit('No direct script access allowed');
require('application/libraries/REST_Controller.php');
use Restserver\Libraries\REST_Controller;
class demo extends REST_Controller {
function __construct(){
parent::__construct();
}
public function demo1_get()
{
echo 'demo 1';
}
}
Error Image
enter image description here
This is My REST_Controller
enter image description here
Hi, I m getting Error for making Codeigniter RESTful API. Please Help me to solve this problem.
It should be like this :
Make sure you have REST_Controller.php in your libraries folder
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require_once APPPATH . '/libraries/REST_Controller.php';
class Demo extends REST_Controller
{
function __construct(){
parent::__construct();
}
public function demo1_get()
{
echo 'demo 1';
}
}
You need to follow the link https://itsolutionstuff.com/post/codeigniter-3-restful-api-tutorialexample.html
and then after you run the code you will get a small error Unable to load the requested language file: language/english/rest_controller_lang.php
The problem is that codeigniter can't find the rest_controller translations. You just need to create this file /application/languages/english/rest_controller_lang.php
Then copy & paste this code inside:
<?php
/*
* English language
*/
$lang['text_rest_invalid_api_key'] = 'Invalid API key %s'; // %s is the REST API key
$lang['text_rest_invalid_credentials'] = 'Invalid credentials';
$lang['text_rest_ip_denied'] = 'IP denied';
$lang['text_rest_ip_unauthorized'] = 'IP unauthorized';
$lang['text_rest_unauthorized'] = 'Unauthorized';
$lang['text_rest_ajax_only'] = 'Only AJAX requests are allowed';
$lang['text_rest_api_key_unauthorized'] = 'This API key does not have access to the requested controller';
$lang['text_rest_api_key_permissions'] = 'This API key does not have enough permissions';
$lang['text_rest_api_key_time_limit'] = 'This API key has reached the time limit for this method';
$lang['text_rest_ip_address_time_limit'] = 'This IP Address has reached the time limit for this method';
$lang['text_rest_unknown_method'] = 'Unknown method';
$lang['text_rest_unsupported'] = 'Unsupported protocol';
Hope this helps
I've spent my whole day trying to figure out this problem. Posting this issue here is my last hope. I hope someone can help to continue working on my first job.
SyntaxError: JSON.parse: unexpected character at line 2 column 1 of the JSON data
So, POST works fine when directly passing data from my views to the RestServer directly. However, RESTServer API is not able to find POSTs data sent from the RestClient.
my Example
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH . 'libraries/REST_Controller.php';
class Example extends REST_Controller {
function __construct()
{
parent::__construct();
$this->methods['get']['limit'] = 500;
$this->methods['post']['limit'] = 200;
$this->methods['delete']['limit'] = 50;
}
public function post()
{
$message = array(
'id' => null,
'name' => $this->input->post('name'),
'message' => $this->input->post('email')
);
$this->set_response($message);
}
}
First off:, try renaming your POST function from post() to index_post() that is simply a standard that can help with SOLID principles.
Second:
Is there in your Controller somewhere whare you put the use CI_Controller namespace either you added it in the REST_Controller.php file, since it is not in the Example controller; just REMOVE that namespace. The below 3 requirements should work just fine for you.
require(APPPATH . 'libraries/REST_Controller.php');
use Restserver\Libraries\REST_Controller;
require (APPPATH . 'libraries/Format.php');
class Example extends REST_Controller {
...
}
I've been using this rolling-curl library with success outside of Codeigniter http://code.google.com/p/rolling-curl/source/browse/#svn%2Ftrunk
I want to use the library with Codeigniter but cannot get it to work.
I've created a file called Rollingcurl.php and placed in codeigniter's application/library folder. In this file I've copied the originals RollingCurl.php file's content (see RollingCurl.php file in link posted above).
My controller looks like this:
class Rolling_curl extends CI_Controller {
function __construct()
{
parent::__construct();
}
function index(){
$this->load->library('rollingcurl', 'request_callback');
$this->rollingcurl->request("http://www.google.com");
$this->rollingcurl->execute();
}
function request_callback($response, $info, $request) {
// parse the page title out of the returned HTML
if (preg_match("~<title>(.*?)</title>~i", $response, $out)) {
$title = $out[1];
}
echo "<b>$title</b><br />";
print_r($info);
print_r($request);
echo "<hr>";
}
In the index function I'm loading the rollingcurl library I've created from the orginal RollingCurl.php and passing the parameter 'request_callback', which is the name of the other function in the controller (see orginal rolling-curl example: http://code.google.com/p/rolling-curl/source/browse/trunk/example.php).
But, it doesn't work... What am I doing wrong?
i just created my own library on this folder (application/library) and following all steps to create individual library,
once i load this library in my controller it execute the function, but when trying to pass it to the view, nothing return
here is my code
MY OWN FUNCTION
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Common {
public function date_arabic()
{
$daysarabic=array('الأحد','الاثنين','الثلاثاء'
,'الأربعاء','الخميس','الجمعة','السبت');
$monarabic=array('','يناير','فبراير','مارس',
'أبريل','مايو','يونيو','يوليو'
,'أغسطس','سبتمبر','أكتوبر','نوفمبر','ديسمبر');
$date=getdate(time());
echo $daysarabic[$date['wday']].' '.$date['mday'].' '.$monarabic[$date['mon']].' '.$date['year']/*.' الوقت الأن '.$date['hours'].':'.$date['minutes'].':'.$date['seconds']*/;
}
}
MY Controller
//arabic date
$this->load->library('Common');
$this->common->date_arabic();
here it prints out the data in my own function, i tried to store this in a $data to pass it to the view like that
//arabic date
$this->load->library('Common');
$data['date_arabic'] = $this->common->date_arabic();
...
$this->load->view('home_page.php', $data);
then when going to view i just type
<?php echo $date_arabic ; ?>
but nothing returned
In your function, change the last line from this:
echo $daysarabic[$date['wday']].' '.$date['mday'].' '.$monarabic[$date['mon']].' '.$date['year']/*.' الوقت الأن '.$date['hours'].':'.$date['minutes'].':'.$date['seconds']*/;
to this:
return $daysarabic[$date['wday']].' '.$date['mday'].' '.$monarabic[$date['mon']].' '.$date['year']/*.' الوقت الأن '.$date['hours'].':'.$date['minutes'].':'.$date['seconds']*/;
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();
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();
}
}