call libraries from model in codeigniter - php

I'm trying to call a library from model in codeigniter but doesn't want to fire and I don't really know how to debug it, I have been trying to use codeigniters default logging method but doesn't show anything. My code looks as it follows.
class Model_products extends CI_Model{
function __construct(){
parent::__construct();
$this->load->library('ApiClient');
log_message('error', $this->load->library('ApiClient'));
}
}
/application/libraries/ApiClient.php
libary final class ApiClient
{}

You cannot name a library ApiClient - class names should follow the CI naming convention (which is identical to ucfirst()) and when loading anything in CI you need to load it with lowercase naming.
Naming is the key.
Your file should be named Apiclient.php (libraries has the first letter in uppercase in the filenames, models, views and controller are all lowercase filenaming).
Your class definition should reflect this also:
class Apiclient
{
...
}
And when using the CI instance to load the library, you need to load it with lowercase naming:
$this->load->library('apiclient');

You miss a semicolon at the end of your line $this->load->library('ApiClient'). Could this be it?

Related

how to use another controller function without extends in our controller

how to use another controller function without extends in our controller
$this->load->library('../controllers/controllername');
already used
it is giving error =
Unable to locate the specified class: Session.php
Well you are not supposed to do that. If your controller uses repeatable logic, you should make class (Service for example), put the re-usable logic into it and call it in your controllers.
You can't use another controller function inside the controller. You can archive this in these two ways.
Create a Helper class
Create a generic model.

CI Model Access -> Unable to locate the model you have specified when model name is specified in lower case letters

