couldn't load model properly codeigniter - php

I have loaded the 2 models on a function in a controller. In my first controller, loading both models works. Now, when I load the same models in the same order, my second controller makes an error which is thrown to ajax. When I delete the user_model - the first model, it works fine: ajax fires its success function.
How come it throws error when I try loading the user_model? It works perfectly fine on a different controller.
Here's the code of the constructor method in my second controller
public function __construct(){
parent::__construct();
$this->load->model('friend_model');
$this->load->model('user_model');
if (!isset($_SESSION)) session_start();
}
It's the same constructor method that I used in my another controller.
the error I got from the $.ajax error function is parsererror.

You will load all models,libraries and helpers file in constructor in controller file
<?php
class usercontroller extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->model('user_model');
}
}
?>
it's works fine.

Related

Accessing controller's function

I am using Codeigniter framework to develop a website. I am currently working on home.php view under the view folder. I need to use UserInfo() function which is inside one of the controllers. Any suggestion how to access that function?
class Welcome extends CI_Controller {
public function UserInfo(){
$this->load->model('model_user');
$data['title'] = 'Users';
$data['users'] = $this->model_user->getUser();
$this->load->view('template/users', $data);
}
}
You cant call controller method inside another controller. Its No Way to do it.
You have two way to resolve this issue
If you want to access the function which place inside the
controller, add that into an model. So by loading model you can call
it.
use redirect('welcome/UserInfo') if you just need to call the function
As You want to call controller function in other controller.In codeigniter App folder core folder exists you make a custom core controller and all other controllers extend with your custom controller
In your Custom Core controller
class CustomCore extends CI_Controller
{
/* ---YOUR FUNCTION IN CUSTOMCORE---- */
public function mycorefunc()
{
//Do something
}
}
and your all other controllers extend with custom core
class YourController extends Customcore
{
function controllerfunction()
{
$this->mycorefunc();// Call corefunction
}
}

Global Query Data

There's a part of a code (query) that will be required in all controllers, they will be passed into views for display.
Can I know is there anyway to declare them in just a single file so that I can reference them directly from my view? Without declaring them in each controller's _construct.
I'm using codeigniter3, here's a sample code:
MainController.php
public function index(){
$data['userCampaign'] = $this->Usermodel->getCampaign();
}
Create default controller in your project which extends CI_Controller and your all controller extends new controller and in __construct(); function of your new controller you can add this code.
Declare this function as protected in parent controller class.
No. I don't know there is method like that.
You want call the function in __construct() or you have to declare function in controller and call it back. $this->check_session()

Codeigniter doesnt load the model

I have a controller class and inside i have a contructor, that loads a specific model. I've checked the model code, theres not anything wrong in it. However the controller script shows a white screen error if that model is loaded.
Heres the controller code:
class Home extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('Adder');
}
public function send_feedback()
{
$this->Adder->addTo('feedback');
}
}

how do i pass some controller methods to an auth check in codeigniter/php

i am using ion auth and codeigniter to build a web app, i would like to specifiy some methods of a controller to maybe an array or something that i can perform a authentication function- and some not, there must be an easier way of added a if/else statement to the function
the reason being here is a very quick examplle
class employers extends MY_Controller{
function __construct (){
parent::__construct();
}
//i want these to be public
function index(){}
function signup(){}
//i wan these to require a login
function post_job(){}
function edit_job(){}
function delete_job(){}
Segment the pages or controller actions. You want to require authentication for a controller class. Then you can add a __construct method for that controller class that checks the session to see if the user is logged in. See below
class employers extends MY_Controller{
function __construct (){
parent::__construct();
}
function index(){}
function signup(){}
And
class employersDashboard extends MY_Controller{
function __construct ()
{
// Add code to check authentication. If they pass redirect them to one of the actions below.
// If they fail, redirect them to "signup" action in controller "employers" above
parent::__construct();
}
function post_job(){}
function edit_job(){}
function delete_job(){}

OOP PHP Codeigniter Controller Hierarchy

I'm having a litle problems with my concepts of OOP. I'll try to explain the best I can.
I have this class
class Application_controller extends CI_Controller{
public function addItem(){
"some code to add the item to the database (working)";
}
}
And I have another class, both controllers:
require_once 'application_controller.php';
class Contact extends Application_controller{
public function __construct(){
parent::__construct("variables needed");
}
}
And in the View add of the contact I added the following action contact/addItem.
Ok, now here's what I know about OOP in general.
Isn't the method addItem supposed to be part of the Contact class because its extends Application_controller?
I'm asking because when I submit the form I get no action, and when I add the method addItem in the class Contact overriding the parent one it works.
The reason you get no action is that codeigniter doesn't find a method addItem in your Contact class (update: this is probably due to the way CodeIgniter routing works). The solution would be to make addItem a generic method in a Model that stores data in a table, move it to a Model, and load the model in your controller.
Create application/models/writeModel.php
class writeModel extends CI_Model{
function addItem(){
// code here
}
}
In your controller:
class Contact extends Controller{
function __controller(){
parent::Controller();
$this->load->model('writeModel');
}
function somefunction(){
$this->writeModel->addItem(); // call the method here
}
}
Reference: CodeIgniter Models
The problem here (other then the several syntax errors in the OP) is likely to be that "Contact" can not extend "Application_controller" because it does not know it exists. If we setup a test like this:
/controllers/Test.php
class Test extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
echo 'test';
}
}
/controllers/TestTwo.php
require_once("Test.php");
class TestTwo extends Test
{
function __construct()
{
parent::__construct();
}
function index()
{
parent::index();
echo ' and test two';
}
}
We will get the desired output of "test and test two" by navigating to appurl/TestTwo/. This is because TestTwo knows of Test. Removing the require(); line from TestTwo.php will break the relation.
Removing the index() function from TestTwo will then result in only "test" being output by navigating to appurl/TestTwo/.
I found an answer to some similar question on the Codeigniter forums. It says this
your ShopDownloads will inherit (methods,properties etc etc) from the Shop controller. and as said in the video tutorial, u must inherit your class from the controller class so that it can inherit all the properties and methods codeigniter provides for u.
Sohaib,
The link for the post is http://codeigniter.com/forums/viewthread/102718/#518120
I don't know how but this is working today. It was probably the server. Just needed a restart.
Its Solved, just by start the server today and start developing LOL. Thanks for youre time guys.
Regards,
Elkas

Categories