Detach the database category of CodeIgniter

Source: Internet
Author: User
Okay, because the organization needs to, and recently started to switch to php, the business logic is okay to say, mainly because the boss asked to add login state verification on the data access layer. In fact, this requirement is also reasonable. Internet services require the upper layer to protect the lower layer, but the lower layer cannot fully trust the upper layer. But the problem arises,... SyntaxHighlighter. all ();

Okay, because the organization needs to, and recently started to switch to php, the business logic is okay to say, mainly because the boss asked to add login state verification on the data access layer.
In fact, this requirement is also reasonable. Internet services require the upper layer to protect the lower layer, but the lower layer cannot fully trust the upper layer. But the problem arises. There are two solutions:
1. write a mysql proxy server to assemble the requests sent by the caller and return them to the caller. The main difficulty of doing so is:
A) SQL statement assembly and serialization
B) data set serialization. although there are many products in this field, it is still too complicated after all, and there is no time to get involved.
Give up.
2. encapsulate a mysql api and the caller can directly call it locally. In this case, you only need to consider the SQL statement assembly. Now there are a lot of options,
A) use a Model class similar to the Model class in django.
B) use Active Record in ci
Although the Model method effectively blocks the data layer, the team members generally think that this method is too heavy and lightweight, and finally chose the AR in CodeIgniter.
Okay. now, it's time to test the splitting of the ci module!
I will not talk about all the hard work in the middle. I will simply copy the system \ database to a separate directory, x:/php /.
Create a file myconfig. php:
Define ('basepath', dirname (_ FILE __).'/');
Define ('ext ','. php ');
Require_once (BASEPATH. 'database/db'. EXT );
 
Function & instantiate_class (& $ class_object)
{
Return $ class_object;
}
 
Function log_message ($ level = 'error', $ message, $ php_error = FALSE)
{
Echo ($ message );
}
Function MYDB ()
{
$ Params = array (
'Dbdriver '=> 'mysql ',
'Hostname' => 'localhost ',
'Username' => 'root ',
'Password' => '',
'Database' => 'Dante ',
'Pconnect '=> TRUE,
'Db _ debug' => FALSE,
'Cache _ on' => FALSE,
'Char _ set' => 'utf-8 ',
'Dbcollat' => 'utf8 _ general_ci ',
);
$ Db = DB ($ params, TRUE );
Return $ db;
}
?>
Create a test file test. php:
 
Require_once ('myconfig. php ');
$ Db = MYDB ();
$ Db-> select ('Id, user_login, user_email ');
$ Query = $ db-> get ('WP _ users ');
Echo "\ n ";
Foreach ($ query-> result () as $ row)
{
Print $ row-> ID. "\ n ";
Print $ row-> user_login. "\ n ";
Print $ row-> user_email. "\ n ";
}
?>
The input result is as follows:

Database Driver Class Initialized
1
Admin
Zny2008@gmail.com
OK ~~~ In this way, if we need to perform permission verification before data access, we only need to make a judgment in the MYDB function.
In addition, I have to say that the ci module is really good to split. instantiate_class comes from its system \ codeigniter \ Common. php. I have rewritten log_message, because the log writing method is different for each caller. (For example, I printed it on the screen this time ....), Recently, we are looking at the Design Mode. this method also conforms to the template mode.

Author: "wolf's personal space"

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.