Everytime the controller tries to access home model, It displays
Unable to locate the model you have specified: HomeModel
error.
The controller has no problem in accessing view.
And it was able to access the model class after I changes the model name to lowercase. Can somebody explain why this happens?
When creating a model on codeigniter
classes and file names should have there first letter as upper case this also applies for controllers and libraries etc.
Home_model.php
class Home_model extends CI_Model {
public function __construct() {
parent::__construct();
}
public function some_function() {
}
}
How to load model on controller example only
class Welcome extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('home_model');
// Or In a subfolder application / models / subfoldername
$this->load->model('subfoldername/home_model');
}
public function index() {
$data['results'] = $this->home_model->some_function();
$this->load->view('welcome_message', $data)
}
}
refer to Correct naming structure for CodeIgnitor
URLs
Your URLs should typically be all lowercase letters. If you expect capital letters, there's a chance you could accidentally exclude their lowercase counterparts, even though they're the same URL. Example: www.example.com/controller/method/param
Controllers
Controller class names should be all lowercase, except the first letter.
If your URL is www.example.com/gallery, the controller name is Gallery.
If your URL is www.example.com/admin_folder, the controller name is Admin_folder.
Controller file names should match the class name, but be all lowercase.
Gallery :: gallery.php
Admin_folder :: admin_folder.php
Controller methods should be all lowercase as well. There is some flexibility with uppercase, but similar to URLs, there are opportunities where it can goof something up (here's an example where capital letters interfered with a form validation callback method).
Models
Models follow most of the same conventions as controllers. The only difference is with model method names, which can use your preference of capitalization. Since these methods are not tied to URLs, and are called using normal PHP OOP, you can name them as you please.
It is recommended to load models using the all lowercase version. While it is not required by CI, it can confuse some users if they load it with a capital letter, but then attempt to access it as all lowercase (this is due to native PHP being case sensitive with class properties [and variables in general], not CodeIgniter).
Model class name: Users_model (the _model suffix is also not required, but some people may use it as a personal preference, or to prevent naming conflicts with a Users controller).
Model file name: users_model.php
Model loading: $this->load->model('users_model')
Model method names (all okay): $this->users->getAll(), $this->users->find_by_name($name), etc.
Libraries
Libraries follow the same conventions except for the file name. In their case, file names should match the class name.
Similar to models, it's recommended to load libraries using the lowercase name.
These rules are the same for CI's libraries (located in application/core and application/libraries, as well as custom or third-party libraries.
Special note: when extending default CI libraries, the prefix as defined in application/config.php comes into play. This prefix typically should be all uppercase, followed by an underscore. The default is MY_.
Library class name: Photos
Library file name: Photos.php,
Library load: $this->load->library('photos')
Helpers
Helper names and loading are all lowercase. The filename consists of the helper name with _helper appended after.
Helper name: url
Helper file name: url_helper.php
Helper load: $this->load->helper('url')
Notes
CodeIgniter is somewhat inconsistent in their naming conventions, but there really aren't too many rules, so they are easy to get used to and memorize. I very rarely have issues with naming and loading in CI, and when I do, it's usually because I was just working on a Composer-related project so I got into a different habit.
The rules in this answer are for CodeIgniter 2.1.x as of this writing. There is discussion on Github for 3.0 to better and add more consistency to naming conventions, which you can read about and contribute to if you'd like.

parent controll in Codeigniter 2.2 project not found

In my CI 2.2 project I want to make my parent controll with app's common functionality for use in all app and for this I create file :
application/libraries/N_Controller.php :
<?php
class N_Controller extends Controller
{
public function __construct()
{
parent::__construct();
}
But on first attempt to use it in file
application/controllers/admin/admin.php
<?php
class Admin extends N_Controller
{
public function __construct()
{
parent::__construct();
I got error:
PHP Fatal error: Class 'N_Controller' not found in /controllers/admin/admin.php on line 3, referer: http://local-ci22.com/admin/hostel/edit/15
I tried to add in application/config/autoload.php file :
$autoload['libraries'] = array( 'AppSmarty', 'AppUtils', 'N_Controller');
But it did not help. Which is the correct way ?
if you want to extend the functionality of a system class. you need to follow this recomendations.
place your extended class in /application/core, be sure that you name it exactly like the name and casing of the class created.
your class should extend from CI_Model or CI_Controller, depending on your needs.
wherever you implement your new class, be sure that you honor the same name casing from your extended class.
you should have configured the $config['subclass_prefix'] on /application/config/config.php. let's say in your case with the value 'N_'
what i can see from your code, you are not extending from CI_Controller and your path seems wrong.
Informative note: the /application/library is used to place classes and libraries from 3rd parties that won't fit into CI schemas.

Unable to locate the model you have specified - CodeIgniter Issue

I'm getting an unable to locate model error.
$this->load->model('1/Gift_model');
My model file name is gift_model.php within /models/1/.
I declare the model the following way.
class Gift_model extends CI_Model {
According to CodeIgniter's documentation I'm doing it the correct way. Any suggestions? I have 5 other models named exactly the same way and they're all loading fine.
Make the model class name Uppercase My_model
Make the model php file name Lowercase my_model
Load the model using Lowercase (file name) $this->load->model('my_model');
$this->load->model('1/Gift_model'); should be $this->load->model('1/gift_model');. Lowercase on this load argument and the php filename, uppercase on the class name within the file (you had two of three correct).
http://www.codeigniter.com/userguide3/installation/upgrade_300.html
Starting with CodeIgniter 3.0, all class filenames (libraries, drivers, controllers and models) must be named in a Ucfirst-like manner or in other words - they must start with a capital letter.
Used to be the model files started with lower case, but they changed it.
Ensure that the the model name is Gift_model and the class name is also Gift_model
class Gift_model extends CI_Model
{
}
but loading the class is '1/gift_model' NOT 'Gift_model'
$this->load->model('1/gift_model');
hope this was helpful
The problem is that your file name is all lowercase (gift_model.php) while you are loading Gift_model within CodeIgniter. Either change the file name to Gift_model.php or update your code accordingly.
Are you calling the parent's constructor for the model?
class Gift_model extends CI_Model {
function __construct()
{
parent::__construct();
}
-> Model Class name must be Uppercase
-> Model PHP file name must be Lowercase
-> Load Model using Lowercase(filename): $this->load->model('gift_model', TRUE);
Starting with CodeIgniter 3.0, all class filenames (libraries, drivers, controllers and models) must be named in a Ucfirst-like manner or in other words - they must start with a capital letter.
(Source: CI docs)
`if using codeignitor 3.1.3
every thing same otherwise showing error
class name => My_model
file name => My_model.php
load model => $this->load->model('My_model');
call function => $this->My_model->function();`

Where do I put constants file for Codeigniter (PHP)?

I have a list of constants (I'm using them as an enum), some are define statements, and some are just global variables.
Where am I suppose to put them in the MVC framework so I can use them for both my model and my controller that needs to reference it?
I'd rather not stick it into config/constants.php since they shouldn't be called except for by this model and the controllers that use it.
Edit 1: Clarification
To be more specific, I have my message_model model and it has a bunch of constants that I need that are stored in message_model_constants.php. Where should I put message_model_constants.php and is there a way to have it automatically included by the controller that loads message_model when message_model is not (and I don't want it to be) auto-loaded.
Edit 2:
I really don't want to have the constants auto-loaded except for when I use the model
Go to application/config/constants.php and define your constant their and you can use your constants on Model-View-Controller of CI not include "Helper" and "Library"
But in your case I prefer you to create a php file that has your constants and rename it to something like my_constants_helper.php.
In your model or controller __construct just
$this->load->helper('my_constants');
And hooray you can access them =)
You can choose to load a particular config file when you load a particular model in the controller. For instance in your file:
application/controllers/messages.php
You would use a line like this:
$this->config->load('messages');
If you include it at the top of your controller like this
function __construct() {
$this->config->load('messages');
$this->load->model('message_model');
}
Then all of those constants will be available to all the functions and methods in the given controller. You then call each config constant like:
$this->config->item('item name')
And you can name protected $variables; in the construct as well for shorter syntax.
If you are using these config constants and the message model in multiple different controllers you may want make a "Library" file that then loads both the config and the model and declares all variables there.
extending Brennan Novak answer, you can simplify your code by loading your config file in the model constructor. That way, you only have to load the model in your controllers and everything else is done automatically.
Model
class Message_model extends Model {
function __construct()
{
parent::Model();
$this->load->config('message_model_constants');
}
...
}
Controller
class Some_controller extends Controller {
function __construct()
{
parent::Controller();
$this->load->model('message_model');
}
...
}
As already stated, your config files should be application/config/message_model_constants.php
For global configs, add to the config/config.php or create a config/application.php and then load the config, then the item.
$this->config->load('application'); // or autoload this - $autoload['config'] = array('application');
$this->config->item('item name');
Have you considered just adding your constants to your Message_Model Class? You'll then reference them by self::ConstantName inside the Class and Message_Model::ConstantName outside the class. This would also prevent name space collision.

Categories