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');
}
}
Related
I'm trying to create a new site and it includes database, as it said every time you want to access your database you need to do it in your Model but I don't know why I am having a problem with my Model. Please the codes below.
Controller:main
class Main extends CI_Controller {
public function index()
{
$this->home();
}
public function home(){
$this->announce($data);
}
public function announce(){
$this->load->library('table');
$this->load->model('home_model');
//echo "test";
}
Model:home_model.php
class Home_model extends CI_Model{
echo '';
}
after running the program. localhost/ci_gcc/main/announce or localhost/ci_gcc/main I got this error.
Parse error: parse error, expecting `T_FUNCTION' in C:\wamp\www\ci_gcc\application\models\home_model.php on line 4
but if I didn't type anything inside the model class there is no error.
Thus anyone have a solution for this?
You are doing a bad attempt. Don't echo like this in model class.
$this->load->model('home_model');
This will load your model class. Inorder to do any operations inside the model class, you have to write functions inside it. And you can call that function from your controller like,.
$this->home_model->functionname();
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.
One stupid question, I want to call from my admin_news controller function which resides in another controller Admin. FUnction name is is_logged_in();
admin.php
public function is_logged_in()
{
....
}
admin_news.php
public function __contruct()
{
parent::__construct();
//admin->is_logged_in();??
}
how can I do that?
Thanks
You will have to move that functionality somewhere else, Codeigniter's architecture doesn't allow multiple controller instances in one request. You have multiple options like using a common base class, libraries, helpers and so on.
I would recommend you to create your own MY_Controller base class (see Extending Core Classes) and put your method there, like this:
class MY_Controller extends CI_Controller {
protected function is_logged_in() {
// ...
}
}
Once you have it there you can call it like:
class AdminNews extends MY_Controller {
public function __construct() {
parent::__construct();
$this->is_logged_in();
}
}
I have a set of controllers that should only be accessible if you are an admin (as opposed to a regular user).
Thus, in the constructor for each of the controllers, I would do:
public function __construct() {
parent::__construct();
if (! is_admin()) {
show_404();
}
}
Instead of adding this code to the constructor of every Admin Controller, is there a better way to do this?
I was thinking I could create a Base controller called Admin_Controller that would look like this:
public class Admin_Controller extends CI_Controller {
public function __construct() {
//the above code goes here
}
}
And then all my other controllers can extend this class, instead of the CI_Controller class. The only problem with this is, I need to include this file at the top of my other controllers, or CodeIgniter cannot find Admin_Controller.
Is there a better way to do this?
place this in your application/core folder: MY_Controller.php (note the correct use of capitals)
class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
if (! is_admin())
{
show_404();
}
}
Then in all your normal controllers that you want users to be logged in
class Whatever extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
}
See userguide here on extending core classes
I think that you could use Hooks
I've done the same thing, but I require a much more precise control so I have to check on every controller. Not all controllers are forbidden for single users.
I have created an admin controller that extends the blog controller (both are located in application/controller). However, when I tried to access the controller, it gives me an error that the blog controller was not found. If I put require_once(APPPATH.'controllers/blog.php'); inside the admin.php file it works. But I'm just wondering if there's another possible way to extends the blog controller without having to use require_once inside the admin.php
From CI user guide
If you are extending the Controller
core class, then be sure to extend
your new class in your application
controller's constructors.
class Welcome extends MY_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->view('welcome_message');
}
}
That means your Blog controller must extends CI_Controller
Example:
class MY_Blog extends CI_Controller {
function hello() {
$data = 'something';
}
}
class Admin extends MY_Blog {
function do_something() {}
}
Userguide