I have following code:
class MY_Router extends CI_Router {
function __construct() {
parent::__construct();
log_message('debug', "My Child Router Class is here");
} }
I have run the web page and saw log, but didn't find any log message there.
My purpose is that, instaed of default router, I want to use custom made router.
Can some one guide me what and where Iam doing wrong. And how it can be rectified.
Thanks in advance
If you are simply testing to see if the class if being constructed properly, it may be easier to just call an error message. If the error message displays then you're good to go.
show_error("My child router class is up and running.");
Also, ensure that your file is named "MY_Router.php" and is placed in your /application/core directory.
The user guide entry relating to extending core classes can be found at http://codeigniter.com/user_guide/general/core_classes.html
Related
Im current trying to learn more about the core of OpenCart and how its classes actually work. Im also trying to advance my OOP skills in general as Im still learning in that area, so perhaps theres something obvious that Im not seeing.
Im wondering how a controller file knows how to find the cart class (for example).
E.g.
In catalog/controller/checkout cart there is (obviously with code removed)
class ControllerCheckoutCart extends Controller {
public function index() {
$this->cart->update();
}
}
The Controller class can be found in system/engine/controller.php
update() can be found system/library/cart.
I assumed that in the controller.php there would be a link to the cart class, or an object made from it. (Im basing that on the use of $this->).
So how is the cart class actually found from the controller?
Thank you
Firstly, your ControllerCheckoutCart extends the Controller class, so this is the class we need to focus on. You can find this class in /system/engine/controller.php.
Inside this class, there are two magic methods we are interested in. The first is the __construct, where the "registry" class is loaded (found in /system/engine/registry.php if you're interested in picking that apart - it's very simplistic).
You can think of this as a lookup of all the classes the store uses, such as model files, library files and so on. In the construct, the registry is passed to the controller so it has a reference to it
public function __construct($registry) {
$this->registry = $registry;
}
The second and more important magic method is the __get method. This is called when a classes property doesn't exist, for you to handle it yourself if you wish to do so. OpenCart uses this to try and get the class with that key from the registry
public function __get($key) {
return $this->registry->get($key);
}
So $this->cart in any controller would try to get the object with the key cart from the registry. If you look at the index.php file you will see this is allocated in there
// Cart
$registry->set('cart', new Cart($registry));
ControllerCheckoutCart extends Controller, which means it inherits all the code in Controller which you are not seeing here. Some code in Controller, likely in Controller::__construct, is creating the $this->cart object. Example:
class Controller {
public function __construct() {
$this->cart = new Cart;
}
}
Since this constructor is inherited by all child classes, they construct their own $this->cart as well and have access to it in their own methods.
As mentioned by Jay Gilford, you need to register your newly added library class file in the index.php and/or admin/index.php (depending on if you are using it in catalog or admin)
$registry->set('yourlibraryclass', new YourLibraryClass());
so that upon system loading, Opencart knows that your class exists, then you can call all its functions by:
$this->yourlibraryfilename->function();
Please note that your library file name is normally the same as your class name, hence it is used in the example here.
After the change has been done in the index.php files, you need to logout and login again to see the changes.
So, i have some problem.
I just want to create some website where visitor can interact with my site if they're registered.
Let say they've provided their username,email,password, blah..blah..blah...
And then after they provided their blah..blah..blah.. it will autologin (if their data is passed) to my site.
After they logged in my site, they must provided more data again, like they uploaded their profile picture, how they control their privacy in my site, like step by step registration.
I don't want they interact with my site, until they complete their registration.
How do i make every page in my site looks like registration page until they finished their registration.
It's not like i will give this kind of function right.
if(is_login()){
if(is_registration_complete()){
//you're free to go
} else {
// complete your registration first
}
} else {
//you're not logged in
}
In my every controller, if you know what I mean :)
How do I create this function globaly?
If their registration isn't complete, they will go to registration controller, in every routes.
If they complete their registration, they will go to the, yeah you know the default routes.
I'm so sorry if my English is bad, English isn't my native language, sorry for grammar mistakes :)
The easiest is probably to create a library with your checking function and then to include it in the Constructor of the impacted ControllerS :
class Blog extends CI_Controller {
public function __construct()
{
parent::__construct();
// Load the lib here or Autoload
$this->load->library('mylogincheckhelper');
$this->mylogincheckhelper->is_complete();
}
}
And then you do all the ckecks and routing in the Lib.
create a view with your post-registering stuff and make them conditionally visible. and include the view in your templates.
One way you can do it is to create a custom controller by extend the core CI_Controller. Then you can have your page controllers extend from your custom controllers. By extending, you inherit the functions of the parent, as well as run the parent constructor (or run it if you override it), making them "globally available" to whoever extends it.
//extend the core controller
class MY_Controller extends CI_Controller {
//override to provide additional functionality
public function __construct(){
//run the core controller
parent::__construct();
//then do your login and registration checks here
//you can put code here, call another function or load a helper class
//or better, redirect them to your registration page
}
}
//your page's controller extending from your custom controller
class Page extends MY_Controller {
//not overriding the constructor will execute the parent constructor
//every page that extends your extended controller will inherit it's functions
//and execute it's constructor
}
class Api extends CI_Controller {
public function index()
{
show_error("You are not authorized to access this page", 401);
}
I have an api class class Api extends CI_Controller and an another
class myproject extends , now if want to use the functions of the api class in myproject class. how can i do . do i have to create an object of api class or just extend the myproject class with parent::api class . please help me as i m not good at oops.
details - i have made a class "class myproject extends ci_controller" which has different functions for user registration and login application. it all works fine using a single controller. But now want to use an api file.which has functions for login. how can i call those functions in api file from "class myproject"
You can create an helper, like the form one and use your method.
Then you can load it:
$this->load->helper('api');
First of all this looks more like a software design problem rather then a real code issue.
CodeIgniter is based on the MVC concept, and the Controller is only meant to build up the page.
You should probably make a library for all the API functionality, and have the controller calling that library and converting the data to JSON or whatever you want it to be converted to.
For myself i apply a simple rule:
Never write/call a Controller method that doesn't generate any form of output.
I recommend you to stick to the CodeIgniter way of coding, and not try to avoid them.
You should just need to extend the API class, as in
class Myproject extends Api { ... }
but there a few other issues, such as include_once("Api.php"), for example, in the Myproject.php file so that the subclass can "see" the superclass declaration. This thread on the codeigniter forums discusses these issues (sorry for the google webcache version but the forums are down for maintenance at the moment).
Firstly, you can't call a function of a controller in a controller unless and until you are inheriting it.
It's better that you create API class as library, instead of a controller.
If you have some restriction to have it as a controller only then use these APIs through curl calls.
But the best way is to have them in helper, if you are accessing them from your own server only.
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
I had been wondering why my error page caused certain pages of my site
not to render, but then I realized that it's because AppError extends
ErrorHandler instead of AppController. This caused some variables
that I set in AppController's beforeFilter method not to be sent to
the view. Since I can't access session variables from AppError, I
thought that I might be able to get away with using the classRegistry
to instantiate something that could and simply copying and pasting the
rest of my code from AppController's beforeFilter... but that isn't working, nor does it seem like a very elegant fix. Does anyone have any clues as to what
would be the best way to approach this? Thanks, David.
Your AppError class has a controller instance. You can call the beforeFilter manually:
<?php
class AppError extends ErrorHandler {
function error404() {
$this->controller->beforeFilter();
parent::error404();
}
}
?>
In CakePHP 2, you can do something like this to achieve the same effect. In app/Config/bootstrap.php, add this line:
Configure::write('Exception.renderer', 'AppExceptionRenderer');
Then create a file app/Lib/Error/AppExceptionRenderer.php with this code:
App::uses('ExceptionRenderer', 'Error');
class AppExceptionRenderer extends ExceptionRenderer {
protected function _outputMessage($template) {
$this->controller->beforeFilter();
$this->controller->render($template);
$this->controller->afterFilter();
$this->controller->response->send();
}
}
Described more generally here: http://book.cakephp.org/2.0/en/development/exceptions.html#using-a-custom-renderer-with-exception-renderer-to-handle-application-exceptions
Edit: Updated link to point to correct location of the CakePHP 2.0 Book as of July 05, 2012.