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.
Related
I have a Controller class that extends from AppController in CakePHP.
And this Controller class has a public function .. lets say testFunc()
class xyzController extends AppController {
..
..
public function testFunc($params, $auth_username)
{
..
..
}
}
I have made this function public since I need to call it from another Controller that too extending from AppController.
class abcController extends AppController {
..
..
public function callingFunc()
{
...
$controller = new xyzController($this->request, $this->response);
$controller->testFunc($params, $username);
}
..
..
}
But since I made it public, I see that testFunc() is accessible using curl command for the below https path
https://[ip_address]/xyz/testFunc/arg=test/root
As you can see, the above path takes "root" as argument to testFunc() and gives full access to anyone using the above path in curl command.
My requirement is to remove this security issue.
I am totally new to PHP and CakePHP. Can someone please give any pointers to how I can proceed?
"I need to call it from another Controller". This is almost never actually true, it usually indicates a flawed design. If testFunc is not meant to be accessed through a browser, then move it to somewhere that both controllers can access it without it being a public member. For example, make it a protected (or even private) member of your AppController, or if it's doing model-specific stuff, maybe it can be moved to that model's table class.
The solution is pretty simple.
public function _testFunc($params, $auth_usernmae) will do the magic.
In Cakephp, if the function has an underscore as prefix, then it cannot be accessed using URI but can be accessed internally from other functions.
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.]
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
In my CI system\libraries directory I have a new class named DD_Controller.php. This file looks like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class DD_Controller extends Controller
{
protected $ddauthentication;
function __construct()
{
parent::Controller();
$this->ddauthentication = "Authenticated";
}
}
?>
My application controller is defined like this:
class Inquiry extends DD_Controller
{...}
The Inquiry class works fine when I extend Controller, but I get a
Fatal error: Class 'DD_Controller' not
found in
C:\development\localhost\applications\inquiry\controllers\inquiry.php
on line 4
When I extend DD_Controller. In the config file I have the prefix defined as such:
$config['subclass_prefix'] = 'DD_';
Any idea of what I'm missing?
TIA
This is a better approach. Do the following:
Go to the following directory: your_ci_app/application/core/ and create a php file called MY_Controller.php (this file will be where your top parent classes will reside)
Open this the file you just created and add your multiple classes, like so:
class Admin_Parent extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function test() {
var_dump("from Admin_Parent");
}
}
class User_Parent extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function test(){
var_dump("from User_Parent");
}
}
Create your children controllers under this directory your_ci_app/application/controllers/ . I will call it adminchild.php
Open adminchild.php and create your controller code, make sure to extend the name of the parent class, like so:
class Adminchild extends Admin_Parent {
function __construct() {
parent::__construct();
}
function test() {
parent::test();
}
}
DD_Controller.php should be in /system/application/libraries/
If you're using the same CI for multiple apps, and you want them all to be able to extends their controllers to your custom one then you can extend the base Controller class in the same file.
In system/libraries/Controller.php below the Controller class:
class Mega_Controller extends Controller {
function Mega_Controller()
{
parent::Controller();
// anything you want to do in every controller, ye shall perform here.
}
}
Then you'll be able to do this in your app controllers:
class Home extends Mega_Controller {
....
Since the extended controller class you created will be available. I think this is better then overwriting the base controller, but that would work as well.
I recommend to avoid "cracking" CodeIgniter core files.
Better use its native extending possibilities and try to fit into them.
The same rule I would recommend for any PHP library / CMS.
This rule has few reasons:
- ability to quiclky upgrade without takint into account thousands of notes where and how was cracked in core files;
- portability;
- possibility to share your code - eg, this will be usable by both you and your friends in case of need, and it will help them to keep their library up to date, the same as you.
In other words, this is much more professional and it pays to you in the future by usability, portability and by update application possibility.
Regarding your personal question...
As for me, there is nothing bad to create your own library with everything you need to extend native CodeIgniter Controller, then load this library in Controller's constructor and you are done. The only thing to make better usability is to give short name to your library.
This way you can even divide what you need in different pieces and put into separate libraries:
WebFeatures
AdminFeatures
etc.
Then you just load needed libraries in your controller's constructor and you are done.
P.S. I know that proposed way does not fit into "right" OOP concept, but in the same time you must never forget about the integrity of the libraries used.
Everything above is just one more view of mine 7-years experience in professional web development, so I hope it will be helpful if not to follow, then at least to take into account.
Regards,
Anton