Always getting "status":false while using rest api in codeigniter 2.0 - php

I am using Ashimante Rest Server API in my project.
This is what I did:-
I have put the Rest.php controller in the application/libraries
folder
I have created webservice-v1 folder inside the
application/controllers/ folder i.e. application/controllers/webservice-v1
I have Controller files rest_api_common.php and
webservice_test.php inside the webservice-v1 folder.
This is my rest_api_common.php code:-
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require(APPPATH . 'libraries/Rest.php');
class Rest_api_common extends REST {
// some variable declarations //
public function __construct() {
parent::__construct();
// loading models and custom config files//
}
}
I want the variables and functions available to all the webservice functions. So my webservice_test.php file code goes like this:-
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
include('rest_api_common.php');
class Webservice_test extends Rest_api_common
{
function __construct(){
parent::__construct();
}
public function mongodata_post()
{
$this->response(array(),200);
}
}
I have defined route like this:-
$route['rest-api/v1/mongodata'] = "webservice-v1/webservice_test/mongodata_post";
Using postman app, I am calling the API like this using POST method:-
http://testdomain.com/rest-api/v1/mongodata
I am getting this response:-
{"status":false,"error":false}
What am I doing wrong?

Related

CodeIgniter - Hyperlink creation

I have tried to create a hyperlink to another page using anchors however it doesn't seem to work.
My function is called calendar and its stored in calcontrol.php within a controllers directory.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Calendar extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->helper('url');
}
public function index()
{
$this->load->view('calendar');
}
}
?>
I have tried other methods but they do not seem to work.
edit : this is my a href
Link
Filename and classname must be the same, otherwise CI will not be able to load it. And if using CI 3, make sure your class name and filename are both Ucfirst. If you have everything set up like this, then you should be able to view the page through http://yourdomain.com/calendar/calendar
Try this code. You forgot to load url helper. File Calendar.php - nogice capital C if CI3:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Calendar extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper('url');
}
public function index()
{
$this->load->view('calendar');
}
}

Library Not loading in Controller

My library Class in common/auth/auth_manager.php folder :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Auth_manager {
public function __construct() {
$this->allow_dev_login = TRUE;
$this->_ci =& get_instance();
$this->_ci->load->spark('flexi-auth/1.5.0/');
}}
My controller :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
error_reporting(E_ALL);
class Contacts extends MY_Controller {
public function __construct() {
parent::__construct();
echo "string";
$this->load->library('auth/auth_manager');
}
In the above code the string before loading the library is working . But after loading the page is just blank . Have to use the functions from those libraries . If i use the below code
$this->auth_manager->register();
Getting the error property not defined .
your library file at wrong place
According to Codeigniter standard Keep your library in application/libraries/ directory then you can load library
$this->load->library('auth_manager');
For more follow here
or official docs :- https://ellislab.com/codeigniter/user-guide/general/creating_libraries.html
$this->load->library(); method is finding library in /application/libraries or /system/libraries folders, if you want to load library from another location then register third party folder path
$this->load->add_package_path(APPPATH.'common/auth/');
and then user load library method
$this->load->library('auth_manager');

Extended core class not found

I have some codeigniter application. It works well on my local server. After I uploaded it to the hosting server it won't work and resulting an error:
Fatal error: Class 'System_Controller' not found in /home/k2113138/public_html/test/application/controllers/login.php on line 3
Here is my System Controller that extending CI_Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//location: application/core/
class System_Controller extends CI_Controller {
public function __construct(){
parent::__construct();
//some code
}
}
my controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//location: application/controllers/
class Login extends System_Controller {
public function __construct()
{
parent::__construct();
}
}
i have set my config as my desired configuration like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['base_url'] = 'http://xx.com/subfolder/';
$config['subclass_prefix'] = 'System_';
?>
I have read many article that discuss this matter but still cannot found the solution. Really confuse about the problem, because everything works well in local server. I have configured the config file as what it's needed like the database and the routes configuration. Is there any other things that i need to re-configure?
EDIT: My Codeigniter version is 2.1.3
Ok, you changed the filename to lowercase, now, try change the classname too:
class system_controller extends CI_Controller
If don't run, play with the classname and the filename, this is a problem of your server, very common in shared servers.
Sometimes, the PHP version and its configuration, play a important rol in this cases.

Codeigniter libraries - Error loading custom classes

I am unable to load my custom class which doesnot extend from any core class.
I have placed my custom class in a subfolder inside application/libraries.
So here is my folder structure
application
|_ libraries
|_ cgh
|_ cgh_asset.php
|_ cgh_article.php
|_ cgh_asettype.php
|_ controllers
|_ welcome.php
Class Cgh_article is a subclass of Cgh_asset
Cgh_asset.php :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
abstract class Cgh_asset
{
public $id;
public $type;
public $title;
public $content;
public $user;
abstract public function generate_url();
function __construct()
{
$this->generate_url();
}
}
?>
Cgh_article.php :
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Cgh_article extends Cgh_asset
{
function __construct()
{
parent::__construct();
$this->type=Cgh_assettype::article;
}
function generate_url()
{
$this->url="Article_URL";
}
}
?>
Cgh_assettype.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Cgh_assettype
{
const type1="type1";
const type2="type2";
const article="article";
}
?>
Controller welcome.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
$this->load->library('cgh/Cgh_assettype','cgh/Cgh_asset','cgh/Cgh_article');
$this->load->view('welcome_message');
}
}
The error I get is :
Unable to load the requested class: Cgh_assettype
I must have tried all possible upper and lower case combinations of classnames, filenames, but the error is always the same.
After going through some answers, I think probably I should add one basic question here - Is it at all possible to have my own custom object types within codeigniter ... types that should be quite obvious from my question ?
This seems to work for me, so here is what I will be doing ... at least till something breaks :
In the constructor of my controller, I use require_once for my classes ... and good thing is I can combine all my classes into a single file -- my classes initially were in a single file anyways --This is my controller after the changes, and this works :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
public $cgh_assettype;
public $cgh_asset;
public $cgh_article;
function __construct()
{
parent::__construct();
//$this->load->library(array('cgh/cgh_assettype','cgh/cgh_asset','cgh/cgh_article'));
echo "Including CGH<br />";
echo "<p>Apppath is ". APPPATH. "</p>";
require_once(APPPATH.'libraries/cgh/Cgh_assettype.php');
require_once(APPPATH.'libraries/cgh/Cgh_asset.php');
require_once(APPPATH.'libraries/cgh/Cgh_article.php');
}
public function index()
{
$iCgh_article=new Cgh_article();
echo "<p>$iCgh_article->url</p>";
$this->load->view('welcome_message');
}
}
You need to call $this->load->library for each library.
$this->load->library('cgh/Cgh_assettype');
$this->load->library('cgh/Cgh_asset');
$this->load->library('cgh/Cgh_article');
$this->load->library takes 3 parameters.
File to load
(optional) $config array
(optional) String to rename library to ($this->Renamed_library)
If you want to load multiple libraries on one line, use an array as the 1st parameter.
$this->load->library(array('cgh/Cgh_assettype','cgh/Cgh_asset','cgh/Cgh_article'));
Are you library file names capitalized? (Your submitted folder structure says they are not).
I don't know about having libraries within sub-directories, but I know the file names need to be capitalized.
http://codeigniter.com/user_guide/general/creating_libraries.html
Naming Conventions
File names must be capitalized. For example: Myclass.php
Class declarations must be capitalized. For example: class Myclass
Class names and file names must match.
You should load library or whatever only once. If you're loading for second time, you get that error.

