Extended Core class of CI advanced usage

Source: Internet
Author: User

In the previous article CI advanced usage to create your own class library, do you think that each controller's construction method to execute the checksum code actually violates the programming specification of the dry (do ' NT repeat yourself) principle?

In fact, we can completely put the code of the checksum in the constructor of the parent class. The parent class of the controller in CI is Ci_controller, now let's extend the parent class.

Create a class file in the Application/core directory, my_controller.php, with the following contents:

<?phpclass My_controller extends Ci_controller{public function __construct () {parent::__construct (); $this Load->library (' auth '); $this->auth->checkpower ();}}


After modifying the welcome controller to the following version, let it inherit the My_controller:

<?phpdefined (' BasePath ') or exit (' No Direct script access allowed '); class Welcome extends My_controller {/** * Index Pa GE for this controller. * Maps to the following URL * http://example.com/index.php/welcome *-OR-* http://example.com/index.php/welcome/index *-OR-* Since This controller is set as the default controller in * config/routes.php, it's displayed at Http://example. com/* * So no other public methods not prefixed with an underscore would * map to/index.php/welcome/<method_name> * @see http://codeigniter.com/user_guide/general/urls.html */public function __construct () {parent::__construct ();} Public Function Index () {$this->load->view (' Welcome_message ');}}
Visit http://localhost/ci2/again, the result is the same error message:

Illegal access


Attention:

① extended classes must be declared to be extended by the parent class.

② the file where the newly extended class resides must be prefixed with my_ (this option is configurable, as described below).


If you do not like my as the class prefix, you can find this item in the application/config/config.php file modified to be a prefix name other than ci_ :

$config [' subclass_prefix '] = ' my_ ';

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Extended Core class of CI advanced usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.