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
Related
I have two controllers. Let's say A and B.
For clarity's sake I want to run some functions in B controller but I have trouble getting back to A.
Of course I could just run
$this->load->view('pages/examplepage');
But this example page has to get data from database and of course it gives me an error that it doesn't have the necessary data.
Now I could just run
$data['exampledata'] = $this->example_model->get_data();
$this->load->view('pages/examplepage', $data);
Now for couple of rows it's okay but isn't there a better way? What am I missing? Can't I just run the A controller's function from B controller and let the already existing function do the job?
Hope this will help you :
user codeigniter url helper redirect()
Example :
Controller A
class Abc extends CI_Controller {
public function first()
{
redirect('xyz/some_method');
}
}
Controller B
class Xyz extends CI_Controller {
public function some_method()
{
redirect('abc/first');
}
}
for More : https://www.codeigniter.com/user_guide/helpers/url_helper.html
I am learning codeigniter 3
In my config.php:
$config['base_url'] = 'http://'.$_SERVER['HTTP_HOST'].'/ci3x/admin/';
In my routes.php:
$route['customer'] = 'customer/index';
In controllers/customer.php:
class Customer extends MY_Controller{
function index(){
// some code here
}
}
When I type: http://localhost/ci3x/admin/customer on brower, it back error 404.
I have no clue to fix, please help me to solve it.
Many thanks
As you are extending a Class and that class does have a constructor, your class needs a constructor that also calls the constructor of the extended class.
Else things won't get up and running correctly.
class Customer extends MY_Controller{
public function __construct() {
parent::__construct(); // Call the MY_Controller constructor
}
function index(){
// some code here
}
}
The same goes for your MY_Controller constructor as it will need to call CI_Controller constructor... It ripples down and everything gets initialised correctly (in simplistic terms) .
Note:
Be careful with your controller and method files names. If you read the user guide it will say that the file names for controllers and methods should start with a capital letter.
So your controllers/customer.php should be controllers/Customer.php. If you are running on Windows it won't care. If you are running on Linux it will definitely matter.
I have a relatively simple question. I am trying to inherit a constructor from a php superclass to authenticate on this controller.
Here is my super class:
class Auth_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
if(!session_id()){
session_start();
}
$this->load->view('login_v/logincheck');
}
}
and here is my subclass:
class Event_Controller extends Auth_Controller {
function __construct()
{
parent::__construct();
}
public function get_events_by_owner() {
$this->load->model('Event_model');
$data['events'] = $this->Event_model->select_by_owner($_SESSION['SignedIn']);
$this->load->view('event_view', $data);
}
}
This is not working. only a white page is rendered. I'm not sure why it isn't working. If I move the constructor from Auth_Controller to Event_Model this works.
Thanks!
EDIT:
Fatal error: Class 'Auth_Controller' not found in
../controllers/event_controller.php on line 12
Your solution is not going to work, you should try either:
Move this code:
if(!session_id()){
session_start();
}
$this->load->view('login_v/logincheck');
to a cutom library, than run this library within constructor of your controller. Please read Creating Libraries for details.
or:
Create MY_Controller class and put auth code (quoted above) into its constructor. Than you'll be able to extend like:
class Event_Controller extends MY_Controller {
(...)
Please read Creating Core System Classes for details.
I'm using CodeIgniter and I want to create some sort of filter to know when a user may/may not access a current controller
If anyone has any idea how to implement this in a different manner, great, but what I was thinking (and trying to do) is the following:
CI_Controller - which is the basic CodeIgniter controller class
MY_Controller - the basic controller which I use which extends CI_Controller
[Controller] - any "physical" controller
what i've tried to do is:
MY_Controller.php
class MY_Controller extends CI_Controller{
private static $namespace = null;
private static $permission = array('site', 'settings');
public function __construct(){
if ((!isset($_SESSION['user'])) && (in_array(__CLASS__, $permission))){
throw new Exception('Unauthorized');
}
parent::__construct();
}
}
obviously, this doesn't work as CLASS will always be that of MY_Controller and not that of the child object... and NAMESPACE doesn't work aswell.
Anyone has any idea? because Id really hate to start putting this snippet of code in every other class, and I'll prolly need the filtering later for some more elaborate things...
As there's no answer so far, maybe this suggestion might help. It's a different approach, though, that uses the Modular Extensions - HMVC ...
Like this you can have a module "login" with a controller that holds some methods to log a user in, to check session status and to log a user out (and the like).
In each other module you can now load the module login and check for the status and redirect if needed ...
There's a tutorial HMVC: an Introduction and Application I followed on nettuts that shows how to do the CodeIgniter From Scratch: Day 6 – Login in that way. Maybe that helps. I had some difficulties as it's with an old codeigniter version. So maybe this thread helps.
It's not how you're trying to do things, but maybe it helps!
I did not try it but what I saw in this forum might be working for you. The predicted code is:
class MY_Controller extends Controller{
private static $permission = array('site', 'settings');
public function __construct(){
if ((!isset($_SESSION['user'])) && (in_array(__CLASS__, $permission))){
throw new Exception('Unauthorized');
}
parent::Controller();
}
}
But then you'd need to use
class My_controller extends MY_Controller
instead of
class My_controller extends Controller
I solved it in the following manner... simply in the constructor, I defined the current class
class MY_Controller extends CI_Controller{
private static $permission = array('site', 'settings');
public function __construct($currentController = __CLASS__){
if ((!isset($_SESSION['user'])) && (in_array($currentController , $permission))){
throw new Exception('Unauthorized');
}
parent::Controller();
}
}
when calling the physical controller, we simply write as follows
class PhysicalController extends MY_Controller{
public function __construct(){
parent::__construct(__CLASS__);
}
}
I have class MY_Controller extends CI_Controller and common logic for big profile section, so I'va tried to create class Profile extends MY_Controller with common logic for profile section and all class related to this section should extends this Profile class as I understand right, but when I tried to create class Index extends Profile I recieve an error:
Fatal error: Class 'Profile' not found
CodeIgniter tries to find this class in index.php which I am running.
Where is my mistake? Or maybe there is anoter better way to mark out common logic?
I take it you have put your MY_Controller in /application/core, and set the prefix in the config.
I would be careful about using index as a class name though. As a function/method in Codeigniter it has a dedicated behaviour.
If you then want to extend that controller you need to put the classes in the same file.
E.g. In /application core
/* start of php file */
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
}
...
}
class another_controller extends MY_Controller {
public function __construct() {
parent::__construct();
}
...
}
/* end of php file */
In /application/controllers
class foo extends MY_Controller {
public function __construct() {
parent::__construct();
}
...
}
or
class bar extends another_controller {
public function __construct() {
parent::__construct();
}
...
}
I found this page on Google because I had the same problem. I didn't like the answers listed here so I created my own solution.
1) Place your parent class in the core folder.
2) Place an include statement at the beginning of all classes that include the parent class.
So a typical controller might look like this:
<?php
require_once APPPATH . 'core/Your_Base_Class.php';
// must use require_once instead of include or you will get an error when loading 404 pages
class NormalController extends Your_Base_Class
{
public function __construct()
{
parent::__construct();
// authentication/permissions code, or whatever you want to put here
}
// your methods go here
}
The reason I like this solution is, the whole point of creating a parent class is to cut down on code repetition. So I don't like the other answer that suggested copy/pasting the parent class into all of your controller classes.
It is possible with Codeigniter 3. Just including the parent file is enough.
require_once(APPPATH."controllers/MyParentController.php");
class MyChildController extends MyParentController {
...
All classes you are extending should live in application/CORE directory so in your case both My_Controller and Profile should live there. All "end point" controllers will live in application/controllers folder
UPDATE
I stand corrected. Extended classes should live in the same file. #Rooneyl's answer shows how to implement
After some struggle with version 3 and this issue I decided this was not a bad solution...
require_once BASEPATH.'core/Controller.php';
require_once APPPATH.'core/MYCI_Controller.php';
to add this second line where the first exists in the system/core/CodeIgniter.php
[If it's not too late, I recommend strongly against php and/or CodeIgniter.]