codeigniter -> having trouble loading multiple libraries/classes

Ok, so in my base controller (page.php) I have the following code which works fine:
$this->load->library('Siteclass');
$mysite = new site_model();
The siteclass library references a model named site_model and instantiates based on data received from that model. All is good.
Now I want to load another library so that I can instantiate another object as well. So I add this to page.php:
$this->load->library('Memberclass');
$mysite = new member_model();
But now I get the following error:
Message: Undefined property: Memberclass::$site_model
Filename: libraries/Loader.php
Line Number: 1035
From what I can tell, it seems that the loader class, when being applied to the Memberclass, is somehow still referencing the site_model instead of the member_model. I've checked my code and I am definitely calling the correct files.
Here's what Siteclass.php looks like:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Siteclass extends Controller {
function __construct() {
parent::Controller();
$this->load->model('Site_model');
$data = $this->Site_model->load_site_data();
// etc etc
and here's what Memberclass.php looks like:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Memberclass extends Controller {
function __construct() {
parent::Controller();
$this->load->model('Member_model');
$data = $this->Member_model->load_member_data();
// etc etc
Thanks in advance for any help!
Gary
I think you're confused about how MVC works in CodeIgniter. Why are you using the loader class to create a controller? Why are you creating a stand-alone instance of your model outside of your controller class?
In CodeIgniter, your URLs represent paths to your controllers' methods. That means that your "base controller" should automatically be instantiated if you go to:
www.example.com/memberclass
Or perhaps more to the point, if you have a link like this:
www.example.com/page
You should have a file in your /application/controllers directory called page.php which looks like this:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Page extends Controller {
function __construct() {
parent::Controller();
// etc etc
Furthermore, unless you're loading data from your model to be used every single time you call this controller, you'll want to put your model calls inside a non-constructor method of this class. Something like:
class Page extends Controller {
function __construct() {
parent::Controller();
}
function index() {
$this->load->model('Member_model');
$data = $this->Member_model->load_member_data();
$this->load->view('myview', array('data'=>$data));
}
}
So again...not entirely sure what context you're doing this all in, but it seems like you're not standing firmly within the framework. There's basically no reason you should be using the loader class to load controllers, and furthermore there's no reason you should be creating stand-alone instances of model classes using PHP's new keyword.

Categories